Added RAW sockets.
Fork of WIZnetInterface 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() : 00028 _is_connected(false) 00029 { 00030 } 00031 00032 int TCPSocketConnection::connect(const char* host, const int port) 00033 { 00034 if (_sock_fd < 0) { 00035 _sock_fd = eth->new_socket(); 00036 if (_sock_fd < 0) { 00037 return -1; 00038 } 00039 } 00040 if (set_address(host, port) != 0) { 00041 return -1; 00042 } 00043 if (!eth->connect(_sock_fd, get_address(), port)) { 00044 return -1; 00045 } 00046 set_blocking(false); 00047 // add code refer from EthernetInterface. 00048 _is_connected = true; 00049 00050 return 0; 00051 } 00052 00053 00054 // Geminate Changes 00055 00056 int TCPSocketConnection::get_DHAR0(void) 00057 { 00058 return( eth->get_DHAR0(_sock_fd)); 00059 } 00060 00061 00062 int TCPSocketConnection::get_DHAR1(void) 00063 { 00064 return( eth->get_DHAR1(_sock_fd)); 00065 } 00066 00067 // End Changes 00068 00069 00070 bool TCPSocketConnection::is_connected(void) 00071 { 00072 // force update recent state. 00073 _is_connected = eth->is_connected(_sock_fd); 00074 return _is_connected; 00075 } 00076 00077 int TCPSocketConnection::send(char* data, int length) 00078 { 00079 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00080 return -1; 00081 00082 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00083 if (size < 0) 00084 return -1; 00085 00086 if (size > length) 00087 size = length; 00088 00089 return eth->send(_sock_fd, data, size); 00090 } 00091 00092 // -1 if unsuccessful, else number of bytes written 00093 int TCPSocketConnection::send_all(char* data, int length) 00094 { 00095 int writtenLen = 0; 00096 00097 if(_sock_fd<0) 00098 return -1; 00099 00100 while (writtenLen < length) { 00101 00102 if(!(eth->is_connected(_sock_fd))) 00103 return -1; 00104 00105 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); 00106 if (size < 0) { 00107 return -1; 00108 } 00109 if (size > (length-writtenLen)) { 00110 size = (length-writtenLen); 00111 } 00112 int ret = eth->send(_sock_fd, data + writtenLen, size); 00113 if (ret < 0) { 00114 return -1; 00115 } 00116 writtenLen += ret; 00117 } 00118 return writtenLen; 00119 } 00120 00121 // -1 if unsuccessful, else number of bytes received 00122 int TCPSocketConnection::receive(char* data, int length) 00123 { 00124 if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) 00125 return -1; 00126 00127 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); 00128 if (size < 0) { 00129 return -1; 00130 } 00131 if (size > length) { 00132 size = length; 00133 } 00134 return eth->recv(_sock_fd, data, size); 00135 } 00136 00137 // -1 if unsuccessful, else number of bytes received 00138 int TCPSocketConnection::receive_all(char* data, int length) 00139 { 00140 if(_sock_fd<0) 00141 return -1; 00142 00143 int readLen = 0; 00144 while (readLen < length) { 00145 00146 if(!(eth->is_connected(_sock_fd))) 00147 return -1; 00148 00149 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); 00150 if (size <= 0) { 00151 break; 00152 } 00153 if (size > (length - readLen)) { 00154 size = length - readLen; 00155 } 00156 int ret = eth->recv(_sock_fd, data + readLen, size); 00157 if (ret < 0) { 00158 return -1; 00159 } 00160 readLen += ret; 00161 } 00162 return readLen; 00163 }
Generated on Wed Jul 13 2022 08:52:50 by
1.7.2
