@@ -352,7 +352,7 @@ def accept(
352
352
wiznet5k .adafruit_wiznet5k .SNSR_SOCK_SYNRECV ,
353
353
wiznet5k .adafruit_wiznet5k .SNSR_SOCK_ESTABLISHED ,
354
354
):
355
- if 0 < self ._timeout < time .monotonic () - stamp :
355
+ if self . _timeout and 0 < self ._timeout < time .monotonic () - stamp :
356
356
raise TimeoutError ("Failed to accept connection." )
357
357
if self ._status == wiznet5k .adafruit_wiznet5k .SNSR_SOCK_CLOSED :
358
358
self .close ()
@@ -403,8 +403,6 @@ def send(self, data: Union[bytes, bytearray]) -> int:
403
403
gc .collect ()
404
404
return bytes_sent
405
405
406
- # def sendto(self, data: bytearray, address: [Tuple[str, int]]) -> int:
407
-
408
406
def sendto (self , data : bytearray , * flags_and_or_address : any ) -> int :
409
407
"""
410
408
Connect to a remote socket and send data.
@@ -441,12 +439,11 @@ def recv(
441
439
442
440
:returns bytes: Data from the socket.
443
441
"""
444
- stop_time = time .monotonic () + self ._timeout
445
- if self ._timeout != 0.0 :
446
- while not self ._available ():
447
- if self ._timeout is not None and time .monotonic () > stop_time :
448
- break
449
- time .sleep (0.05 )
442
+ stamp = time .monotonic ()
443
+ while not self ._available ():
444
+ if self ._timeout and 0 < self ._timeout < time .monotonic () - stamp :
445
+ break
446
+ time .sleep (0.05 )
450
447
bytes_on_socket = self ._available ()
451
448
if not bytes_on_socket :
452
449
return b""
@@ -561,9 +558,13 @@ def _readline(self) -> bytes:
561
558
self ._buffer += _the_interface .socket_read (self ._socknum , avail )[1 ]
562
559
elif self ._sock_type == SOCK_DGRAM :
563
560
self ._buffer += _the_interface .read_udp (self ._socknum , avail )[1 ]
564
- if not avail and 0 < self ._timeout < time .monotonic () - stamp :
565
- self .close ()
566
- raise RuntimeError ("Didn't receive response, failing out..." )
561
+ if (
562
+ self ._timeout
563
+ and not avail
564
+ and 0 < self ._timeout < time .monotonic () - stamp
565
+ ):
566
+ self .close ()
567
+ raise RuntimeError ("Didn't receive response, failing out..." )
567
568
firstline , self ._buffer = self ._buffer .split (b"\r \n " , 1 )
568
569
gc .collect ()
569
570
return firstline
@@ -585,17 +586,22 @@ def _available(self) -> int:
585
586
"""
586
587
return _the_interface .socket_available (self ._socknum , self ._sock_type )
587
588
588
- def settimeout (self , value : float ) -> None :
589
+ def settimeout (self , value : Optional [ float ] ) -> None :
589
590
"""
590
- Set the socket read timeout.
591
-
592
- :param float value: Socket read timeout in seconds.
591
+ Set a timeout on blocking socket operations. The value argument can be a
592
+ non-negative floating point number expressing seconds, or None. If a non-zero
593
+ value is given, subsequent socket operations will raise a timeout exception
594
+ if the timeout period value has elapsed before the operation has completed.
595
+ If zero is given, the socket is put in non-blocking mode. If None is given,
596
+ the socket is put in blocking mode..
593
597
598
+ :param fOptional[float] value: Socket read timeout in seconds.
594
599
"""
595
600
# TODO: Implement None and 0.0 as valid once all socket funcs can handle them.
596
- if value < 0 :
597
- raise ValueError ("Timeout period should be non-negative." )
598
- self ._timeout = value
601
+ if value is None or (isinstance (value , (int , float )) and value >= 0 ):
602
+ self ._timeout = value
603
+ else :
604
+ raise ValueError ("Timeout must be None, 0.0 or a positive numeric value." )
599
605
600
606
def gettimeout (self ) -> Optional [float ]:
601
607
"""
0 commit comments