From f2715b7e771a4242e27d7146634d015efd0c85bc Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 20 Apr 2016 19:10:13 -0500 Subject: [PATCH] Consolidate set_timeout/set_blocking behaviour - Avoids ambiguity when both are used - Matches Python behaviour --- Socket.cpp | 7 +++---- Socket.h | 15 ++++++++++----- TCPServer.cpp | 5 +++-- TCPSocket.cpp | 10 ++++++---- UDPSocket.cpp | 10 ++++++---- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Socket.cpp b/Socket.cpp index 943187d364..d21f984e7e 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -19,8 +19,7 @@ Socket::Socket() : _iface(0) , _socket(0) - , _blocking(true) - , _timeout(0) + , _timeout(-1) { } @@ -83,10 +82,10 @@ int Socket::bind(const SocketAddress &address) void Socket::set_blocking(bool blocking) { - _blocking = blocking; + set_timeout(blocking ? -1 : 0); } -void Socket::set_timeout(unsigned timeout) +void Socket::set_timeout(int timeout) { _timeout = timeout; } diff --git a/Socket.h b/Socket.h index ab093e39d3..195da38204 100644 --- a/Socket.h +++ b/Socket.h @@ -86,7 +86,10 @@ public: * blocking operations such as send/recv/accept return * NSAPI_ERROR_WOULD_BLOCK if they can not continue. * - * @param blocking True for blocking mode, false for non-blocking mode. + * set_blocking(false) is equivalent to set_timeout(-1) + * set_blocking(true) is equivalent to set_timeout(0) + * + * @param blocking true for blocking mode, false for non-blocking mode. */ void set_blocking(bool blocking); @@ -94,11 +97,14 @@ public: * * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK * is returned if a blocking operation takes longer than the specified - * timeout. A timeout of 0 removes a timeout from the socket. + * timeout. A timeout of -1 removes the timeout from the socket. + * + * set_timeout(-1) is equivalent to set_blocking(false) + * set_timeout(0) is equivalent to set_blocking(true) * * @param timeout Timeout in milliseconds */ - void set_timeout(unsigned int timeout); + void set_timeout(int timeout); /* Set stack-specific socket options * @@ -166,8 +172,7 @@ protected: NetworkStack *_iface; void *_socket; - bool _blocking; - unsigned _timeout; + int _timeout; FunctionPointer _callback; }; diff --git a/TCPServer.cpp b/TCPServer.cpp index 83a85d66b4..7a65e954da 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -60,8 +60,9 @@ int TCPServer::accept(TCPSocket *connection) connection->_socket = socket; } - if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { + if (err != NSAPI_ERROR_WOULD_BLOCK + || _timeout < 0 + || timer.read_ms() > _timeout) { return err; } } diff --git a/TCPSocket.cpp b/TCPSocket.cpp index b59715594c..926473dacc 100644 --- a/TCPSocket.cpp +++ b/TCPSocket.cpp @@ -61,8 +61,9 @@ int TCPSocket::send(const void *data, unsigned size) } int sent = _iface->socket_send(_socket, data, size); - if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { + if (sent != NSAPI_ERROR_WOULD_BLOCK + || _timeout < 0 + || timer.read_ms() > _timeout) { return sent; } } @@ -79,8 +80,9 @@ int TCPSocket::recv(void *data, unsigned size) } int recv = _iface->socket_recv(_socket, data, size); - if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { + if (recv != NSAPI_ERROR_WOULD_BLOCK + || _timeout < 0 + || timer.read_ms() > _timeout) { return recv; } } diff --git a/UDPSocket.cpp b/UDPSocket.cpp index 85569c7a58..a8148fb38f 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -52,8 +52,9 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s } int sent = _iface->socket_sendto(_socket, address, data, size); - if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { + if (sent != NSAPI_ERROR_WOULD_BLOCK + || _timeout < 0 + || timer.read_ms() > _timeout) { return sent; } } @@ -70,8 +71,9 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size) } int recv = _iface->socket_recvfrom(_socket, address, buffer, size); - if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { + if (recv != NSAPI_ERROR_WOULD_BLOCK + || _timeout < 0 + || timer.read_ms() > _timeout) { return recv; } }