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_Legacynew 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 if (_cid < 0 ) return -1; 00038 00039 return 0; 00040 } 00041 00042 bool TCPSocketConnection::is_connected(void) 00043 { 00044 bool _is_connected = _wizfi310->isConnected(_cid); 00045 if(!_is_connected) _cid = -1; 00046 00047 return _is_connected; 00048 } 00049 00050 int TCPSocketConnection::send(char *data, int length) 00051 { 00052 if (_cid < 0 || !is_connected()) return -1; 00053 00054 return _wizfi310->send(_cid, data, length); 00055 } 00056 00057 int TCPSocketConnection::send_all(char *data, int length) 00058 { 00059 Timer tmr; 00060 int idx = 0; 00061 00062 if(_cid <0 || !is_connected()) return -1; 00063 00064 tmr.start(); 00065 while((tmr.read_ms() < _timeout) || _blocking) 00066 { 00067 idx += _wizfi310->send(_cid, &data[idx], length - idx); 00068 if(idx < 0) return -1; 00069 00070 if(idx == length) 00071 return idx; 00072 } 00073 return (idx == 0) ? -1 : idx; 00074 } 00075 00076 int TCPSocketConnection::receive(char* data, int length) 00077 { 00078 Timer tmr; 00079 int time = -1; 00080 int len = 0; 00081 00082 if(_cid < 0 || !is_connected()) return -1; 00083 00084 if(_blocking) 00085 { 00086 tmr.start(); 00087 while(time < _timeout) 00088 { 00089 len = _wizfi310->readable(_cid); 00090 if(len == -1) 00091 return len; 00092 00093 if(len > 0) 00094 { 00095 WIZ_DBG("receive readable : %d\r\n",len); 00096 break; 00097 } 00098 time = tmr.read_ms(); 00099 } 00100 if(time >= _timeout) 00101 { 00102 //WIZ_DBG("receive timeout"); 00103 return 0; 00104 } 00105 } 00106 00107 return _wizfi310->recv(_cid, data, length); 00108 } 00109 00110 int TCPSocketConnection::receive_all(char* data, int length) 00111 { 00112 Timer tmr; 00113 int idx = 0; 00114 int time = -1; 00115 00116 if(_cid < 0 || !is_connected()) return -1; 00117 00118 tmr.start(); 00119 00120 while(time < _timeout || _blocking) 00121 { 00122 idx += _wizfi310->recv(_cid, &data[idx], length - idx); 00123 if (idx < 0) return -1; 00124 00125 if(idx == length) 00126 break; 00127 00128 time = tmr.read_ms(); 00129 } 00130 00131 return (idx == 0) ? -1 : idx; 00132 } 00133 00134 void TCPSocketConnection::acceptCID (int cid) 00135 { 00136 char *ip; 00137 int port; 00138 _server = true; 00139 _cid = cid; 00140 00141 00142 if( _wizfi310->cmdSMGMT(cid) ) return; 00143 if( !_wizfi310->getRemote(_cid, &ip, &port)) 00144 { 00145 set_address(ip, port); 00146 } 00147 }
Generated on Wed Jul 13 2022 02:14:29 by
1.7.2
