From 1c0f6c80b203573ae87d280c81d072317af2b747 Mon Sep 17 00:00:00 2001 From: Dan Lizotte Date: Sat, 13 Feb 2021 09:59:57 -0500 Subject: [PATCH 1/3] 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. --- adafruit_minimqtt/adafruit_minimqtt.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index aecde99..2f76715 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -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) @@ -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, @@ -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 @@ -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 @@ -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( @@ -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: From 9791e9dbe530d016380d316ef300cee62d9529da Mon Sep 17 00:00:00 2001 From: Dan Lizotte Date: Sat, 13 Feb 2021 10:49:03 -0500 Subject: [PATCH 2/3] Fixed long comment lines. --- adafruit_minimqtt/adafruit_minimqtt.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 2f76715..e8fbc74 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -909,12 +909,16 @@ def _sock_exact_recv(self, bufsize): else: # ESP32SPI Impl. stamp = time.monotonic() read_timeout = self.keep_alive - rc = self._sock.recv(bufsize) # This will timeout with socket timeout (not keepalive timeout) + # This will timeout with socket timeout (not keepalive timeout) + rc = self._sock.recv(bufsize) 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 + # If no bytes waiting, raise same exception as socketpool + raise OSError(errno.ETIMEDOUT) + # If any bytes waiting, try to read them all, + # or raise exception if wait longer than read_timeout + to_read = bufsize - len(rc) assert to_read >= 0 read_timeout = self.keep_alive while to_read > 0: From 3ab67bb6e4084189792d4246e9645761b849f987 Mon Sep 17 00:00:00 2001 From: Dan Lizotte Date: Fri, 19 Feb 2021 11:52:48 -0500 Subject: [PATCH 3/3] fixed superfluous-parens --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index e8fbc74..f5454de 100755 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -911,7 +911,7 @@ def _sock_exact_recv(self, bufsize): read_timeout = self.keep_alive # This will timeout with socket timeout (not keepalive timeout) rc = self._sock.recv(bufsize) - if(not rc): + if not rc: if self.logger: self.logger.debug("_sock_exact_recv timeout") # If no bytes waiting, raise same exception as socketpool