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.

Files at this revision

API Documentation at this revision

Comitter:
emilmont
Date:
Fri Jul 27 13:58:53 2012 +0000
Parent:
5:300e7ad2dc1d
Child:
7:ac5f77f4497a
Commit message:
thread safe timeouts

Changed in this revision

Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/Endpoint.cpp	Thu Jul 26 15:07:32 2012 +0000
+++ b/Endpoint.cpp	Fri Jul 27 13:58:53 2012 +0000
@@ -21,7 +21,9 @@
 
 using std::memset;
 
-Endpoint::Endpoint()  {}
+Endpoint::Endpoint()  {
+    reset_address();
+}
 Endpoint::~Endpoint() {}
 
 void Endpoint::reset_address(void) {
--- a/Socket.cpp	Thu Jul 26 15:07:32 2012 +0000
+++ b/Socket.cpp	Fri Jul 27 13:58:53 2012 +0000
@@ -36,29 +36,27 @@
     return 0;
 }
 
-
-void Socket::set_timeout(int timeout) {
-    _timeout.tv_sec = timeout / 1000;
-    _timeout.tv_usec = (timeout - (_timeout.tv_sec * 1000)) * 1000;
-}
-
-int Socket::select(fd_set* readset, fd_set* writeset) {
-    if ((_timeout.tv_sec == 0) && (_timeout.tv_usec == 0))
+int Socket::select(struct timeval *timeout, bool read, bool write) {
+    if ((timeout->tv_sec == 0) && (timeout->tv_usec == 0))
         return 0;
     
-    FD_ZERO(&_fdSet);
-    FD_SET(_sock_fd, &_fdSet);
+    fd_set fdSet;
+    FD_ZERO(&fdSet);
+    FD_SET(_sock_fd, &fdSet);
     
-    int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, &_timeout);
-    return (ret <= 0 || !FD_ISSET(_sock_fd, &_fdSet)) ? (-1) : (0);
+    fd_set* readset  = (read ) ? (&fdSet) : (NULL);
+    fd_set* writeset = (write) ? (&fdSet) : (NULL);
+    
+    int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
+    return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
 }
 
-int Socket::wait_readable(void) {
-    return select(&_fdSet, NULL);
+int Socket::wait_readable(TimeInterval& timeout) {
+    return select(&timeout._time, true, false);
 }
 
-int Socket::wait_writable(void) {
-    return select(NULL, &_fdSet);
+int Socket::wait_writable(TimeInterval& timeout) {
+    return select(&timeout._time, false, true);
 }
 
 int Socket::close() {
@@ -70,3 +68,8 @@
     
     return 0;
 }
+
+TimeInterval::TimeInterval(int ms) {
+    _time.tv_sec = ms / 1000;
+    _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
+}
--- a/Socket.h	Thu Jul 26 15:07:32 2012 +0000
+++ b/Socket.h	Fri Jul 27 13:58:53 2012 +0000
@@ -30,6 +30,8 @@
   return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
 }
 
+class TimeInterval;
+
 class Socket {
 public:
     Socket();
@@ -40,15 +42,21 @@
     int _sock_fd;
     int init_socket(int type);
     
-    void set_timeout(int timeout);
-    int wait_readable(void);
-    int wait_writable(void);
+    int wait_readable(TimeInterval& timeout);
+    int wait_writable(TimeInterval& timeout);
 
 private:
-    // At the moment, we assume a simple single threaded access to the socket
-    struct timeval _timeout;
-    fd_set _fdSet;
-    int select(fd_set* readset, fd_set* writeset);
+    int select(struct timeval *timeout, bool read, bool write);
+};
+
+class TimeInterval {
+    friend class Socket;
+
+public:
+    TimeInterval(int ms);
+    
+private:
+    struct timeval _time;
 };
 
 #endif /* SOCKET_H_ */
--- a/TCPSocketConnection.cpp	Thu Jul 26 15:07:32 2012 +0000
+++ b/TCPSocketConnection.cpp	Fri Jul 27 13:58:53 2012 +0000
@@ -40,12 +40,12 @@
     return 0;
 }
 
-int TCPSocketConnection::send(char* data, int length, int timeout) {
+int TCPSocketConnection::send(char* data, int length, int timeout_ms) {
     if ((_sock_fd < 0) || _closedByRemoteHost)
         return -1;
     
-    set_timeout(timeout);
-    if (wait_writable() != 0)
+    TimeInterval timeout(timeout_ms);
+    if (wait_writable(timeout) != 0)
         return -1;
     
     int n = lwip_send(_sock_fd, data, length, 0);
@@ -55,15 +55,15 @@
 }
 
 // -1 if unsuccessful, else number of bytes written
-int TCPSocketConnection::send_all(char* data, int length, int timeout) {
+int TCPSocketConnection::send_all(char* data, int length, int timeout_ms) {
     if ((_sock_fd < 0) || _closedByRemoteHost)
         return -1;
     
     size_t writtenLen = 0;
-    set_timeout(timeout);
+    TimeInterval timeout(timeout_ms);
     while (writtenLen < length) {
         // Wait for socket to be writeable
-        if (wait_writable() != 0)
+        if (wait_writable(timeout) != 0)
             return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
         
         int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
@@ -81,12 +81,12 @@
     return writtenLen;
 }
 
-int TCPSocketConnection::receive(char* data, int length, int timeout) {
+int TCPSocketConnection::receive(char* data, int length, int timeout_ms) {
     if ((_sock_fd < 0) || _closedByRemoteHost)
         return -1;
     
-    set_timeout(timeout);
-    if (wait_readable() != 0)
+    TimeInterval timeout(timeout_ms);
+    if (wait_readable(timeout) != 0)
         return -1;
     
     int n = lwip_recv(_sock_fd, data, length, 0);
@@ -96,15 +96,15 @@
 }
 
 // -1 if unsuccessful, else number of bytes received
-int TCPSocketConnection::receive_all(char* data, int length, int timeout) {
+int TCPSocketConnection::receive_all(char* data, int length, int timeout_ms) {
     if ((_sock_fd < 0) || _closedByRemoteHost)
         return -1;
     
     size_t readLen = 0;
-    set_timeout(timeout);
+    TimeInterval timeout(timeout_ms);
     while (readLen < length) {
         //Wait for socket to be readable
-        if (wait_readable() != 0)
+        if (wait_readable(timeout) != 0)
             return readLen; //Timeout -- FIXME should we return -1 or writtenLength ?
         
         int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
--- a/TCPSocketConnection.h	Thu Jul 26 15:07:32 2012 +0000
+++ b/TCPSocketConnection.h	Fri Jul 27 13:58:53 2012 +0000
@@ -48,7 +48,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to send the buffer.
     \return the number of written bytes on success (>=0) or -1 on failure
      */
-    int send(char* data, int length, int timeout=0);
+    int send(char* data, int length, int timeout_ms=0);
     
     /** Send data to the remote host.
     \param data The buffer to send to the host.
@@ -56,7 +56,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to send the buffer.
     \return the number of written bytes on success (>=0) or -1 on failure
     */
-    int send_all(char* data, int length, int timeout=0);
+    int send_all(char* data, int length, int timeout_ms=0);
     
     /** Receive data from the remote host.
     \param data The buffer in which to store the data received from the host.
@@ -64,7 +64,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to receive data.
     \return the number of received bytes on success (>=0) or -1 on failure
      */
-    int receive(char* data, int length, int timeout=0);
+    int receive(char* data, int length, int timeout_ms=0);
     
     /** Receive data from the remote host.
     \param data The buffer in which to store the data received from the host.
@@ -72,7 +72,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to receive data.
     \return the number of received bytes on success (>=0) or -1 on failure
     */
-    int receive_all(char* data, int length, int timeout=0);
+    int receive_all(char* data, int length, int timeout_ms=0);
 
 private:
     bool _closedByRemoteHost;
--- a/TCPSocketServer.cpp	Thu Jul 26 15:07:32 2012 +0000
+++ b/TCPSocketServer.cpp	Fri Jul 27 13:58:53 2012 +0000
@@ -57,12 +57,12 @@
     return 0;
 }
 
-int TCPSocketServer::accept(TCPSocketConnection& connection, int timeout) {
+int TCPSocketServer::accept(TCPSocketConnection& connection, int timeout_ms) {
     if (_sock_fd < 0)
         return -1;
     
-    set_timeout(timeout);
-    if (wait_readable() != 0)
+    TimeInterval timeout(timeout_ms);
+    if (wait_readable(timeout) != 0)
         return -1;
     
     socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost);
--- a/TCPSocketServer.h	Thu Jul 26 15:07:32 2012 +0000
+++ b/TCPSocketServer.h	Fri Jul 27 13:58:53 2012 +0000
@@ -46,7 +46,7 @@
     \param socket A socket instance that will handle the incoming connection.
     \return 0 on success, -1 on failure.
     */
-    int accept(TCPSocketConnection& connection, int timeout=0);
+    int accept(TCPSocketConnection& connection, int timeout_ms=0);
 };
 
 #endif
--- a/UDPSocket.cpp	Thu Jul 26 15:07:32 2012 +0000
+++ b/UDPSocket.cpp	Fri Jul 27 13:58:53 2012 +0000
@@ -50,13 +50,13 @@
 }
 
 // -1 if unsuccessful, else number of bytes written
-int UDPSocket::sendTo(UDPPacket& packet, int timeout) {
+int UDPSocket::sendTo(UDPPacket& packet, int timeout_ms) {
     if (_sock_fd < 0)
         return -1;
     
-    if (timeout != 0) {
-        set_timeout(timeout);
-        if (wait_writable() != 0)
+    if (timeout_ms != 0) {
+        TimeInterval timeout(timeout_ms);
+        if (wait_writable(timeout) != 0)
             return -1;
     }
     
@@ -64,13 +64,13 @@
 }
 
 // -1 if unsuccessful, else number of bytes received
-int UDPSocket::receiveFrom(UDPPacket& packet, int timeout) {
+int UDPSocket::receiveFrom(UDPPacket& packet, int timeout_ms) {
     if (_sock_fd < 0)
         return -1;
     
-    if (timeout != 0) {
-        set_timeout(timeout);
-        if (wait_readable() != 0)
+    if (timeout_ms != 0) {
+        TimeInterval timeout(timeout_ms);
+        if (wait_readable(timeout) != 0)
             return -1;
     }
     
--- a/UDPSocket.h	Thu Jul 26 15:07:32 2012 +0000
+++ b/UDPSocket.h	Fri Jul 27 13:58:53 2012 +0000
@@ -49,7 +49,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to send the buffer.
     \return the number of written bytes on success (>=0) or -1 on failure
     */
-    int sendTo(UDPPacket& packet, int timeout=0);
+    int sendTo(UDPPacket& packet, int timeout_ms=0);
     
     /** Receive data from a remote host.
     If a message is too long to fit in the supplied packet, excess bytes are
@@ -58,7 +58,7 @@
     \param timeout The maximum amount of time in ms to wait while trying to receive data.
     \return the number of received bytes on success (>=0) or -1 on failure
     */
-    int receiveFrom(UDPPacket& packet, int timeout=0);
+    int receiveFrom(UDPPacket& packet, int timeout_ms=0);
 };
 
 #endif