summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2020-11-30 18:39:50 -0800
committerScott Shawcroft <scott@tannewt.org>2020-11-30 18:39:50 -0800
commit927624468d3f2351826b79ca5789c81980ba0cf7 (patch)
tree0c09fd5ebbf2d39edf768cb990f08065efccd671 /ports
parent5b3c930e384ef6440f0f7b5167bb54ea68a8be76 (diff)
Two minor socket changes
* Remove BrokenPipeError and prefer to return the number of bytes received. (May be zero.) * Add two minute backup timeout to reduce the chance we hang on recv accidentally.
Diffstat (limited to 'ports')
-rw-r--r--ports/esp32s2/common-hal/socketpool/Socket.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/ports/esp32s2/common-hal/socketpool/Socket.c b/ports/esp32s2/common-hal/socketpool/Socket.c
index 92247420f..32c5fc72f 100644
--- a/ports/esp32s2/common-hal/socketpool/Socket.c
+++ b/ports/esp32s2/common-hal/socketpool/Socket.c
@@ -64,6 +64,18 @@ bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const c
} else {
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result);
}
+ } else {
+ // Connection successful, set the timeout on the underlying socket. We can't rely on the IDF
+ // to do it because the config structure is only used for TLS connections. Generally, we
+ // shouldn't hit this timeout because we try to only read available data. However, there is
+ // always a chance that we try to read something that is used internally.
+ int fd;
+ esp_tls_get_conn_sockfd(self->tcp, &fd);
+ struct timeval tv;
+ tv.tv_sec = 2 * 60; // Two minutes
+ tv.tv_usec = 0;
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+ setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
return self->connected;
@@ -123,9 +135,6 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
// socket closed
common_hal_socketpool_socket_close(self);
}
- if (status < 0) {
- mp_raise_BrokenPipeError();
- }
return received;
}