ap

Dependencies:   DnsQuery

Dependents:   WizFi310_TCP_Echo_Server_Example

Fork of NetworkSocketAPI by NetworkSocketAPI

Revision:
82:97d166c4a193
Parent:
81:1600369a29dd
Child:
86:7ca9776b9cc0
--- a/UDPSocket.cpp	Tue Apr 05 12:52:07 2016 -0500
+++ b/UDPSocket.cpp	Tue Apr 05 13:09:43 2016 -0500
@@ -15,6 +15,7 @@
  */
 
 #include "UDPSocket.h"
+#include "Timer.h"
 
 UDPSocket::UDPSocket(NetworkInterface *iface)
     : Socket(iface, NetworkInterface::UDP)
@@ -32,20 +33,38 @@
 
 int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size)
 {
-    if (!_socket) {
-        return NSAPI_ERROR_NO_SOCKET;   
+    mbed::Timer timer;
+    timer.start();
+
+    while (true) {
+        if (!_socket) {
+            return NSAPI_ERROR_NO_SOCKET;
+        }
+    
+        int sent = _iface->socket_sendto(_socket, address, data, size);
+        if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
+            (_timeout && timer.read_ms() > _timeout)) {
+            return sent;
+        }
     }
-
-    return _iface->socket_sendto(_socket, address, data, size);
 }
 
 int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
 {
-    if (!_socket) {
-        return NSAPI_ERROR_NO_SOCKET;   
+    mbed::Timer timer;
+    timer.start();
+
+    while (true) {
+        if (!_socket) {
+            return NSAPI_ERROR_NO_SOCKET;
+        }
+    
+        int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
+        if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || 
+            (_timeout && timer.read_ms() > _timeout)) {
+            return recv;
+        }
     }
-
-    return _iface->socket_recvfrom(_socket, address, buffer, size);
 }