Skip to content

Commit c334c81

Browse files
committed
finish partial reads in CPython
fixes #131
1 parent d60ab23 commit c334c81

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -956,15 +956,29 @@ def _sock_exact_recv(self, bufsize):
956956
bytes is returned or trigger a timeout exception.
957957
958958
:param int bufsize: number of bytes to receive
959-
959+
:return: byte array
960960
"""
961+
stamp = time.monotonic()
961962
if not self._backwards_compatible_sock:
962963
# CPython/Socketpool Impl.
963964
rc = bytearray(bufsize)
964-
self._sock.recv_into(rc, bufsize)
965-
else: # ESP32SPI Impl.
966-
stamp = time.monotonic()
965+
mv = memoryview(rc)
966+
recv = self._sock.recv_into(rc, bufsize)
967+
to_read = bufsize - recv
968+
assert to_read >= 0
967969
read_timeout = self.keep_alive
970+
mv = mv[recv:]
971+
while to_read > 0:
972+
recv = self._sock.recv_into(mv, to_read)
973+
to_read -= recv
974+
mv = mv[recv:]
975+
if time.monotonic() - stamp > read_timeout:
976+
raise MMQTTException(
977+
"Unable to receive {} bytes within {} seconds.".format(
978+
to_read, read_timeout
979+
)
980+
)
981+
else: # ESP32SPI Impl.
968982
# This will timeout with socket timeout (not keepalive timeout)
969983
rc = self._sock.recv(bufsize)
970984
if not rc:

0 commit comments

Comments
 (0)