SocketGUS
Fork of Socket by
Revision 20:f23c3d6c4bc8, committed 2015-12-05
- Comitter:
- RoHe
- Date:
- Sat Dec 05 17:41:50 2015 +0000
- Parent:
- 19:434906b5b977
- Commit message:
- MainTask GUS
Changed in this revision
TCPSocketConnection.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 434906b5b977 -r f23c3d6c4bc8 TCPSocketConnection.cpp --- a/TCPSocketConnection.cpp Mon Aug 19 18:38:18 2013 +0300 +++ b/TCPSocketConnection.cpp Sat Dec 05 17:41:50 2015 +0000 @@ -22,50 +22,55 @@ using std::memcpy; TCPSocketConnection::TCPSocketConnection() : - _is_connected(false) { + _is_connected(false) +{ } -int TCPSocketConnection::connect(const char* host, const int port) { +int TCPSocketConnection::connect(const char* host, const int port) +{ if (init_socket(SOCK_STREAM) < 0) return -1; - - if (set_address(host, port) != 0) + //quitar else + else if (set_address(host, port) != 0) return -1; - - if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { + //quitar else + else if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { close(); return -1; - } - _is_connected = true; - - return 0; + } else {//quitar else + _is_connected = true; + return 0; + }//quitar } -bool TCPSocketConnection::is_connected(void) { +bool TCPSocketConnection::is_connected(void) +{ return _is_connected; } -int TCPSocketConnection::send(char* data, int length) { +int TCPSocketConnection::send(char* data, int length) +{ if ((_sock_fd < 0) || !_is_connected) return -1; - + if (!_blocking) { TimeInterval timeout(_timeout); if (wait_writable(timeout) != 0) return -1; } - + int n = lwip_send(_sock_fd, data, length, 0); _is_connected = (n != 0); - + return n; } // -1 if unsuccessful, else number of bytes written -int TCPSocketConnection::send_all(char* data, int length) { +int TCPSocketConnection::send_all(char* data, int length) +{ if ((_sock_fd < 0) || !_is_connected) return -1; - + int writtenLen = 0; TimeInterval timeout(_timeout); while (writtenLen < length) { @@ -74,7 +79,7 @@ if (wait_writable(timeout) != 0) return writtenLen; } - + int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); if (ret > 0) { writtenLen += ret; @@ -89,27 +94,29 @@ return writtenLen; } -int TCPSocketConnection::receive(char* data, int length) { +int TCPSocketConnection::receive(char* data, int length) +{ if ((_sock_fd < 0) || !_is_connected) return -1; - + if (!_blocking) { TimeInterval timeout(_timeout); if (wait_readable(timeout) != 0) return -1; } - + int n = lwip_recv(_sock_fd, data, length, 0); _is_connected = (n != 0); - + return n; } // -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length) { +int TCPSocketConnection::receive_all(char* data, int length) +{ if ((_sock_fd < 0) || !_is_connected) return -1; - + int readLen = 0; TimeInterval timeout(_timeout); while (readLen < length) { @@ -118,7 +125,7 @@ if (wait_readable(timeout) != 0) return readLen; } - + int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); if (ret > 0) { readLen += ret;