Skip to content

Commit

Permalink
finish partial reads in CPython
Browse files Browse the repository at this point in the history
fixes #131
  • Loading branch information
vladak committed Dec 5, 2022
1 parent d60ab23 commit c334c81
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,15 +956,29 @@ def _sock_exact_recv(self, bufsize):
bytes is returned or trigger a timeout exception.
:param int bufsize: number of bytes to receive
:return: byte array
"""
stamp = time.monotonic()
if not self._backwards_compatible_sock:
# CPython/Socketpool Impl.
rc = bytearray(bufsize)
self._sock.recv_into(rc, bufsize)
else: # ESP32SPI Impl.
stamp = time.monotonic()
mv = memoryview(rc)
recv = self._sock.recv_into(rc, bufsize)
to_read = bufsize - recv
assert to_read >= 0
read_timeout = self.keep_alive
mv = mv[recv:]
while to_read > 0:
recv = self._sock.recv_into(mv, to_read)
to_read -= recv
mv = mv[recv:]
if time.monotonic() - stamp > read_timeout:
raise MMQTTException(
"Unable to receive {} bytes within {} seconds.".format(
to_read, read_timeout
)
)
else: # ESP32SPI Impl.
# This will timeout with socket timeout (not keepalive timeout)
rc = self._sock.recv(bufsize)
if not rc:
Expand Down

0 comments on commit c334c81

Please sign in to comment.