Base class for IP Based Networking Libraries

Dependencies:   DnsQuery

Dependents:   TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more

For a complete getting started guide see the wiki...

Network Socket API

The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.

Network Interfaces

The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface and WiFiInterface.

Sockets

The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket or a UDPSocket which defines the protocol used for the connection.

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Wed Apr 20 19:05:03 2016 -0500
Parent:
114:964eba6394bc
Child:
116:bc043343d753
Commit message:
Consolidate set_timeout/set_blocking behaviour to avoid ambiguity when both are used and match python behaviour

Changed in this revision

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
TCPServer.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocket.cpp 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
--- a/Socket.cpp	Wed Apr 20 11:07:19 2016 -0500
+++ b/Socket.cpp	Wed Apr 20 19:05:03 2016 -0500
@@ -19,8 +19,7 @@
 Socket::Socket()
     : _iface(0)
     , _socket(0)
-    , _blocking(true)
-    , _timeout(0)
+    , _timeout(-1)
 {
 }
 
@@ -83,10 +82,10 @@
 
 void Socket::set_blocking(bool blocking)
 {
-    _blocking = blocking;
+    set_timeout(blocking ? -1 : 0);
 }
 
-void Socket::set_timeout(unsigned timeout)
+void Socket::set_timeout(int timeout)
 {
     _timeout = timeout;
 }
--- a/Socket.h	Wed Apr 20 11:07:19 2016 -0500
+++ b/Socket.h	Wed Apr 20 19:05:03 2016 -0500
@@ -86,7 +86,10 @@
      *  blocking operations such as send/recv/accept return
      *  NSAPI_ERROR_WOULD_BLOCK if they can not continue.
      *
-     *  @param blocking True for blocking mode, false for non-blocking mode.
+     *  set_blocking(false) is equivalent to set_timeout(-1)
+     *  set_blocking(true) is equivalent to set_timeout(0)
+     *
+     *  @param blocking true for blocking mode, false for non-blocking mode.
      */
     void set_blocking(bool blocking);
     
@@ -94,11 +97,14 @@
      *
      *  Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
      *  is returned if a blocking operation takes longer than the specified
-     *  timeout. A timeout of 0 removes a timeout from the socket.
+     *  timeout. A timeout of -1 removes the timeout from the socket.
+     *
+     *  set_timeout(-1) is equivalent to set_blocking(false)
+     *  set_timeout(0) is equivalent to set_blocking(true)
      *
      *  @param timeout  Timeout in milliseconds
      */
-    void set_timeout(unsigned int timeout);
+    void set_timeout(int timeout);
 
     /*  Set stack-specific socket options
      *
@@ -166,8 +172,7 @@
 
     NetworkStack *_iface;
     void *_socket;
-    bool _blocking;
-    unsigned _timeout;
+    int _timeout;
     FunctionPointer _callback;
 };
 
--- a/TCPServer.cpp	Wed Apr 20 11:07:19 2016 -0500
+++ b/TCPServer.cpp	Wed Apr 20 19:05:03 2016 -0500
@@ -60,8 +60,9 @@
             connection->_socket = socket;
         }
 
-        if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
-            (_timeout && timer.read_ms() > _timeout)) {
+        if (err != NSAPI_ERROR_WOULD_BLOCK
+            || _timeout < 0
+            || timer.read_ms() > _timeout) {
             return err;
         }
     }
--- a/TCPSocket.cpp	Wed Apr 20 11:07:19 2016 -0500
+++ b/TCPSocket.cpp	Wed Apr 20 19:05:03 2016 -0500
@@ -61,8 +61,9 @@
         }
 
         int sent = _iface->socket_send(_socket, data, size);
-        if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
-            (_timeout && timer.read_ms() > _timeout)) {
+        if (sent != NSAPI_ERROR_WOULD_BLOCK
+            || _timeout < 0
+            || timer.read_ms() > _timeout) {
             return sent;
         }
     }
@@ -79,8 +80,9 @@
         }
     
         int recv = _iface->socket_recv(_socket, data, size);
-        if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
-            (_timeout && timer.read_ms() > _timeout)) {
+        if (recv != NSAPI_ERROR_WOULD_BLOCK
+            || _timeout < 0
+            || timer.read_ms() > _timeout) {
             return recv;
         }
     }
--- a/UDPSocket.cpp	Wed Apr 20 11:07:19 2016 -0500
+++ b/UDPSocket.cpp	Wed Apr 20 19:05:03 2016 -0500
@@ -52,8 +52,9 @@
         }
     
         int sent = _iface->socket_sendto(_socket, address, data, size);
-        if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
-            (_timeout && timer.read_ms() > _timeout)) {
+        if (sent != NSAPI_ERROR_WOULD_BLOCK
+            || _timeout < 0
+            || timer.read_ms() > _timeout) {
             return sent;
         }
     }
@@ -70,8 +71,9 @@
         }
     
         int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
-        if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
-            (_timeout && timer.read_ms() > _timeout)) {
+        if (recv != NSAPI_ERROR_WOULD_BLOCK
+            || _timeout < 0
+            || timer.read_ms() > _timeout) {
             return recv;
         }
     }