WIZnetInterface using namespace
Dependents: DualNetworkInterface-Basic
Fork of WIZnetInterface by
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 00019 #include "TCPSocketConnection.hpp" 00020 #include <cstring> 00021 00022 using std::memset; 00023 using std::memcpy; 00024 00025 using namespace wiznet_space; 00026 00027 // not a big code. 00028 // refer from EthernetInterface by mbed official driver 00029 TCPSocketConnection::TCPSocketConnection() : 00030 _is_connected(false) 00031 { 00032 } 00033 00034 int TCPSocketConnection::connect(const char* host, const int port) 00035 { 00036 if (_sock_fd < 0) { 00037 _sock_fd = eth->new_socket(); 00038 if (_sock_fd < 0) { 00039 return -1; 00040 } 00041 } 00042 if (set_address(host, port) != 0) { 00043 return -1; 00044 } 00045 if (!eth->connect(_sock_fd, get_address(), port)) { 00046 return -1; 00047 } 00048 set_blocking(false); 00049 // add code refer from EthernetInterface. 00050 _is_connected = true; 00051 00052 return 0; 00053 } 00054 00055 bool TCPSocketConnection::is_connected(void) 00056 { 00057 // force update recent state. 00058 _is_connected = eth->is_connected(_sock_fd); 00059 return _is_connected; 00060 } 00061 00062 int TCPSocketConnection::send(char* data, int length) 00063 { 00064 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00065 return -1; 00066 00067 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00068 if (size < 0) 00069 return -1; 00070 00071 if (size > length) 00072 size = length; 00073 00074 return eth->send(_sock_fd, data, size); 00075 } 00076 00077 // -1 if unsuccessful, else number of bytes written 00078 int TCPSocketConnection::send_all(char* data, int length) 00079 { 00080 int writtenLen = 0; 00081 00082 if(_sock_fd<0) 00083 return -1; 00084 00085 while (writtenLen < length) { 00086 00087 if(!(eth->is_connected(_sock_fd))) 00088 return -1; 00089 00090 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00091 if (size < 0) { 00092 return -1; 00093 } 00094 if (size > (length-writtenLen)) { 00095 size = (length-writtenLen); 00096 } 00097 int ret = eth->send(_sock_fd, data + writtenLen, size); 00098 if (ret < 0) { 00099 return -1; 00100 } 00101 writtenLen += ret; 00102 } 00103 return writtenLen; 00104 } 00105 00106 // -1 if unsuccessful, else number of bytes received 00107 int TCPSocketConnection::receive(char* data, int length) 00108 { 00109 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00110 return -1; 00111 00112 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); 00113 if (size < 0) { 00114 return -1; 00115 } 00116 if (size > length) { 00117 size = length; 00118 } 00119 return eth->recv(_sock_fd, data, size); 00120 } 00121 00122 // -1 if unsuccessful, else number of bytes received 00123 int TCPSocketConnection::receive_all(char* data, int length) 00124 { 00125 if(_sock_fd<0) 00126 return -1; 00127 00128 int readLen = 0; 00129 while (readLen < length) { 00130 00131 if(!(eth->is_connected(_sock_fd))) 00132 return -1; 00133 00134 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); 00135 if (size <= 0) { 00136 break; 00137 } 00138 if (size > (length - readLen)) { 00139 size = length - readLen; 00140 } 00141 int ret = eth->recv(_sock_fd, data + readLen, size); 00142 if (ret < 0) { 00143 return -1; 00144 } 00145 readLen += ret; 00146 } 00147 return readLen; 00148 }
Generated on Wed Jul 13 2022 05:21:37 by 1.7.2