Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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.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() : 00028 _is_connected(false) 00029 { 00030 } 00031 00032 int TCPSocketConnection::connect(const char* host, const int port) 00033 { 00034 Serial pc5(PA_13,PA_14); 00035 00036 pc5.printf("in connect\r\n"); 00037 if (_sock_fd < 0) { 00038 pc5.printf("_sock_fd\r\n"); 00039 _sock_fd = eth->new_socket(); 00040 pc5.printf("_sock_fd2\r\n"); 00041 if (_sock_fd < 0) { 00042 pc5.printf("in connect 1 return -1\r\n"); 00043 return -1; 00044 } 00045 } 00046 pc5.printf("in2 connect\r\n"); 00047 00048 if (set_address(host, port) != 0) { 00049 pc5.printf("in connect 2 return -1\r\n"); 00050 return -1; 00051 } 00052 00053 pc5.printf("in3 connect\r\n"); 00054 if (!eth->connect(_sock_fd, get_address(), port)) {//should jion the timeout 00055 pc5.printf("in connect 3 return -1\r\n"); 00056 return -1; 00057 } 00058 pc5.printf("in4 connect\r\n"); 00059 set_blocking(false); 00060 // add code refer from EthernetInterface. 00061 _is_connected = true; 00062 pc5.printf("_is_connected=true\r\n"); 00063 return 0; 00064 } 00065 00066 bool TCPSocketConnection::is_connected(void) 00067 { 00068 Serial pc2(PA_13,PA_14); 00069 00070 pc2.printf("in is_connected()\r\n"); 00071 // force update recent state. 00072 _is_connected = eth->is_connected(_sock_fd); 00073 return _is_connected; 00074 } 00075 00076 int TCPSocketConnection::send(char* data, int length) 00077 { 00078 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00079 return -1; 00080 00081 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00082 if (size < 0) 00083 return -1; 00084 00085 if (size > length) 00086 size = length; 00087 00088 return eth->send(_sock_fd, data, size); 00089 } 00090 00091 // -1 if unsuccessful, else number of bytes written 00092 int TCPSocketConnection::send_all(char* data, int length) 00093 { 00094 int writtenLen = 0; 00095 00096 if(_sock_fd<0) 00097 return -1; 00098 00099 while (writtenLen < length) { 00100 00101 if(!(eth->is_connected(_sock_fd))) 00102 return -1; 00103 00104 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00105 if (size < 0) { 00106 return -1; 00107 } 00108 if (size > (length-writtenLen)) { 00109 size = (length-writtenLen); 00110 } 00111 int ret = eth->send(_sock_fd, data + writtenLen, size); 00112 if (ret < 0) { 00113 return -1; 00114 } 00115 writtenLen += ret; 00116 } 00117 return writtenLen; 00118 } 00119 00120 // -1 if unsuccessful, else number of bytes received 00121 int TCPSocketConnection::receive(char* data, int length) 00122 { 00123 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00124 return -1; 00125 00126 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); 00127 if (size < 0) { 00128 return -1; 00129 } 00130 if (size > length) { 00131 size = length; 00132 } 00133 return eth->recv(_sock_fd, data, size); 00134 } 00135 00136 // -1 if unsuccessful, else number of bytes received 00137 int TCPSocketConnection::receive_all(char* data, int length) 00138 { 00139 if(_sock_fd<0) 00140 return -1; 00141 00142 int readLen = 0; 00143 while (readLen < length) { 00144 00145 if(!(eth->is_connected(_sock_fd))) 00146 return -1; 00147 00148 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); 00149 if (size <= 0) { 00150 break; 00151 } 00152 if (size > (length - readLen)) { 00153 size = length - readLen; 00154 } 00155 int ret = eth->recv(_sock_fd, data + readLen, size); 00156 if (ret < 0) { 00157 return -1; 00158 } 00159 readLen += ret; 00160 } 00161 return readLen; 00162 }
Generated on Tue Jul 12 2022 18:35:42 by
1.7.2
