Skip to content

Commit 1c0f6c8

Browse files
committed
Changed format codes from %x to %s in logger. _sock_exact_recv raises OSError like socketpool does if no bytes are present and none arrive before first socket timeout. This allows loop to be non-blocking.
1 parent 99a6099 commit 1c0f6c8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 11 additions & 8 deletions
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,8 +909,12 @@ def _sock_exact_recv(self, bufsize):
910909
else: # ESP32SPI Impl.
911910
stamp = time.monotonic()
912911
read_timeout = self.keep_alive
913-
rc = self._sock.recv(bufsize)
914-
to_read = bufsize - len(rc)
912+
rc = self._sock.recv(bufsize) # This will timeout with socket timeout (not keepalive timeout)
913+
if(not rc):
914+
if self.logger:
915+
self.logger.debug("_sock_exact_recv timeout")
916+
raise OSError(errno.ETIMEDOUT) # If no bytes waiting, raise same exception as socketpool
917+
to_read = bufsize - len(rc) # If any bytes waiting, try to read them all
915918
assert to_read >= 0
916919
read_timeout = self.keep_alive
917920
while to_read > 0:

0 commit comments

Comments
 (0)