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:
geky
Date:
Fri Apr 01 17:18:27 2016 +0000
Parent:
65:ca337f9ebdab
Child:
67:691869eebacd
Child:
68:a52251517491
Commit message:
Changed API to better match Posix sockets; ; - Send returns size on success; - Returns NS_ERROR_WOULD_BLOCK when recv would block

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
Socket.h Show annotated file Show diff for this revision Revisions of this file
SocketInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/NetworkInterface.h	Fri Apr 01 16:29:45 2016 +0000
+++ b/NetworkInterface.h	Fri Apr 01 17:18:27 2016 +0000
@@ -30,6 +30,7 @@
  *  @brief enum of standardized error codes
  */
 enum ns_error_t {
+    NS_ERROR_WOULD_BLOCK   = -3000,     /*!< no data is not available but call is non-blocking */
     NS_ERROR_TIMEOUT       = -3001,     /*!< operation took longer than allowed */
     NS_ERROR_NO_CONNECTION = -3002,     /*!< not connected to a network */
     NS_ERROR_NO_SOCKET     = -3003,     /*!< socket not available for use */
--- a/Socket.cpp	Fri Apr 01 16:29:45 2016 +0000
+++ b/Socket.cpp	Fri Apr 01 17:18:27 2016 +0000
@@ -98,7 +98,7 @@
         
         int32_t recv = _socket->recv(data, size);
 
-        if (recv != 0 || !blocking) {
+        if (recv != NS_ERROR_WOULD_BLOCK || !blocking) {
             return recv;
         }
     }
--- a/Socket.h	Fri Apr 01 16:29:45 2016 +0000
+++ b/Socket.h	Fri Apr 01 17:18:27 2016 +0000
@@ -43,14 +43,14 @@
     /** Send data over the socket
      *  @param data Buffer of data to send
      *  @param size Size of data to send
-     *  @return 0 on success
+     *  @return Number of bytes sent or a negative value on failure
      */
     int32_t send(const void *data, uint32_t size);
 
     /** Recieve data over the socket
      *  @param data Buffer to store recieved data
      *  @param size Size of provided buffer
-     *  @param blocking If true wait for data, otherwise return 0 if no data is available
+     *  @param blocking If true wait for data, otherwise return NS_ERROR_WOULD_BLOCK
      *  @return Number of bytes recieved or a negative value on failure
      */
     int32_t recv(void *data, uint32_t size, bool blocking = true);
--- a/SocketInterface.h	Fri Apr 01 16:29:45 2016 +0000
+++ b/SocketInterface.h	Fri Apr 01 17:18:27 2016 +0000
@@ -56,14 +56,14 @@
     /** Send data
      *  @param data Buffer of data to send
      *  @param size Size of data to send
-     *  @return 0 on success
+     *  @return Number of bytes received or a negative value on success
      */
     virtual int32_t send(const void *data, uint32_t size) = 0;
 
     /** Receive data
      *  @note
-     *      This call should return immediately with a value of 0
-     *      if no data is available.
+     *      This call should return immediately with a value of 
+     *      NS_ERROR_WOULD_BOCK if no data is available.
      *
      *  @param data A buffer to store the data in
      *  @param size Size of buffer