Skip to content

Commit b0b4418

Browse files
authored
Merge pull request #69 from dlizotte-uwo/master
Make loop() in esp32spi implementation non-blocking
2 parents 85d9d84 + 3ab67bb commit b0b4418

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
510510
if self.logger:
511511
self.logger.debug("Sending CONNECT to broker...")
512512
self.logger.debug(
513-
"Fixed Header: %x\nVariable Header: %x", fixed_header, var_header
513+
"Fixed Header: %s\nVariable Header: %s", fixed_header, var_header
514514
)
515515
self._sock.send(fixed_header)
516516
self._sock.send(var_header)
@@ -634,7 +634,7 @@ def publish(self, topic, msg, retain=False, qos=0):
634634

635635
if self.logger:
636636
self.logger.debug(
637-
"Sending PUBLISH\nTopic: %s\nMsg: %x\
637+
"Sending PUBLISH\nTopic: %s\nMsg: %s\
638638
\nQoS: %d\nRetain? %r",
639639
topic,
640640
msg,
@@ -803,8 +803,7 @@ def loop(self, timeout=1):
803803
# Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
804804
if self.logger is not None:
805805
self.logger.debug(
806-
"KeepAlive period elapsed - \
807-
requesting a PINGRESP from the server..."
806+
"KeepAlive period elapsed - requesting a PINGRESP from the server..."
808807
)
809808
rcs = self.ping()
810809
self._timestamp = 0
@@ -826,7 +825,7 @@ def _wait_for_msg(self, timeout=0.1):
826825
res = self._sock_exact_recv(1)
827826
except OSError as error:
828827
if error.errno == errno.ETIMEDOUT:
829-
# raised by a socket timeout in socketpool
828+
# raised by a socket timeout if 0 bytes were present
830829
return None
831830
raise MMQTTException from error
832831

@@ -837,7 +836,7 @@ def _wait_for_msg(self, timeout=0.1):
837836
return None
838837
if res[0] == MQTT_PINGRESP:
839838
if self.logger:
840-
self.logger.debug("Checking PINGRESP")
839+
self.logger.debug("Got PINGRESP")
841840
sz = self._sock_exact_recv(1)[0]
842841
if sz != 0x00:
843842
raise MMQTTException(
@@ -910,7 +909,15 @@ def _sock_exact_recv(self, bufsize):
910909
else: # ESP32SPI Impl.
911910
stamp = time.monotonic()
912911
read_timeout = self.keep_alive
912+
# This will timeout with socket timeout (not keepalive timeout)
913913
rc = self._sock.recv(bufsize)
914+
if not rc:
915+
if self.logger:
916+
self.logger.debug("_sock_exact_recv timeout")
917+
# If no bytes waiting, raise same exception as socketpool
918+
raise OSError(errno.ETIMEDOUT)
919+
# If any bytes waiting, try to read them all,
920+
# or raise exception if wait longer than read_timeout
914921
to_read = bufsize - len(rc)
915922
assert to_read >= 0
916923
read_timeout = self.keep_alive

0 commit comments

Comments
 (0)