aboutsummaryrefslogtreecommitdiff
path: root/extmod/modusocket.c
diff options
context:
space:
mode:
authoriabdalkader <i.abdalkader@gmail.com>2022-01-07 22:26:17 +0200
committerDamien George <damien@micropython.org>2022-01-12 14:36:55 +1100
commit67420de4f4faa214de853dc70ef307d3571bfc28 (patch)
tree94dcaf09f5dda097a1b192b3efbf7e64ea0074a3 /extmod/modusocket.c
parentb47b245c2eeb734f69d5445372d0947f1ea43259 (diff)
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.
Diffstat (limited to 'extmod/modusocket.c')
-rw-r--r--extmod/modusocket.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/extmod/modusocket.c b/extmod/modusocket.c
index 9c2dc6fca..0335f53cb 100644
--- a/extmod/modusocket.c
+++ b/extmod/modusocket.c
@@ -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) {
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
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);
- if (self->nic == MP_OBJ_NULL) {
- // not connected
- mp_raise_OSError(MP_ENOTCONN);
- }
mp_uint_t timeout;
if (timeout_in == mp_const_none) {
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);
#endif
}
- int _errno;
- if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
- mp_raise_OSError(_errno);
+ if (self->nic == MP_OBJ_NULL) {
+ #if MICROPY_PY_USOCKET_EXTENDED_STATE
+ // 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;
}