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.

Branch:
api-changes
Revision:
43:09ea32f2eb54
Parent:
42:49893d13c432
Child:
45:c8aca7c1e93f
--- a/Socket.cpp	Tue Feb 23 05:07:02 2016 -0600
+++ b/Socket.cpp	Wed Feb 24 22:04:19 2016 -0600
@@ -24,19 +24,25 @@
 {
     memset(_ip_address, 0, NS_IP_SIZE);
     _port = 0;
-
-    _timeout = iface->getTimeout();
 }
 
 Socket::~Socket()
 {
-    if (_socket) close();
+    if (_socket) {
+        close();
+    }
 }
 
 int32_t Socket::setURL(const char *url)
 {
+    if (!_iface) {
+        return -4;
+    }
+
     int32_t err = _iface->getHostByName(url, _ip_address);
-    if (err) return err;
+    if (err) {
+        return err;
+    }
 
     if (_socket) {
         _socket->setIPAddress(_ip_address);
@@ -77,20 +83,6 @@
     return _port;
 }
 
-void Socket::setTimeout(uint32_t timeout)
-{
-    _timeout = timeout;
-
-    if (_socket) {
-        _socket->setTimeout(timeout);
-    }
-}
-
-uint32_t Socket::getTimeout() const
-{
-    return _timeout;
-}
-
 bool Socket::isConnected()
 {
     if (!_socket) {
@@ -100,16 +92,24 @@
     return _socket->isConnected();
 }
 
-int32_t Socket::open(const char *url, uint16_t port)
+int32_t Socket::open(const char *address, uint16_t port)
 {
     int32_t err;
 
     err = close();
-    if (err) return err;
+    if (err) {
+        return err;
+    }
 
-    if (url) {
-        err = setURL(url);
-        if (err) return err;
+    if (!_iface) {
+        return -4;
+    }
+
+    if (address) {
+        err = setURL(address);
+        if (err) {
+            return err;
+        }
     }
 
     if (port) {
@@ -121,9 +121,9 @@
     }
 
     _socket = _iface->createSocket(_proto);
-    if (!_socket) return -2;
-
-    _socket->setTimeout(_timeout);
+    if (!_socket) {
+        return -2;
+    }
 
     err = _socket->open(_ip_address, _port);
 
@@ -136,7 +136,9 @@
 
 int32_t Socket::close()
 {
-    if (!_socket) return 0;
+    if (!_socket) {
+        return 0;
+    }
 
     int32_t err = _socket->close();
 
@@ -149,13 +151,17 @@
 
 int32_t Socket::send(const void *data, uint32_t len)
 {
-    if (!_socket) return -2;
+    if (!_socket) {
+        return -2;
+    }
     return _socket->send(data, len);
 }
 
 int32_t Socket::recv(void *data, uint32_t len)
 {
-    if (!_socket) return -2;
+    if (!_socket) {
+        return -2;
+    }
     return _socket->recv(data, len);
 }