mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Deprecated

This is an mbed 2 sockets library. For mbed 5, network sockets have been revised to better support additional network stacks and thread safety here.

Revision:
11:3d83c348fb8b
Parent:
10:d24738f4ef99
Child:
19:434906b5b977
--- a/TCPSocketConnection.cpp	Tue Jul 31 11:50:55 2012 +0000
+++ b/TCPSocketConnection.cpp	Wed Aug 01 13:02:32 2012 +0000
@@ -22,7 +22,7 @@
 using std::memcpy;
 
 TCPSocketConnection::TCPSocketConnection() :
-        _closedByRemoteHost(false) {
+        _is_connected(false) {
 }
 
 int TCPSocketConnection::connect(const char* host, const int port) {
@@ -36,12 +36,17 @@
         close();
         return -1;
     }
+    _is_connected = true;
     
     return 0;
 }
 
+bool TCPSocketConnection::is_connected(void) {
+    return _is_connected;
+}
+
 int TCPSocketConnection::send(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     if (!_blocking) {
@@ -51,24 +56,23 @@
     }
     
     int n = lwip_send(_sock_fd, data, length, 0);
-    _closedByRemoteHost = (n == 0);
+    _is_connected = (n != 0);
     
     return n;
 }
 
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     size_t writtenLen = 0;
-    
     TimeInterval timeout(_timeout);
     while (writtenLen < length) {
         if (!_blocking) {
             // Wait for socket to be writeable
             if (wait_writable(timeout) != 0)
-                return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
+                return writtenLen;
         }
         
         int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
@@ -76,18 +80,17 @@
             writtenLen += ret;
             continue;
         } else if (ret == 0) {
-            _closedByRemoteHost = true;
-            return writtenLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
+            _is_connected = false;
+            return writtenLen;
         } else {
             return -1; //Connnection error
         }
     }
-    
     return writtenLen;
 }
 
 int TCPSocketConnection::receive(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     if (!_blocking) {
@@ -97,14 +100,14 @@
     }
     
     int n = lwip_recv(_sock_fd, data, length, 0);
-    _closedByRemoteHost = (n == 0);
+    _is_connected = (n != 0);
     
     return n;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive_all(char* data, int length) {
-    if ((_sock_fd < 0) || _closedByRemoteHost)
+    if ((_sock_fd < 0) || !_is_connected)
         return -1;
     
     size_t readLen = 0;
@@ -113,22 +116,18 @@
         if (!_blocking) {
             //Wait for socket to be readable
             if (wait_readable(timeout) != 0)
-                return readLen; //Timeout -- FIXME should we return -1 or writtenLength ?
+                return readLen;
         }
         
         int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
         if (ret > 0) {
             readLen += ret;
         } else if (ret == 0) {
-            _closedByRemoteHost = true;
-            return readLen; //Connection was closed by remote host -- FIXME how do we signal that the connection was closed ?
+            _is_connected = false;
+            return readLen;
         } else {
             return -1; //Connnection error
         }
     }
     return readLen;
 }
-
-TCPSocketConnection::~TCPSocketConnection() {
-    close(); //Don't want to leak 
-}