mbed socket API
Dependents: EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more
TCPSocketConnection.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 #include "TCPSocketConnection.h" 00019 #include <cstring> 00020 00021 using std::memset; 00022 using std::memcpy; 00023 00024 TCPSocketConnection::TCPSocketConnection() : 00025 _is_connected(false) { 00026 } 00027 00028 int TCPSocketConnection::connect(const char* host, const int port) { 00029 if (init_socket(SOCK_STREAM) < 0) 00030 return -1; 00031 00032 if (set_address(host, port) != 0) 00033 return -1; 00034 00035 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { 00036 close(); 00037 return -1; 00038 } 00039 _is_connected = true; 00040 00041 return 0; 00042 } 00043 00044 bool TCPSocketConnection::is_connected(void) { 00045 return _is_connected; 00046 } 00047 00048 int TCPSocketConnection::send(char* data, int length) { 00049 if ((_sock_fd < 0) || !_is_connected) 00050 return -1; 00051 00052 if (!_blocking) { 00053 TimeInterval timeout(_timeout); 00054 if (wait_writable(timeout) != 0) 00055 return -1; 00056 } 00057 00058 int n = lwip_send(_sock_fd, data, length, 0); 00059 _is_connected = (n != 0); 00060 00061 return n; 00062 } 00063 00064 // -1 if unsuccessful, else number of bytes written 00065 int TCPSocketConnection::send_all(char* data, int length) { 00066 if ((_sock_fd < 0) || !_is_connected) 00067 return -1; 00068 00069 int writtenLen = 0; 00070 TimeInterval timeout(_timeout); 00071 while (writtenLen < length) { 00072 if (!_blocking) { 00073 // Wait for socket to be writeable 00074 if (wait_writable(timeout) != 0) 00075 return writtenLen; 00076 } 00077 00078 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); 00079 if (ret > 0) { 00080 writtenLen += ret; 00081 continue; 00082 } else if (ret == 0) { 00083 _is_connected = false; 00084 return writtenLen; 00085 } else { 00086 return -1; //Connnection error 00087 } 00088 } 00089 return writtenLen; 00090 } 00091 00092 int TCPSocketConnection::receive(char* data, int length) { 00093 if ((_sock_fd < 0) || !_is_connected) 00094 return -1; 00095 00096 if (!_blocking) { 00097 TimeInterval timeout(_timeout); 00098 if (wait_readable(timeout) != 0) 00099 return -1; 00100 } 00101 00102 int n = lwip_recv(_sock_fd, data, length, 0); 00103 _is_connected = (n != 0); 00104 00105 return n; 00106 } 00107 00108 // -1 if unsuccessful, else number of bytes received 00109 int TCPSocketConnection::receive_all(char* data, int length) { 00110 if ((_sock_fd < 0) || !_is_connected) 00111 return -1; 00112 00113 int readLen = 0; 00114 TimeInterval timeout(_timeout); 00115 while (readLen < length) { 00116 if (!_blocking) { 00117 //Wait for socket to be readable 00118 if (wait_readable(timeout) != 0) 00119 return readLen; 00120 } 00121 00122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); 00123 if (ret > 0) { 00124 readLen += ret; 00125 } else if (ret == 0) { 00126 _is_connected = false; 00127 return readLen; 00128 } else { 00129 return -1; //Connnection error 00130 } 00131 } 00132 return readLen; 00133 }
Generated on Tue Jul 12 2022 11:05:10 by 1.7.2