NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Tue Feb 23 04:01:38 2016 -0600
Branch:
api-changes
Parent:
40:11d4a94df3f7
Child:
42:49893d13c432
Commit message:
Moved timeout handling to setTimeout/getTimeout functions

Changed in this revision

EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
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.cpp 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
WiFiInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.h	Mon Feb 22 23:52:09 2016 -0600
+++ b/EthernetInterface.h	Tue Feb 23 04:01:38 2016 -0600
@@ -27,10 +27,9 @@
 {
 public:
     /** Start the interface
-     *  @param timeout_ms Time in milliseconds to wait for a connection
      *  @return 0 on success
      */
-    virtual int32_t connect(uint32_t timeout_ms = 15000) = 0;
+    virtual int32_t connect() = 0;
 
     /** Stop the interface
      *  @return 0 on success
--- a/NetworkInterface.cpp	Mon Feb 22 23:52:09 2016 -0600
+++ b/NetworkInterface.cpp	Tue Feb 23 04:01:38 2016 -0600
@@ -20,6 +20,7 @@
 #include <string.h>
 
 NetworkInterface::NetworkInterface()
+    : _timeout(15000)
 {
     memset(_ip_address, 0, SOCK_IP_SIZE);
     memset(_network_mask, 0, SOCK_IP_SIZE);
@@ -75,6 +76,16 @@
     }
 }
 
+void NetworkInterface::setTimeout(uint32_t timeout)
+{
+    _timeout = timeout;
+}
+
+uint32_t NetworkInterface::getTimeout()
+{
+    return _timeout;
+}
+
 bool NetworkInterface::isConnected()
 {
     return getIPAddress() != 0;
--- a/NetworkInterface.h	Mon Feb 22 23:52:09 2016 -0600
+++ b/NetworkInterface.h	Tue Feb 23 04:01:38 2016 -0600
@@ -70,12 +70,23 @@
      *  @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
      */
     virtual bool isConnected(void);
-    
+
     /** Looks up the specified host's IP address
      *  @param name URL of host
      *  @param ip Buffer to hold IP address, must be at least SOCK_IP_SIZE
@@ -104,6 +115,7 @@
     char _ip_address[SOCK_IP_SIZE];
     char _network_mask[SOCK_IP_SIZE];
     char _gateway[SOCK_IP_SIZE];
+    uint32_t _timeout;
 };
 
 #endif
--- a/Socket.cpp	Mon Feb 22 23:52:09 2016 -0600
+++ b/Socket.cpp	Tue Feb 23 04:01:38 2016 -0600
@@ -24,6 +24,8 @@
 {
     memset(_ip_address, 0, SOCK_IP_SIZE);
     _port = 0;
+
+    _timeout = iface->getTimeout();
 }
 
 Socket::~Socket()
@@ -75,6 +77,20 @@
     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) {
@@ -86,13 +102,13 @@
 
 int32_t Socket::open(const char *url, uint16_t port)
 {
-    if (_socket) {
-        int32_t err = close();
-        if (err) return err;
-    }
+    int32_t err;
+
+    err = close();
+    if (err) return err;
 
     if (url) {
-        int32_t err = setURL(url);
+        err = setURL(url);
         if (err) return err;
     }
 
@@ -107,7 +123,9 @@
     _socket = _iface->createSocket(_proto);
     if (!_socket) return -2;
 
-    int32_t err = _socket->open(_ip_address, _port);
+    _socket->setTimeout(_timeout);
+
+    err = _socket->open(_ip_address, _port);
 
     if (err) {
         _iface->destroySocket(_socket);
@@ -129,15 +147,15 @@
     return err;
 }
 
-int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
+int32_t Socket::send(const void *data, uint32_t len)
 {
     if (!_socket) return -2;
-    return _socket->send(data, len, timeout_ms);
+    return _socket->send(data, len);
 }
 
-int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
+int32_t Socket::recv(void *data, uint32_t len)
 {
     if (!_socket) return -2;
-    return _socket->recv(data, len, timeout_ms);
+    return _socket->recv(data, len);
 }
 
--- a/Socket.h	Mon Feb 22 23:52:09 2016 -0600
+++ b/Socket.h	Tue Feb 23 04:01:38 2016 -0600
@@ -53,6 +53,16 @@
      */
     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
      */
@@ -74,18 +84,16 @@
     /** Send data over the socket
      *  @param data Buffer of data to send
      *  @param len Size of data to send
-     *  @param timeout_ms Maximum amount of time to wait
      *  @return 0 on success
      */
-    int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
+    int32_t send(const void *data, uint32_t len);
 
     /** Recieve data over the socket
      *  @param data Buffer to store recieved data
      *  @param len Size of provided buffer
-     *  @param timeout_ms Maximum amount of time to wait
      *  @return Number of bytes sent or a negative value on failure
      */
-    int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
+    int32_t recv(void *data, uint32_t len);
 
 
 protected:
@@ -99,6 +107,7 @@
 
     char _ip_address[SOCK_IP_SIZE];
     uint16_t _port;
+    uint32_t _timeout;
 };
 
 #endif
--- a/SocketInterface.cpp	Mon Feb 22 23:52:09 2016 -0600
+++ b/SocketInterface.cpp	Tue Feb 23 04:01:38 2016 -0600
@@ -16,9 +16,10 @@
 
 #include "SocketInterface.h"
 
-// By default setIPAddress/setPort has no special behaviour 
-void SocketInterface::setIPAddress(const char *ip) {}
-void SocketInterface::setPort(uint16_t port) {}
+// By default set hooks have no special behaviour 
+void SocketInterface::setIPAddress(const char *) {}
+void SocketInterface::setPort(uint16_t) {}
+void SocketInterface::setTimeout(uint32_t) {}
 
 // By default isConnected is always true if the socket
 // was created successfully
--- a/SocketInterface.h	Mon Feb 22 23:52:09 2016 -0600
+++ b/SocketInterface.h	Tue Feb 23 04:01:38 2016 -0600
@@ -37,7 +37,7 @@
 {
 public:
     /** Set the IP address of the socket
-     *  @param ip IP address to connect to, copied internally
+     *  @param ip IP address to connect to
      */
     virtual void setIPAddress(const char *ip);
 
@@ -46,6 +46,11 @@
      */
     virtual void setPort(uint16_t port);
 
+    /** Set a timeout on network operations
+     *  @param timeout Maximum time in milliseconds for socket operations
+     */
+    virtual void setTimeout(uint32_t timeout);
+
     /** Status of the socket
      *  @return True if connected
      */
@@ -66,18 +71,16 @@
     /** Send data
      *  @param data Buffer of data to send
      *  @param len Size of data to send
-     *  @param timeout_ms Maximum amount of time to wait
      *  @return 0 on success
      */
-    virtual int32_t send(const void *data, uint32_t len, uint32_t timeout_ms) = 0;
+    virtual int32_t send(const void *data, uint32_t len) = 0;
 
     /** In client or server mode receive data
      *  @param data a buffer to store the data in
      *  @param amount The amount of data to receive
-     *  @param timeout_ms The longest time to wait for the data
      *  @return Number of bytes sent or a negative value on failure
      */
-    virtual int32_t recv(void *data, uint32_t len, uint32_t timeout_ms) = 0;
+    virtual int32_t recv(void *data, uint32_t len) = 0;
 };
 
 #endif
--- a/WiFiInterface.h	Mon Feb 22 23:52:09 2016 -0600
+++ b/WiFiInterface.h	Tue Feb 23 04:01:38 2016 -0600
@@ -39,14 +39,12 @@
      *  @param ssid Name of the network to connect to
      *  @param pass Security passphrase to connect to the network
      *  @param security Type of encryption to connect with
-     *  @param timeout_ms Time in milliseconds to wait for a connection
      *  @return 0 on success
      */
     virtual int32_t connect(
         const char *ssid,
         const char *pass,
-        wifi_security_t security = WI_NONE,
-        uint32_t timeout_ms = 15000) = 0;
+        wifi_security_t security = WI_NONE) = 0;
 
     /** Stop the interface
      *  @return 0 on success