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:23:42 2016 -0500
Parent:
92:dd5f19874adf
Child:
94:644df37bb05b
Commit message:
Renamed NetworkInterface create/destroy methods to match Socket methods

- socket_create -> socket_open
- socket_destroy -> socket_close

Changed in this revision

NetworkInterface.h 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
TCPServer.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/NetworkInterface.h	Tue Apr 19 18:23:29 2016 -0500
+++ b/NetworkInterface.h	Tue Apr 19 18:23:42 2016 -0500
@@ -89,16 +89,20 @@
     friend class TCPSocket;
     friend class TCPServer;
 
-    /** Create a socket
-     *  @param proto    The type of socket to open, TCP or UDP
-     *  @return         The alocated socket or null on failure
+    /** Open a socket
+     *  @param handle   Handle in which to store new socket
+     *  @param proto    Type of socket to open, NSAPI_TCP or NSAPI_UDP
+     *  @return         0 on success, negative on failure
      */
-    virtual void *socket_create(nsapi_protocol_t proto) = 0;
+    virtual int socket_open(void **handle, nsapi_protocol_t proto) = 0;
 
-    /** Destroy a socket
-     *  @param socket     Previously allocated socket
+    /** Close the socket
+     *  @param handle   Socket handle
+     *  @return         0 on success, negative on failure
+     *  @note On failure, any memory associated with the socket must still 
+     *        be cleaned up
      */
-    virtual void socket_destroy(void *handle) = 0;
+    virtual int socket_close(void *handle) = 0;
 
     /** Set socket options
      *  @param handle   Socket handle
@@ -147,13 +151,13 @@
     virtual bool socket_is_connected(void *handle) = 0;
 
     /** Accept a new connection.
-     *  @param handle   Socket handle
-     *  @param socket   A TCPSocket instance that will handle the incoming connection.
-     *  @return         0 on success, negative on failure.
+     *  @param handle   Handle in which to store new socket
+     *  @param server   Socket handle to server to accept from
+     *  @return         0 on success, negative on failure
      *  @note This call is not-blocking, if this call would block, must
      *        immediately return NSAPI_ERROR_WOULD_WAIT
      */
-    virtual int socket_accept(void *handle, void **connection) = 0;
+    virtual int socket_accept(void **handle, void *server) = 0;
 
     /** Send data to the remote host
      *  @param handle   Socket handle
@@ -199,11 +203,6 @@
      */
     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
 
-    /** Close the socket
-     *  @param handle   Socket handle
-     */
-    virtual int socket_close(void *handle) = 0;
-
     /** Register a callback on state change of the socket
      *  @param handle   Socket handle
      *  @param callback Function to call on state change
--- a/Socket.cpp	Tue Apr 19 18:23:29 2016 -0500
+++ b/Socket.cpp	Tue Apr 19 18:23:42 2016 -0500
@@ -34,8 +34,17 @@
 int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto)
 {
     _iface = iface;
-    _socket = _iface->socket_create(proto);
+
+    void *socket;
+    int err = _iface->socket_open(&socket, proto);
+    if (err) {
+        return err;
+    }
+
+    _socket = socket;
     _iface->socket_attach(_socket, &Socket::thunk, this);
+
+    return 0;
 }
 
 int Socket::close()
@@ -44,14 +53,9 @@
         return 0;
     }
 
-    int err = _iface->socket_close(_socket);
-    if (!err) {
-        void *socket = _socket;
-        _socket = 0;
-        _iface->socket_destroy(socket);
-    }
-
-    return err;
+    void *socket = _socket;
+    _socket = 0;
+    return _iface->socket_close(socket);
 }
 
 void Socket::set_blocking(bool blocking)
--- a/TCPServer.cpp	Tue Apr 19 18:23:29 2016 -0500
+++ b/TCPServer.cpp	Tue Apr 19 18:23:42 2016 -0500
@@ -64,9 +64,8 @@
         }
 
         void *socket;
-        int err = _iface->socket_accept(_socket, &socket);
-
-        if (err > 0) {
+        int err = _iface->socket_accept(&socket, _socket);
+        if (!err) {
             connection->_socket = socket;
         }