Neal Horman / Socket

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 using std::memset;
00022 using std::memcpy;
00023 
00024 TCPSocketConnection::TCPSocketConnection() :
00025         _closedByRemoteHost(false) {
00026 }
00027 
00028 int TCPSocketConnection::connect(const char* host, const int port) {
00029     if (init_socket(SOCK_STREAM) < 0)
00030         return -1;
00031     
00032     if (set_address(host, port) != 0)
00033         return -1;
00034     
00035     if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
00036         close();
00037         return -1;
00038     }
00039     
00040     return 0;
00041 }
00042 
00043 int TCPSocketConnection::send(char* data, int length, int timeout_ms) {
00044     if ((_sock_fd < 0) || _closedByRemoteHost)
00045         return -1;
00046     
00047     TimeInterval timeout(timeout_ms);
00048     if (wait_writable(timeout) != 0)
00049         return -1;
00050     
00051     int n = lwip_send(_sock_fd, data, length, 0);
00052     _closedByRemoteHost = (n == 0);
00053     
00054     return n;
00055 }
00056 
00057 // -1 if unsuccessful, else number of bytes written
00058 int TCPSocketConnection::send_all(char* data, int length, int timeout_ms) {
00059     if ((_sock_fd < 0) || _closedByRemoteHost)
00060         return -1;
00061     
00062     size_t writtenLen = 0;
00063     TimeInterval timeout(timeout_ms);
00064     while (writtenLen < length) {
00065         // Wait for socket to be writeable
00066         if (wait_writable(timeout) != 0)
00067             return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
00068         
00069         int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
00070         if (ret > 0) {
00071             writtenLen += ret;
00072             continue;
00073         } else if (ret == 0) {
00074             _closedByRemoteHost = true;
00075             return writtenLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
00076         } else {
00077             return -1; //Connnection error
00078         }
00079     }
00080     
00081     return writtenLen;
00082 }
00083 
00084 int TCPSocketConnection::receive(char* data, int length, int timeout_ms) {
00085     if ((_sock_fd < 0) || _closedByRemoteHost)
00086         return -1;
00087     
00088     TimeInterval timeout(timeout_ms);
00089     if (wait_readable(timeout) != 0)
00090         return -1;
00091     
00092     int n = lwip_recv(_sock_fd, data, length, 0);
00093     _closedByRemoteHost = (n == 0);
00094     
00095     return n;
00096 }
00097 
00098 // -1 if unsuccessful, else number of bytes received
00099 int TCPSocketConnection::receive_all(char* data, int length, int timeout_ms) {
00100     if ((_sock_fd < 0) || _closedByRemoteHost)
00101         return -1;
00102     
00103     size_t readLen = 0;
00104     TimeInterval timeout(timeout_ms);
00105     while (readLen < length) {
00106         //Wait for socket to be readable
00107         if (wait_readable(timeout) != 0)
00108             return readLen; //Timeout -- FIXME should we return -1 or writtenLength ?
00109         
00110         int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
00111         if (ret > 0) {
00112             readLen += ret;
00113         } else if (ret == 0) {
00114             _closedByRemoteHost = true;
00115             return readLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
00116         } else {
00117             return -1; //Connnection error
00118         }
00119     }
00120     return readLen;
00121 }
00122 
00123 TCPSocketConnection::~TCPSocketConnection() {
00124     close(); //Don't want to leak 
00125 }