NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Diff: TCPSocket.cpp
- Revision:
- 82:97d166c4a193
- Parent:
- 81:1600369a29dd
- Child:
- 87:94e2cf3a06be
--- a/TCPSocket.cpp Tue Apr 05 12:52:07 2016 -0500 +++ b/TCPSocket.cpp Tue Apr 05 13:09:43 2016 -0500 @@ -15,6 +15,7 @@ */ #include "TCPSocket.h" +#include "Timer.h" TCPSocket::TCPSocket(NetworkInterface *iface) : Socket(iface, NetworkInterface::TCP) @@ -47,20 +48,38 @@ int TCPSocket::send(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_send(_socket, data, size); + if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || + (_timeout && timer.read_ms() > _timeout)) { + return sent; + } } - - return _iface->socket_send(_socket, data, size); } int TCPSocket::recv(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 recv = _iface->socket_recv(_socket, data, size); + if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || + (_timeout && timer.read_ms() > _timeout)) { + return recv; + } } - - return _iface->socket_recv(_socket, data, size); }