extmod/modlwip: Consolidate socket.accept timeout logic.
This makes the code a bit simpler to understand for the three cases of timeout behaviour (-1, 0, non-zero), and eliminates a dependency on the (slow) `mp_hal_delay_ms(100)` call. Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
@@ -1040,26 +1040,22 @@ static mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
|
|||||||
// accept incoming connection
|
// accept incoming connection
|
||||||
struct tcp_pcb *volatile *incoming_connection = &lwip_socket_incoming_array(socket)[socket->incoming.connection.iget];
|
struct tcp_pcb *volatile *incoming_connection = &lwip_socket_incoming_array(socket)[socket->incoming.connection.iget];
|
||||||
if (*incoming_connection == NULL) {
|
if (*incoming_connection == NULL) {
|
||||||
if (socket->timeout == 0) {
|
mp_uint_t ticks_start = mp_hal_ticks_ms();
|
||||||
|
for (;;) {
|
||||||
MICROPY_PY_LWIP_EXIT
|
MICROPY_PY_LWIP_EXIT
|
||||||
m_del_obj(lwip_socket_obj_t, socket2);
|
poll_sockets();
|
||||||
mp_raise_OSError(MP_EAGAIN);
|
MICROPY_PY_LWIP_REENTER
|
||||||
} else if (socket->timeout != -1) {
|
if (*incoming_connection != NULL) {
|
||||||
mp_uint_t retries = socket->timeout / 100;
|
break;
|
||||||
while (*incoming_connection == NULL) {
|
}
|
||||||
|
if (socket_is_timedout(socket, ticks_start)) {
|
||||||
MICROPY_PY_LWIP_EXIT
|
MICROPY_PY_LWIP_EXIT
|
||||||
if (retries-- == 0) {
|
m_del_obj(lwip_socket_obj_t, socket2);
|
||||||
m_del_obj(lwip_socket_obj_t, socket2);
|
if (socket->timeout == 0) {
|
||||||
|
mp_raise_OSError(MP_EAGAIN);
|
||||||
|
} else {
|
||||||
mp_raise_OSError(MP_ETIMEDOUT);
|
mp_raise_OSError(MP_ETIMEDOUT);
|
||||||
}
|
}
|
||||||
mp_hal_delay_ms(100);
|
|
||||||
MICROPY_PY_LWIP_REENTER
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while (*incoming_connection == NULL) {
|
|
||||||
MICROPY_PY_LWIP_EXIT
|
|
||||||
poll_sockets();
|
|
||||||
MICROPY_PY_LWIP_REENTER
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user