Skip to content

Commit f2715b7

Browse files
committed
Consolidate set_timeout/set_blocking behaviour
- Avoids ambiguity when both are used - Matches Python behaviour
1 parent bd5e913 commit f2715b7

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

Socket.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
Socket::Socket()
2020
: _iface(0)
2121
, _socket(0)
22-
, _blocking(true)
23-
, _timeout(0)
22+
, _timeout(-1)
2423
{
2524
}
2625

@@ -83,10 +82,10 @@ int Socket::bind(const SocketAddress &address)
8382

8483
void Socket::set_blocking(bool blocking)
8584
{
86-
_blocking = blocking;
85+
set_timeout(blocking ? -1 : 0);
8786
}
8887

89-
void Socket::set_timeout(unsigned timeout)
88+
void Socket::set_timeout(int timeout)
9089
{
9190
_timeout = timeout;
9291
}

Socket.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,25 @@ class Socket {
8686
* blocking operations such as send/recv/accept return
8787
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
8888
*
89-
* @param blocking True for blocking mode, false for non-blocking mode.
89+
* set_blocking(false) is equivalent to set_timeout(-1)
90+
* set_blocking(true) is equivalent to set_timeout(0)
91+
*
92+
* @param blocking true for blocking mode, false for non-blocking mode.
9093
*/
9194
void set_blocking(bool blocking);
9295

9396
/** Set timeout on blocking socket operations
9497
*
9598
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
9699
* is returned if a blocking operation takes longer than the specified
97-
* timeout. A timeout of 0 removes a timeout from the socket.
100+
* timeout. A timeout of -1 removes the timeout from the socket.
101+
*
102+
* set_timeout(-1) is equivalent to set_blocking(false)
103+
* set_timeout(0) is equivalent to set_blocking(true)
98104
*
99105
* @param timeout Timeout in milliseconds
100106
*/
101-
void set_timeout(unsigned int timeout);
107+
void set_timeout(int timeout);
102108

103109
/* Set stack-specific socket options
104110
*
@@ -166,8 +172,7 @@ class Socket {
166172

167173
NetworkStack *_iface;
168174
void *_socket;
169-
bool _blocking;
170-
unsigned _timeout;
175+
int _timeout;
171176
FunctionPointer _callback;
172177
};
173178

TCPServer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ int TCPServer::accept(TCPSocket *connection)
6060
connection->_socket = socket;
6161
}
6262

63-
if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
64-
(_timeout && timer.read_ms() > _timeout)) {
63+
if (err != NSAPI_ERROR_WOULD_BLOCK
64+
|| _timeout < 0
65+
|| timer.read_ms() > _timeout) {
6566
return err;
6667
}
6768
}

TCPSocket.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ int TCPSocket::send(const void *data, unsigned size)
6161
}
6262

6363
int sent = _iface->socket_send(_socket, data, size);
64-
if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
65-
(_timeout && timer.read_ms() > _timeout)) {
64+
if (sent != NSAPI_ERROR_WOULD_BLOCK
65+
|| _timeout < 0
66+
|| timer.read_ms() > _timeout) {
6667
return sent;
6768
}
6869
}
@@ -79,8 +80,9 @@ int TCPSocket::recv(void *data, unsigned size)
7980
}
8081

8182
int recv = _iface->socket_recv(_socket, data, size);
82-
if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
83-
(_timeout && timer.read_ms() > _timeout)) {
83+
if (recv != NSAPI_ERROR_WOULD_BLOCK
84+
|| _timeout < 0
85+
|| timer.read_ms() > _timeout) {
8486
return recv;
8587
}
8688
}

UDPSocket.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
5252
}
5353

5454
int sent = _iface->socket_sendto(_socket, address, data, size);
55-
if (sent != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
56-
(_timeout && timer.read_ms() > _timeout)) {
55+
if (sent != NSAPI_ERROR_WOULD_BLOCK
56+
|| _timeout < 0
57+
|| timer.read_ms() > _timeout) {
5758
return sent;
5859
}
5960
}
@@ -70,8 +71,9 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
7071
}
7172

7273
int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
73-
if (recv != NSAPI_ERROR_WOULD_BLOCK || !_blocking ||
74-
(_timeout && timer.read_ms() > _timeout)) {
74+
if (recv != NSAPI_ERROR_WOULD_BLOCK
75+
|| _timeout < 0
76+
|| timer.read_ms() > _timeout) {
7577
return recv;
7678
}
7779
}

0 commit comments

Comments
 (0)