Skip to content

Commit b7650a3

Browse files
author
BiffoBear
committed
Implemented timeout is None makes operations blocking.
1 parent 0d91240 commit b7650a3

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,6 @@ def socket_write(
978978
"""
979979
assert self.link_status, "Ethernet cable disconnected!"
980980
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
981-
status = 0
982-
ret = 0
983-
free_size = 0
984981
if len(buffer) > SOCK_SIZE:
985982
ret = SOCK_SIZE
986983
else:

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def accept(
352352
wiznet5k.adafruit_wiznet5k.SNSR_SOCK_SYNRECV,
353353
wiznet5k.adafruit_wiznet5k.SNSR_SOCK_ESTABLISHED,
354354
):
355-
if 0 < self._timeout < time.monotonic() - stamp:
355+
if self._timeout and 0 < self._timeout < time.monotonic() - stamp:
356356
raise TimeoutError("Failed to accept connection.")
357357
if self._status == wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED:
358358
self.close()
@@ -403,8 +403,6 @@ def send(self, data: Union[bytes, bytearray]) -> int:
403403
gc.collect()
404404
return bytes_sent
405405

406-
# def sendto(self, data: bytearray, address: [Tuple[str, int]]) -> int:
407-
408406
def sendto(self, data: bytearray, *flags_and_or_address: any) -> int:
409407
"""
410408
Connect to a remote socket and send data.
@@ -441,12 +439,11 @@ def recv(
441439
442440
:returns bytes: Data from the socket.
443441
"""
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)
450447
bytes_on_socket = self._available()
451448
if not bytes_on_socket:
452449
return b""
@@ -561,9 +558,13 @@ def _readline(self) -> bytes:
561558
self._buffer += _the_interface.socket_read(self._socknum, avail)[1]
562559
elif self._sock_type == SOCK_DGRAM:
563560
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...")
567568
firstline, self._buffer = self._buffer.split(b"\r\n", 1)
568569
gc.collect()
569570
return firstline
@@ -585,17 +586,22 @@ def _available(self) -> int:
585586
"""
586587
return _the_interface.socket_available(self._socknum, self._sock_type)
587588

588-
def settimeout(self, value: float) -> None:
589+
def settimeout(self, value: Optional[float]) -> None:
589590
"""
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..
593597
598+
:param fOptional[float] value: Socket read timeout in seconds.
594599
"""
595600
# 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.")
599605

600606
def gettimeout(self) -> Optional[float]:
601607
"""

0 commit comments

Comments
 (0)