Freedman v2
Fork of WizFi250Interface by
Embed:
(wiki syntax)
Show/hide line numbers
UDPSocket.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 /* Copyright (C) 2014 Wiznet, MIT License 00019 * port to the Wiznet Module WizFi250 00020 */ 00021 00022 #include "UDPSocket.h" 00023 00024 #include <string> 00025 #include <algorithm> 00026 00027 UDPSocket::UDPSocket() 00028 { 00029 endpoint_connected = false; 00030 } 00031 00032 int UDPSocket::init(void) 00033 { 00034 _server = false; 00035 return 0; 00036 } 00037 00038 // Server initialization 00039 int UDPSocket::bind(int port) 00040 { 00041 _port = port; 00042 _server = true; 00043 return 0; 00044 } 00045 00046 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) 00047 { 00048 Timer tmr; 00049 int idx = 0; 00050 00051 if (_cid < 0 && _wizfi250->isAssociated()) 00052 { 00053 if (_server) 00054 { 00055 _cid = _wizfi250->listen(WizFi250::PROTO_UDP, _port); 00056 } 00057 else 00058 { 00059 _cid = _wizfi250->open(WizFi250::PROTO_UDP, remote.get_address(), remote.get_port(), _port); 00060 } 00061 if (_cid < 0) return -1; 00062 } 00063 00064 tmr.start(); 00065 00066 while ((tmr.read_ms() < _timeout) || _blocking) 00067 { 00068 if(_server) 00069 { 00070 idx += _wizfi250->sendto(_cid, packet, length, remote.get_address(), remote.get_port()); 00071 } 00072 else 00073 { 00074 idx += _wizfi250->send(_cid, packet, length); 00075 } 00076 if (idx < 0) 00077 { 00078 if (!_wizfi250->isConnected(_cid)) _cid = -1; 00079 } 00080 00081 if (idx == length) 00082 return idx; 00083 } 00084 return (idx == 0) ? -1 : idx; 00085 } 00086 00087 00088 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) 00089 { 00090 Timer tmr; 00091 int idx = 0; 00092 int time = -1; 00093 char ip[16]; 00094 int port; 00095 00096 if(_cid < 0 && _wizfi250->isAssociated()) 00097 { 00098 // Socket open 00099 if (_server) { 00100 _cid = _wizfi250->listen(WizFi250::PROTO_UDP, _port); 00101 //WIZ_DBG("TEST CID : %d",_cid); 00102 } 00103 else 00104 { 00105 _cid = _wizfi250->open(WizFi250::PROTO_UDP, remote.get_address(), remote.get_port(), _port); 00106 } 00107 if (_cid < 0) return -1; 00108 } 00109 00110 if (_blocking) 00111 { 00112 tmr.start(); 00113 while (time < _timeout + 20) 00114 { 00115 if(_wizfi250->readable(_cid)) 00116 { 00117 break; 00118 } 00119 time = tmr.read_ms(); 00120 } 00121 if (time >= _timeout + 20) 00122 { 00123 return -1; 00124 } 00125 } 00126 else 00127 { 00128 if(!_wizfi250->readable(_cid)) 00129 { 00130 return 0; 00131 } 00132 } 00133 00134 00135 tmr.reset(); 00136 time = -1; 00137 tmr.start(); 00138 00139 while (time < _timeout) 00140 { 00141 if(_server) 00142 { 00143 idx += _wizfi250->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); 00144 } 00145 else 00146 { 00147 //idx += _wizfi250->recv(_cid, &buffer[idx], length - idx); 00148 idx += _wizfi250->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); 00149 } 00150 00151 if (idx == length) 00152 break; 00153 00154 00155 time = tmr.read_ms(); 00156 } 00157 00158 remote.set_address(ip, port); 00159 return (idx==0) ? -1 : idx; 00160 }
Generated on Sat Jul 16 2022 22:06:24 by 1.7.2