extmod/modusocket: Allow setting timeout on unbound sockets.

For an extended state socket, if settimeout() is called before a NIC is
bound, save the timeout until the NIC is bound.
pull/8166/head
iabdalkader 2022-01-07 22:26:17 +02:00 zatwierdzone przez Damien George
rodzic b47b245c2e
commit 67420de4f4
1 zmienionych plików z 20 dodań i 7 usunięć

Wyświetl plik

@ -85,6 +85,13 @@ STATIC void socket_select_nic(mod_network_socket_obj_t *self, const byte *ip) {
if (self->nic_type->socket(self, &_errno) != 0) { if (self->nic_type->socket(self, &_errno) != 0) {
mp_raise_OSError(_errno); mp_raise_OSError(_errno);
} }
#if MICROPY_PY_USOCKET_EXTENDED_STATE
// if a timeout was set before binding a NIC, call settimeout to reset it
if (self->timeout != 0 && self->nic_type->settimeout(self, self->timeout, &_errno) != 0) {
mp_raise_OSError(_errno);
}
#endif
} }
} }
@ -317,10 +324,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_s
// otherwise, timeout is in seconds // otherwise, timeout is in seconds
STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->nic == MP_OBJ_NULL) {
// not connected
mp_raise_OSError(MP_ENOTCONN);
}
mp_uint_t timeout; mp_uint_t timeout;
if (timeout_in == mp_const_none) { if (timeout_in == mp_const_none) {
timeout = -1; timeout = -1;
@ -331,9 +334,19 @@ STATIC mp_obj_t socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
timeout = 1000 * mp_obj_get_int(timeout_in); timeout = 1000 * mp_obj_get_int(timeout_in);
#endif #endif
} }
int _errno; if (self->nic == MP_OBJ_NULL) {
if (self->nic_type->settimeout(self, timeout, &_errno) != 0) { #if MICROPY_PY_USOCKET_EXTENDED_STATE
mp_raise_OSError(_errno); // store the timeout in the socket state until a NIC is bound
self->timeout = timeout;
#else
// not connected
mp_raise_OSError(MP_ENOTCONN);
#endif
} else {
int _errno;
if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
mp_raise_OSError(_errno);
}
} }
return mp_const_none; return mp_const_none;
} }