@@ -211,7 +211,11 @@ int lwipsocket_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len
211211 ret = 0 ;
212212 break ;
213213 }
214- // blocking do nothing
214+ // blocking and timed out, return with error
215+ // mbedtls_net_recv_timeout() returned with timeout
216+ else {
217+ break ;
218+ }
215219 }
216220 else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) {
217221 // printf("Close notify received\n");
@@ -278,33 +282,41 @@ int lwipsocket_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, m
278282
279283int lwipsocket_socket_settimeout (mod_network_socket_obj_t * s , mp_int_t timeout_ms , int * _errno ) {
280284 int ret ;
281- uint32_t option = lwip_fcntl_r (s -> sock_base .u .sd , F_GETFL , 0 );
282285
283- if (timeout_ms <= 0 ) {
284- if (timeout_ms == 0 ) {
285- // set non-blocking mode
286- option |= O_NONBLOCK ;
286+ if (s -> sock_base .is_ssl ) {
287+ mp_obj_ssl_socket_t * ss = (mp_obj_ssl_socket_t * )s ;
288+ // mbedtls_net_recv_timeout() API is registered with mbedtls_ssl_set_bio() so setting timeout on receive works
289+ mbedtls_ssl_conf_read_timeout (& ss -> conf , timeout_ms );
290+ }
291+ else {
292+ uint32_t option = lwip_fcntl_r (s -> sock_base .u .sd , F_GETFL , 0 );
293+
294+ if (timeout_ms <= 0 ) {
295+ if (timeout_ms == 0 ) {
296+ // set non-blocking mode
297+ option |= O_NONBLOCK ;
298+ } else {
299+ // set blocking mode
300+ option &= ~O_NONBLOCK ;
301+ timeout_ms = UINT32_MAX ;
302+ }
287303 } else {
288304 // set blocking mode
289305 option &= ~O_NONBLOCK ;
290- timeout_ms = UINT32_MAX ;
291306 }
292- } else {
293- // set blocking mode
294- option &= ~O_NONBLOCK ;
295- }
296307
297- // set the timeout
298- struct timeval tv ;
299- tv .tv_sec = timeout_ms / 1000 ; // seconds
300- tv .tv_usec = (timeout_ms % 1000 ) * 1000 ; // microseconds
301- ret = lwip_setsockopt_r (s -> sock_base .u .sd , SOL_SOCKET , SO_SNDTIMEO , & tv , sizeof (tv ));
302- ret |= lwip_setsockopt_r (s -> sock_base .u .sd , SOL_SOCKET , SO_RCVTIMEO , & tv , sizeof (tv ));
303- ret |= lwip_fcntl_r (s -> sock_base .u .sd , F_SETFL , option );
308+ // set the timeout
309+ struct timeval tv ;
310+ tv .tv_sec = timeout_ms / 1000 ; // seconds
311+ tv .tv_usec = (timeout_ms % 1000 ) * 1000 ; // microseconds
312+ ret = lwip_setsockopt_r (s -> sock_base .u .sd , SOL_SOCKET , SO_SNDTIMEO , & tv , sizeof (tv ));
313+ ret |= lwip_setsockopt_r (s -> sock_base .u .sd , SOL_SOCKET , SO_RCVTIMEO , & tv , sizeof (tv ));
314+ ret |= lwip_fcntl_r (s -> sock_base .u .sd , F_SETFL , option );
304315
305- if (ret != 0 ) {
306- * _errno = errno ;
307- return -1 ;
316+ if (ret != 0 ) {
317+ * _errno = errno ;
318+ return -1 ;
319+ }
308320 }
309321
310322 s -> sock_base .timeout = timeout_ms ;
0 commit comments