NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Wed Feb 24 22:04:19 2016 -0600
Branch:
api-changes
Parent:
42:49893d13c432
Child:
44:aef3f416e4b0
Commit message:
Responded to feedback in API decisions

- Removed small .cpp files
- Added null _iface checks
- Matched mbed coding standard
- Removed setTimeout/getTimeout

Changed in this revision

NetworkInterface.cpp Show annotated file Show diff for this revision Revisions of this file
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
TCPSocket.h 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/NetworkInterface.cpp	Tue Feb 23 05:07:02 2016 -0600
+++ b/NetworkInterface.cpp	Wed Feb 24 22:04:19 2016 -0600
@@ -20,7 +20,6 @@
 #include <string.h>
 
 NetworkInterface::NetworkInterface()
-    : _timeout(15000)
 {
     memset(_ip_address, 0, NS_IP_SIZE);
     memset(_network_mask, 0, NS_IP_SIZE);
@@ -76,16 +75,6 @@
     }
 }
 
-void NetworkInterface::setTimeout(uint32_t timeout)
-{
-    _timeout = timeout;
-}
-
-uint32_t NetworkInterface::getTimeout()
-{
-    return _timeout;
-}
-
 bool NetworkInterface::isConnected()
 {
     return getIPAddress() != 0;
--- a/NetworkInterface.h	Tue Feb 23 05:07:02 2016 -0600
+++ b/NetworkInterface.h	Wed Feb 24 22:04:19 2016 -0600
@@ -74,17 +74,6 @@
      *  @return String MAC address of the interface
      */
     virtual const char *getMACAddress() = 0;
-    
-    /** Set a timeout on network operations
-     *  @param timeout Maximum time in milliseconds for socket operations
-     */
-    virtual void setTimeout(uint32_t timeout);
-
-    /** Get the current timeout on network operations
-     *  @return Maximum time in milliseconds for socket operations
-     */
-    virtual uint32_t getTimeout();
-    
 
     /** Get the current status of the interface
      *  @return true if connected
@@ -119,7 +108,6 @@
     char _ip_address[NS_IP_SIZE];
     char _network_mask[NS_IP_SIZE];
     char _gateway[NS_IP_SIZE];
-    uint32_t _timeout;
 };
 
 #endif
--- 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);
 }
 
--- a/Socket.h	Tue Feb 23 05:07:02 2016 -0600
+++ b/Socket.h	Wed Feb 24 22:04:19 2016 -0600
@@ -53,16 +53,6 @@
      */
     uint16_t getPort() const;
 
-    /** Set a timeout on network operations
-     *  @param timeout Maximum time in milliseconds for socket operations
-     */
-    void setTimeout(uint32_t timeout);
-
-    /** Get the current timeout on network operations
-     *  @return Maximum time in milliseconds for socket operations
-     */
-    uint32_t getTimeout() const;
-
     /** Returns status of socket
      *  @return true if connected
      */
@@ -70,11 +60,11 @@
 
 
     /** Open a connection to the underlying address
-     *  @param url Optional URL or IP address to connect to
+     *  @param address Optional URL or IP address to connect to
      *  @param port Optional port to connect to
      *  @return 0 on success
      */
-    int32_t open(const char *url = 0, uint16_t port = 0);
+    int32_t open(const char *address = 0, uint16_t port = 0);
 
     /** Close an open connection
      *  @return 0 on success
@@ -107,7 +97,6 @@
 
     char _ip_address[NS_IP_SIZE];
     uint16_t _port;
-    uint32_t _timeout;
 };
 
 #endif
--- a/SocketInterface.h	Tue Feb 23 05:07:02 2016 -0600
+++ b/SocketInterface.h	Wed Feb 24 22:04:19 2016 -0600
@@ -39,22 +39,26 @@
     /** Set the IP address of the socket
      *  @param ip IP address to connect to
      */
-    virtual void setIPAddress(const char *ip);
+    virtual void setIPAddress(const char *ip) { (void)ip; }
 
     /** Set the port of the socket
      *  @param port Port to connect to
      */
-    virtual void setPort(uint16_t port);
+    virtual void setPort(uint16_t port) { (void)port; }
 
     /** Set a timeout on network operations
      *  @param timeout Maximum time in milliseconds for socket operations
      */
-    virtual void setTimeout(uint32_t timeout);
+    virtual void setTimeout(uint32_t timeout) { (void)timeout; }
 
     /** Status of the socket
      *  @return True if connected
      */
-    virtual bool isConnected();
+    virtual bool isConnected()
+    {
+        // By default return true if socket was created successfully
+        return true;
+    }
 
     /** Open a connection to the underlying address
      *  @param ip IP address to connect to
--- a/TCPSocket.h	Tue Feb 23 05:07:02 2016 -0600
+++ b/TCPSocket.h	Wed Feb 24 22:04:19 2016 -0600
@@ -32,7 +32,10 @@
      *  @param url Optional URL to connect to, copied internally
      *  @param port Optional port to connect to
      */
-    TCPSocket(NetworkInterface *iface);
+    TCPSocket(NetworkInterface *iface)
+        : Socket(iface, SOCK_TCP)
+    {
+    }
 };
 
 #endif
--- a/UDPSocket.h	Tue Feb 23 05:07:02 2016 -0600
+++ b/UDPSocket.h	Wed Feb 24 22:04:19 2016 -0600
@@ -32,7 +32,10 @@
      *  @param ip Optional URL to connect to, copied internally
      *  @param port Optional port to connect to
      */
-    UDPSocket(NetworkInterface *iface);
+    UDPSocket(NetworkInterface *iface)
+        : Socket(iface, SOCK_UDP)
+    {
+    }
 };
 
 #endif