WIZ820io(W5200) network interface, EthernetInterface compatible.

Dependents:   Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

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 
00021 TCPSocketConnection::TCPSocketConnection()
00022 {
00023 }
00024 
00025 int TCPSocketConnection::connect(const char* host, const int port)
00026 {  
00027     if (_sock_fd < 0) {
00028         _sock_fd = eth->new_socket();
00029         if (_sock_fd < 0) {
00030             return -1;
00031         }
00032     }
00033     if (set_address(host, port) != 0) {
00034         return -1;
00035     }    
00036     if (!eth->connect(_sock_fd, get_address(), port)) {
00037         return -1;
00038     }
00039     return 0;
00040 }
00041 
00042 bool TCPSocketConnection::is_connected(void)
00043 {
00044     return eth->is_connected(_sock_fd);
00045 }
00046 
00047 int TCPSocketConnection::send(char* data, int length)
00048 {
00049     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
00050     if (size < 0) {
00051         return -1;
00052     }
00053     if (size > length) {
00054         size = length;
00055     }
00056     return eth->send(_sock_fd, data, size);
00057 }
00058 
00059 // -1 if unsuccessful, else number of bytes written
00060 int TCPSocketConnection::send_all(char* data, int length)
00061 {
00062     int writtenLen = 0;
00063     while (writtenLen < length) {
00064         int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
00065         if (size < 0) {
00066             return -1;
00067         }
00068         if (size > (length-writtenLen)) {
00069             size = (length-writtenLen);
00070         }
00071         int ret = eth->send(_sock_fd, data + writtenLen, size);
00072         if (ret < 0) {
00073             return -1;
00074         }
00075         writtenLen += ret;
00076     }
00077     return writtenLen;
00078 }
00079 
00080 // -1 if unsuccessful, else number of bytes received
00081 int TCPSocketConnection::receive(char* data, int length)
00082 {
00083     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
00084     if (size < 0) {
00085         return -1;
00086     }
00087     if (size > length) {
00088         size = length;
00089     }
00090     return eth->recv(_sock_fd, data, size);
00091 }
00092 
00093 // -1 if unsuccessful, else number of bytes received
00094 int TCPSocketConnection::receive_all(char* data, int length)
00095 {
00096     int readLen = 0;
00097     while (readLen < length) {
00098         int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
00099         if (size <= 0) {
00100             break;
00101         }
00102         if (size > (length - readLen)) {
00103             size = length - readLen;
00104         }
00105         int ret = eth->recv(_sock_fd, data + readLen, size);
00106         if (ret < 0) {
00107             return -1;
00108         }
00109         readLen += ret;
00110     }
00111     return readLen;
00112 }