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