Skip to content

Commit

Permalink
Changed format codes from %x to %s in logger. _sock_exact_recv raises…
Browse files Browse the repository at this point in the history
… OSError like socketpool does if no bytes are present and none arrive before first socket timeout. This allows loop to be non-blocking.
  • Loading branch information
dlizotte-uwo committed Feb 13, 2021
1 parent 99a6099 commit 1c0f6c8
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
if self.logger:
self.logger.debug("Sending CONNECT to broker...")
self.logger.debug(
"Fixed Header: %x\nVariable Header: %x", fixed_header, var_header
"Fixed Header: %s\nVariable Header: %s", fixed_header, var_header
)
self._sock.send(fixed_header)
self._sock.send(var_header)
Expand Down Expand Up @@ -634,7 +634,7 @@ def publish(self, topic, msg, retain=False, qos=0):

if self.logger:
self.logger.debug(
"Sending PUBLISH\nTopic: %s\nMsg: %x\
"Sending PUBLISH\nTopic: %s\nMsg: %s\
\nQoS: %d\nRetain? %r",
topic,
msg,
Expand Down Expand Up @@ -803,8 +803,7 @@ def loop(self, timeout=1):
# Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
if self.logger is not None:
self.logger.debug(
"KeepAlive period elapsed - \
requesting a PINGRESP from the server..."
"KeepAlive period elapsed - requesting a PINGRESP from the server..."
)
rcs = self.ping()
self._timestamp = 0
Expand All @@ -826,7 +825,7 @@ def _wait_for_msg(self, timeout=0.1):
res = self._sock_exact_recv(1)
except OSError as error:
if error.errno == errno.ETIMEDOUT:
# raised by a socket timeout in socketpool
# raised by a socket timeout if 0 bytes were present
return None
raise MMQTTException from error

Expand All @@ -837,7 +836,7 @@ def _wait_for_msg(self, timeout=0.1):
return None
if res[0] == MQTT_PINGRESP:
if self.logger:
self.logger.debug("Checking PINGRESP")
self.logger.debug("Got PINGRESP")
sz = self._sock_exact_recv(1)[0]
if sz != 0x00:
raise MMQTTException(
Expand Down Expand Up @@ -910,8 +909,12 @@ def _sock_exact_recv(self, bufsize):
else: # ESP32SPI Impl.
stamp = time.monotonic()
read_timeout = self.keep_alive
rc = self._sock.recv(bufsize)
to_read = bufsize - len(rc)
rc = self._sock.recv(bufsize) # This will timeout with socket timeout (not keepalive timeout)
if(not rc):
if self.logger:
self.logger.debug("_sock_exact_recv timeout")
raise OSError(errno.ETIMEDOUT) # If no bytes waiting, raise same exception as socketpool
to_read = bufsize - len(rc) # If any bytes waiting, try to read them all
assert to_read >= 0
read_timeout = self.keep_alive
while to_read > 0:
Expand Down

0 comments on commit 1c0f6c8

Please sign in to comment.