NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Thu Feb 25 03:13:26 2016 -0600
Branch:
api-changes
Parent:
47:13929d610ed8
Child:
49:85fe0b99948d
Commit message:
Added non-blocking recv

Changed in this revision

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/Socket.cpp	Thu Feb 25 21:58:46 2016 +0000
+++ b/Socket.cpp	Thu Feb 25 03:13:26 2016 -0600
@@ -141,19 +141,26 @@
     return err;
 }
 
-int32_t Socket::send(const void *data, uint32_t len)
+int32_t Socket::send(const void *data, uint32_t size)
 {
     if (!_socket) {
         return NS_ERROR_NO_CONNECTION;
     }
-    return _socket->send(data, len);
+    return _socket->send(data, size);
 }
 
-int32_t Socket::recv(void *data, uint32_t len)
+int32_t Socket::recv(void *data, uint32_t size, bool blocking)
 {
     if (!_socket) {
         return NS_ERROR_NO_CONNECTION;
     }
-    return _socket->recv(data, len);
+
+    while (true) {
+        int32_t size = _socket->recv(data, size);
+
+        if (size != 0 || !blocking) {
+            return size;
+        }
+    }
 }
 
--- a/Socket.h	Thu Feb 25 21:58:46 2016 +0000
+++ b/Socket.h	Thu Feb 25 03:13:26 2016 -0600
@@ -73,17 +73,18 @@
 
     /** Send data over the socket
      *  @param data Buffer of data to send
-     *  @param len Size of data to send
+     *  @param size Size of data to send
      *  @return 0 on success
      */
-    int32_t send(const void *data, uint32_t len);
+    int32_t send(const void *data, uint32_t size);
 
     /** Recieve data over the socket
      *  @param data Buffer to store recieved data
-     *  @param len Size of provided buffer
-     *  @return Number of bytes sent or a negative value on failure
+     *  @param size Size of provided buffer
+     *  @param blocking If true wait for data, otherwise return 0 if no data is available
+     *  @return Number of bytes recieved or a negative value on failure
      */
-    int32_t recv(void *data, uint32_t len);
+    int32_t recv(void *data, uint32_t size, bool blocking = true);
 
 
 protected:
--- a/SocketInterface.h	Thu Feb 25 21:58:46 2016 +0000
+++ b/SocketInterface.h	Thu Feb 25 03:13:26 2016 -0600
@@ -46,11 +46,6 @@
      */
     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) { (void)timeout; }
-
     /** Status of the socket
      *  @return True if connected
      */
@@ -74,17 +69,21 @@
 
     /** Send data
      *  @param data Buffer of data to send
-     *  @param len Size of data to send
+     *  @param size Size of data to send
      *  @return 0 on success
      */
-    virtual int32_t send(const void *data, uint32_t len) = 0;
+    virtual int32_t send(const void *data, uint32_t size) = 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
-     *  @return Number of bytes sent or a negative value on failure
+    /** Receive data
+     *  @note
+     *      This call should return immediately with a value of 0 
+     *      if no data is available.
+     *
+     *  @param data A buffer to store the data in
+     *  @param size Size of buffer
+     *  @return Number of bytes received or a negative value on failure
      */
-    virtual int32_t recv(void *data, uint32_t len) = 0;
+    virtual int32_t recv(void *data, uint32_t size) = 0;
 };
 
 #endif