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.
This commit is contained in:
Scott Shawcroft 2020-11-30 18:39:50 -08:00
parent 5b3c930e38
commit 927624468d
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
1 changed files with 12 additions and 3 deletions

View File

@ -64,6 +64,18 @@ bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const c
} else { } else {
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result); 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; return self->connected;
@ -123,9 +135,6 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
// socket closed // socket closed
common_hal_socketpool_socket_close(self); common_hal_socketpool_socket_close(self);
} }
if (status < 0) {
mp_raise_BrokenPipeError();
}
return received; return received;
} }