Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WizFi310Interface_Legacy by
TCPSocketConnection.cpp
00001 /* 00002 * Copyright (C) 2013 gsfan, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 /* Copyright (C) 2014 Wiznet, MIT License 00020 * port to the Wiznet Module WizFi250 00021 */ 00022 /* Copyright (C) 2017 Wiznet, MIT License 00023 * port to the Wiznet Module WizFi310 00024 */ 00025 00026 #include "TCPSocketConnection.h" 00027 #include <algorithm> 00028 00029 TCPSocketConnection::TCPSocketConnection() {} 00030 00031 int TCPSocketConnection::connect(const char* host, const int port) 00032 { 00033 if (set_address(host, port) != 0) return -1; 00034 00035 _server = false; 00036 _cid = _wizfi310->open(WizFi310::PROTO_TCP, get_address(), get_port()); 00037 printf("_cid : %d\n",_cid); 00038 if (_cid < 0 ) return -1; 00039 00040 return 0; 00041 } 00042 00043 bool TCPSocketConnection::is_connected(void) 00044 { printf("_cid: %d\n", _cid); 00045 bool _is_connected = _wizfi310->isConnected(_cid); 00046 if(!_is_connected) _cid = -1; 00047 printf(" is_connected: %d\n",_is_connected); 00048 return _is_connected; 00049 } 00050 00051 int TCPSocketConnection::send(char *data, int length) 00052 { 00053 if (_cid < 0 || !is_connected()) return -1; 00054 00055 return _wizfi310->send(_cid, data, length); 00056 } 00057 00058 int TCPSocketConnection::send_all(char *data, int length) 00059 { 00060 Timer tmr; 00061 int idx = 0; 00062 00063 if(_cid <0 || !is_connected()) return -1; 00064 00065 tmr.start(); 00066 while((tmr.read_ms() < _timeout) || _blocking) 00067 { 00068 idx += _wizfi310->send(_cid, &data[idx], length - idx); 00069 if(idx < 0) return -1; 00070 00071 if(idx == length) 00072 return idx; 00073 } 00074 return (idx == 0) ? -1 : idx; 00075 } 00076 00077 int TCPSocketConnection::receive(char* data, int length) 00078 { 00079 Timer tmr; 00080 int time = -1; 00081 int len = 0; 00082 00083 if(_cid < 0 || !is_connected()) return -1; 00084 00085 if(_blocking) 00086 { 00087 tmr.start(); 00088 while(time < _timeout) 00089 { 00090 len = _wizfi310->readable(_cid); 00091 if(len == -1) 00092 return len; 00093 00094 if(len > 0) 00095 { 00096 WIZ_DBG("receive readable : %d\r\n",len); 00097 break; 00098 } 00099 time = tmr.read_ms(); 00100 } 00101 if(time >= _timeout) 00102 { 00103 //WIZ_DBG("receive timeout"); 00104 return 0; 00105 } 00106 } 00107 00108 return _wizfi310->recv(_cid, data, length); 00109 } 00110 00111 int TCPSocketConnection::receive_all(char* data, int length) 00112 { 00113 Timer tmr; 00114 int idx = 0; 00115 int time = -1; 00116 00117 if(_cid < 0 || !is_connected()) return -1; 00118 00119 tmr.start(); 00120 00121 while(time < _timeout || _blocking) 00122 { 00123 idx += _wizfi310->recv(_cid, &data[idx], length - idx); 00124 if (idx < 0) return -1; 00125 00126 if(idx == length) 00127 break; 00128 00129 time = tmr.read_ms(); 00130 } 00131 00132 return (idx == 0) ? -1 : idx; 00133 } 00134 00135 void TCPSocketConnection::acceptCID (int cid) 00136 { 00137 char *ip; 00138 int port; 00139 _server = true; 00140 _cid = cid; 00141 00142 00143 if( _wizfi310->cmdSMGMT(cid) ) return; 00144 if( !_wizfi310->getRemote(_cid, &ip, &port)) 00145 { 00146 set_address(ip, port); 00147 } 00148 }
Generated on Thu Jul 14 2022 15:18:32 by
1.7.2
