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:
Tue Apr 19 18:22:15 2016 -0500
Parent:
89:b1d417383c0d
Child:
91:cad29ce6a01c
Commit message:
Add open call as alternative to passing NetworkInterface at construction

Pros
- Allows memory to be statically allocated
- Avoids issues with Thread creation before entering main
- Matches existing APIs such as FunctionPointer and Ticker

Cons
- Does not enforce passing a NetworkInterface

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
TCPServer.h 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
TCPSocket.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/Socket.cpp	Tue Apr 19 18:20:38 2016 -0500
+++ b/Socket.cpp	Tue Apr 19 18:22:15 2016 -0500
@@ -16,12 +16,12 @@
 
 #include "Socket.h"
 
-Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto)
-    : _iface(iface)
+Socket::Socket()
+    : _iface(0)
+    , _socket(0)
     , _blocking(true)
     , _timeout(0)
 {
-    _socket = _iface->socket_create(proto);
 }
 
 Socket::~Socket()
@@ -31,6 +31,28 @@
     }
 }
 
+int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto)
+{
+    _iface = iface;
+    _socket = _iface->socket_create(proto);
+}
+
+int Socket::close(bool shutdown)
+{
+    if (!_socket) {
+        return 0;
+    }
+
+    int err = _iface->socket_close(_socket, shutdown);
+    if (!err) {
+        void *socket = _socket;
+        _socket = 0;
+        _iface->socket_destroy(socket);
+    }
+
+    return err;
+}
+
 void Socket::set_blocking(bool blocking)
 {
     _blocking = blocking;
@@ -59,22 +81,6 @@
     return _iface->socket_get_option(_socket, optname, optval, optlen);
 }
 
-int Socket::close(bool shutdown)
-{
-    if (!_socket) {
-        return 0;
-    }
-
-    int err = _iface->socket_close(_socket, shutdown);
-    if (!err) {
-        void *socket = _socket;
-        _socket = 0;
-        _iface->socket_destroy(socket);
-    }
-
-    return err;
-}
-
 void Socket::thunk(void *p) 
 {
     FunctionPointer *fptr = (FunctionPointer *)p;
--- a/Socket.h	Tue Apr 19 18:20:38 2016 -0500
+++ b/Socket.h	Tue Apr 19 18:22:15 2016 -0500
@@ -27,6 +27,11 @@
     /** Socket lifetime
      */
     virtual ~Socket();
+
+    /** Open the socket
+     *  @param iface    Interface to open socket on
+     */
+    virtual int open(NetworkInterface *iface) = 0;
     
     /** Set blocking or non-blocking mode of the socket
      *  @param blocking true for blocking mode, false for non-blocking mode.
@@ -60,7 +65,8 @@
     int close(bool shutdown=true);
 
 protected:
-    Socket(NetworkInterface *iface, nsapi_protocol_t proto);
+    Socket();
+    int open(NetworkInterface *iface, nsapi_protocol_t proto);
 
     static void thunk(void *);
 
--- a/TCPServer.cpp	Tue Apr 19 18:20:38 2016 -0500
+++ b/TCPServer.cpp	Tue Apr 19 18:22:15 2016 -0500
@@ -17,9 +17,18 @@
 #include "TCPServer.h"
 #include "Timer.h"
 
+TCPServer::TCPServer()
+{
+}
+
 TCPServer::TCPServer(NetworkInterface *iface)
-    : Socket(iface, NSAPI_TCP)
 {
+    open(iface);
+}
+
+int TCPServer::open(NetworkInterface *iface)
+{
+    return Socket::open(iface, NSAPI_TCP);
 }
 
 int TCPServer::bind(uint16_t port)
@@ -45,15 +54,16 @@
     mbed::Timer timer;
     timer.start();
 
-    void *socket = connection->_socket;
-    connection->_socket = 0;
-    _iface->socket_destroy(socket);
+    if (connection->_socket) {
+        connection->close();
+    }
 
     while (true) {
         if (!_socket) {
             return NSAPI_ERROR_NO_SOCKET;   
         }
 
+        void *socket;
         int err = _iface->socket_accept(_socket, &socket);
 
         if (err > 0) {
--- a/TCPServer.h	Tue Apr 19 18:20:38 2016 -0500
+++ b/TCPServer.h	Tue Apr 19 18:22:15 2016 -0500
@@ -27,8 +27,14 @@
 public:
     /** TCP Server lifetime
      */
+    TCPServer();
     TCPServer(NetworkInterface *iface);
     virtual ~TCPServer();
+
+    /** Open the socket
+     *  @param iface    Interface to open socket on
+     */
+    virtual int open(NetworkInterface *iface);
     
     /** Bind a socket to a specific port
      * @param port      The port to listen for incoming connections on
--- a/TCPSocket.cpp	Tue Apr 19 18:20:38 2016 -0500
+++ b/TCPSocket.cpp	Tue Apr 19 18:22:15 2016 -0500
@@ -17,9 +17,18 @@
 #include "TCPSocket.h"
 #include "Timer.h"
 
+TCPSocket::TCPSocket()
+{
+}
+
 TCPSocket::TCPSocket(NetworkInterface *iface)
-    : Socket(iface, NSAPI_TCP)
 {
+    open(iface);
+}
+
+int TCPSocket::open(NetworkInterface *iface)
+{
+    return Socket::open(iface, NSAPI_TCP);
 }
 
 int TCPSocket::connect(const SocketAddress &addr)
--- a/TCPSocket.h	Tue Apr 19 18:20:38 2016 -0500
+++ b/TCPSocket.h	Tue Apr 19 18:22:15 2016 -0500
@@ -26,8 +26,14 @@
 public:
     /** TCP socket lifetime
      */
+    TCPSocket();
     TCPSocket(NetworkInterface *iface);
     virtual ~TCPSocket();
+
+    /** Open the socket
+     *  @param iface    Interface to open socket on
+     */
+    virtual int open(NetworkInterface *iface);
     
     /** Connects this TCP socket to the server
      *  @param host     The host to connect to. It can either be an IP Address
--- a/UDPSocket.cpp	Tue Apr 19 18:20:38 2016 -0500
+++ b/UDPSocket.cpp	Tue Apr 19 18:22:15 2016 -0500
@@ -17,9 +17,18 @@
 #include "UDPSocket.h"
 #include "Timer.h"
 
+UDPSocket::UDPSocket()
+{
+}
+
 UDPSocket::UDPSocket(NetworkInterface *iface)
-    : Socket(iface, NSAPI_UDP)
 {
+    open(iface);
+}
+
+int UDPSocket::open(NetworkInterface *iface)
+{
+    return Socket::open(iface, NSAPI_UDP);
 }
 
 int UDPSocket::bind(uint16_t port)
--- a/UDPSocket.h	Tue Apr 19 18:20:38 2016 -0500
+++ b/UDPSocket.h	Tue Apr 19 18:22:15 2016 -0500
@@ -26,8 +26,14 @@
 public:
     /** UDPSocket lifetime
      */
+    UDPSocket();
     UDPSocket(NetworkInterface *iface);
     virtual ~UDPSocket();
+
+    /** Open the socket
+     *  @param iface    Interface to open socket on
+     */
+    virtual int open(NetworkInterface *iface);
     
     /** Bind a UDP Server Socket to a specific port
      *  @param port     The port to listen for incoming connections on