Implementation of the WifiPlusClick hardware module.
Dependents: WifiPlusKlickExample
TCPSocketConnection.cpp
00001 /* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) 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 #include "mbed.h" 00019 #include "TCPSocketConnection.h" 00020 00021 TCPSocketConnection::TCPSocketConnection() 00022 : _is_connected(false) 00023 { 00024 } 00025 00026 int TCPSocketConnection::connect(const char *host, const int port) 00027 { 00028 _socket = Wifi::getInstance()->SocketCreate(TCP); 00029 if (_socket == InvalidSocketHandle) 00030 return -1; 00031 00032 if (set_address(host, port) != 0) 00033 return -1; 00034 00035 if (!Wifi::getInstance()->SocketConnect(_socket, reinterpret_cast<IPADDRESS_t*>(get_address()), port)) { 00036 close(); 00037 return -1; 00038 } 00039 _is_connected = true; 00040 00041 return 0; 00042 } 00043 00044 bool TCPSocketConnection::is_connected() 00045 { 00046 return _is_connected; 00047 } 00048 00049 int TCPSocketConnection::send(char *data, int length) 00050 { 00051 if ((_socket == InvalidSocketHandle) || !_is_connected) 00052 return -1; 00053 00054 int n = Wifi::getInstance()->SocketSend(_socket, data, length); 00055 00056 return n; 00057 } 00058 00059 int TCPSocketConnection::send_all(char *data, int length) 00060 { 00061 if ((_socket == InvalidSocketHandle) || !_is_connected) 00062 return -1; 00063 00064 int n = 0; 00065 00066 Timer t; 00067 t.start(); 00068 00069 while( n < length) { 00070 int r = Wifi::getInstance()->SocketSend(_socket, &data[n], length-n); 00071 if (r == -1) { 00072 return n; 00073 } 00074 n += r; 00075 00076 if (_blocking && (t.read_ms() > _timeout)) { 00077 break; 00078 } 00079 } 00080 00081 return n; 00082 } 00083 00084 int TCPSocketConnection::recv(char *data, int length) 00085 { 00086 if ((_socket == InvalidSocketHandle) || !_is_connected) 00087 return -1; 00088 00089 int n = Wifi::getInstance()->SocketRecv(_socket, data, length); 00090 00091 return n; 00092 } 00093 00094 int TCPSocketConnection::recv_all(char *data, int length) 00095 { 00096 if ((_socket == InvalidSocketHandle) || !_is_connected) 00097 return -1; 00098 00099 int n = 0; 00100 00101 Timer t; 00102 t.start(); 00103 00104 while( n < length) { 00105 int r = Wifi::getInstance()->SocketRecv(_socket, &data[n], length-n); 00106 if (r == -1) { 00107 return n; 00108 } 00109 n += r; 00110 00111 if (_blocking && (t.read_ms() > _timeout)) { 00112 break; 00113 } 00114 } 00115 00116 return n; 00117 }
Generated on Tue Jul 12 2022 23:18:35 by 1.7.2