diff options
| author | Damien George <damien@micropython.org> | 2024-06-07 13:36:30 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2024-06-08 09:02:01 +1000 |
| commit | 5903ee561c8e2fec7ecc8f08f221c27574090649 (patch) | |
| tree | bb3c48a4db0a75295dc59be2b4188156412883aa | |
| parent | df0d7e942986b65ed42fbbf75712639f4a83dc0e (diff) | |
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>
| -rw-r--r-- | extmod/modlwip.c | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 1ae8c114c..bcee3f0ff 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1040,26 +1040,22 @@ static mp_obj_t lwip_socket_accept(mp_obj_t self_in) { // accept incoming connection struct tcp_pcb *volatile *incoming_connection = &lwip_socket_incoming_array(socket)[socket->incoming.connection.iget]; if (*incoming_connection == NULL) { - if (socket->timeout == 0) { + mp_uint_t ticks_start = mp_hal_ticks_ms(); + for (;;) { MICROPY_PY_LWIP_EXIT - m_del_obj(lwip_socket_obj_t, socket2); - mp_raise_OSError(MP_EAGAIN); - } else if (socket->timeout != -1) { - mp_uint_t retries = socket->timeout / 100; - while (*incoming_connection == NULL) { + poll_sockets(); + MICROPY_PY_LWIP_REENTER + if (*incoming_connection != NULL) { + break; + } + if (socket_is_timedout(socket, ticks_start)) { 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_hal_delay_ms(100); - MICROPY_PY_LWIP_REENTER - } - } else { - while (*incoming_connection == NULL) { - MICROPY_PY_LWIP_EXIT - poll_sockets(); - MICROPY_PY_LWIP_REENTER } } } |
