Marcelo Rebonatto / Socket

Dependents:   EthernetInterface

Fork of Socket 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 #include "TCPSocketConnection.h"
00019 #include <cstring>
00020 
00021 #include "stdio.h"
00022 
00023 using std::memset;
00024 using std::memcpy;
00025 
00026 TCPSocketConnection::TCPSocketConnection() :
00027         _is_connected(false) {
00028 }
00029 
00030 int TCPSocketConnection::connect(const char* host, const int port) {
00031     if (init_socket(SOCK_STREAM) < 0){
00032         printf("Erro SOCK_STREAM\n");
00033         return -1;
00034     }
00035     
00036     if (set_address(host, port) != 0){
00037         printf("Erro set_address\n");
00038         return -1;
00039     }
00040     
00041     if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
00042         //printf("Erro lwip_connect\n");
00043         close();
00044         return -1;
00045     }
00046     _is_connected = true;
00047     
00048     return 0;
00049 }
00050 
00051 bool TCPSocketConnection::is_connected(void) {
00052     return _is_connected;
00053 }
00054 
00055 int TCPSocketConnection::send(char* data, int length) {
00056     if ((_sock_fd < 0) || !_is_connected)
00057         return -1;
00058     
00059     if (!_blocking) {
00060         TimeInterval timeout(_timeout);
00061         if (wait_writable(timeout) != 0)
00062             return -1;
00063     }
00064     
00065     int n = lwip_send(_sock_fd, data, length, 0);
00066     _is_connected = (n != 0);
00067     
00068     return n;
00069 }
00070 
00071 // -1 if unsuccessful, else number of bytes written
00072 int TCPSocketConnection::send_all(char* data, int length) {
00073     if ((_sock_fd < 0) || !_is_connected)
00074         return -1;
00075     
00076     int writtenLen = 0;
00077     TimeInterval timeout(_timeout);
00078     while (writtenLen < length) {
00079         if (!_blocking) {
00080             // Wait for socket to be writeable
00081             if (wait_writable(timeout) != 0)
00082                 return writtenLen;
00083         }
00084         
00085         int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
00086         if (ret > 0) {
00087             writtenLen += ret;
00088             continue;
00089         } else if (ret == 0) {
00090             _is_connected = false;
00091             return writtenLen;
00092         } else {
00093             return -1; //Connnection error
00094         }
00095     }
00096     return writtenLen;
00097 }
00098 
00099 int TCPSocketConnection::receive(char* data, int length) {
00100     if ((_sock_fd < 0) || !_is_connected)
00101         return -1;
00102     
00103     if (!_blocking) {
00104         TimeInterval timeout(_timeout);
00105         if (wait_readable(timeout) != 0)
00106             return -1;
00107     }
00108     
00109     int n = lwip_recv(_sock_fd, data, length, 0);
00110     _is_connected = (n != 0);
00111     
00112     return n;
00113 }
00114 
00115 // -1 if unsuccessful, else number of bytes received
00116 int TCPSocketConnection::receive_all(char* data, int length) {
00117     if ((_sock_fd < 0) || !_is_connected)
00118         return -1;
00119     
00120     int readLen = 0;
00121     TimeInterval timeout(_timeout);
00122     while (readLen < length) {
00123         if (!_blocking) {
00124             //Wait for socket to be readable
00125             if (wait_readable(timeout) != 0)
00126                 return readLen;
00127         }
00128         
00129         int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
00130         if (ret > 0) {
00131             readLen += ret;
00132         } else if (ret == 0) {
00133             _is_connected = false;
00134             return readLen;
00135         } else {
00136             return -1; //Connnection error
00137         }
00138     }
00139     return readLen;
00140 }