Consolidate set_timeout/set_blocking behaviour

- Avoids ambiguity when both are used
- Matches Python behaviour
pull/2216/head^2
Christopher Haster 2016-04-20 19:10:13 -05:00
parent bd5e913ca1
commit f2715b7e77
5 changed files with 28 additions and 19 deletions

View File

@ -19,8 +19,7 @@
Socket::Socket() Socket::Socket()
: _iface(0) : _iface(0)
, _socket(0) , _socket(0)
, _blocking(true) , _timeout(-1)
, _timeout(0)
{ {
} }
@ -83,10 +82,10 @@ int Socket::bind(const SocketAddress &address)
void Socket::set_blocking(bool blocking) 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; _timeout = timeout;
} }

View File

@ -86,7 +86,10 @@ public:
* blocking operations such as send/recv/accept return * blocking operations such as send/recv/accept return
* NSAPI_ERROR_WOULD_BLOCK if they can not continue. * 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); void set_blocking(bool blocking);
@ -94,11 +97,14 @@ public:
* *
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
* is returned if a blocking operation takes longer than the specified * 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 * @param timeout Timeout in milliseconds
*/ */
void set_timeout(unsigned int timeout); void set_timeout(int timeout);
/* Set stack-specific socket options /* Set stack-specific socket options
* *
@ -166,8 +172,7 @@ protected:
NetworkStack *_iface; NetworkStack *_iface;
void *_socket; void *_socket;
bool _blocking; int _timeout;
unsigned _timeout;
FunctionPointer _callback; FunctionPointer _callback;
}; };

View File

@ -60,8 +60,9 @@ int TCPServer::accept(TCPSocket *connection)
connection->_socket = socket; connection->_socket = socket;
} }
if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking || if (err != NSAPI_ERROR_WOULD_BLOCK
(_timeout && timer.read_ms() > _timeout)) { || _timeout < 0
|| timer.read_ms() > _timeout) {
return err; return err;
} }
} }

View File

@ -61,8 +61,9 @@ int TCPSocket::send(const void *data, unsigned size)
} }
int sent = _iface->socket_send(_socket, data, size); int sent = _iface->socket_send(_socket, data, size);
if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || if (sent != NSAPI_ERROR_WOULD_BLOCK
(_timeout && timer.read_ms() > _timeout)) { || _timeout < 0
|| timer.read_ms() > _timeout) {
return sent; return sent;
} }
} }
@ -79,8 +80,9 @@ int TCPSocket::recv(void *data, unsigned size)
} }
int recv = _iface->socket_recv(_socket, data, size); int recv = _iface->socket_recv(_socket, data, size);
if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || if (recv != NSAPI_ERROR_WOULD_BLOCK
(_timeout && timer.read_ms() > _timeout)) { || _timeout < 0
|| timer.read_ms() > _timeout) {
return recv; return recv;
} }
} }

View File

@ -52,8 +52,9 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
} }
int sent = _iface->socket_sendto(_socket, address, data, size); int sent = _iface->socket_sendto(_socket, address, data, size);
if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking || if (sent != NSAPI_ERROR_WOULD_BLOCK
(_timeout && timer.read_ms() > _timeout)) { || _timeout < 0
|| timer.read_ms() > _timeout) {
return sent; return sent;
} }
} }
@ -70,8 +71,9 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
} }
int recv = _iface->socket_recvfrom(_socket, address, buffer, size); int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking || if (recv != NSAPI_ERROR_WOULD_BLOCK
(_timeout && timer.read_ms() > _timeout)) { || _timeout < 0
|| timer.read_ms() > _timeout) {
return recv; return recv;
} }
} }