Skip to content

Commit a907575

Browse files
author
brentru
committed
public command methods use shared rx buffer
1 parent 984c600 commit a907575

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def disconnect(self):
505505
self._sock.send(MQTT_DISCONNECT)
506506
if self.logger:
507507
self.logger.debug("Closing socket")
508-
self._sock.close()
508+
self._free_sockets()
509509
self._is_connected = False
510510
self._subscribed_topics = None
511511
if self.on_disconnect is not None:
@@ -516,6 +516,7 @@ def ping(self):
516516
there is an active network connection.
517517
"""
518518
self.is_connected()
519+
buf = self._rx_buffer
519520
if self.logger:
520521
self.logger.debug("Sending PINGREQ")
521522
self._sock.send(MQTT_PINGREQ)
@@ -524,8 +525,8 @@ def ping(self):
524525
while True:
525526
op = self._wait_for_msg()
526527
if op == 208:
527-
ping_resp = self._sock.recv(2)
528-
if ping_resp[0] != 0x00:
528+
self._recv_into(buf, 2)
529+
if buf[0] != 0x00:
529530
raise MMQTTException("PINGRESP not returned from broker.")
530531
return
531532

@@ -579,6 +580,7 @@ def publish(self, topic, msg, retain=False, qos=0):
579580
0 <= qos <= 1
580581
), "Quality of Service Level 2 is unsupported by this library."
581582

583+
buf = self._rx_buffer
582584
# fixed header. [3.3.1.2], [3.3.1.3]
583585
pub_hdr_fixed = bytearray([0x30 | retain | qos << 1])
584586

@@ -621,13 +623,14 @@ def publish(self, topic, msg, retain=False, qos=0):
621623
while True:
622624
op = self._wait_for_msg()
623625
if op == 0x40:
624-
sz = self._sock.recv(1)
625-
assert sz == b"\x02"
626-
rcv_pid = self._sock.recv(2)
627-
rcv_pid = rcv_pid[0] << 0x08 | rcv_pid[1]
628-
if pid == rcv_pid:
626+
self._recv_into(buf, 1)
627+
assert buf == b"\x02"
628+
# rcv_pid = self._sock.recv(2)
629+
self._recv_into(buf, 2)
630+
buf = buf[0] << 0x08 | buf[1]
631+
if pid == buf:
629632
if self.on_publish is not None:
630-
self.on_publish(self, self._user_data, topic, rcv_pid)
633+
self.on_publish(self, self._user_data, topic, buf)
631634
return
632635

633636
def subscribe(self, topic, qos=0):
@@ -686,6 +689,8 @@ def subscribe(self, topic, qos=0):
686689
self._check_qos(q)
687690
self._check_topic(t)
688691
topics.append((t, q))
692+
# Rx buffer
693+
buf = self._rx_buffer
689694
# Assemble packet
690695
packet_length = 2 + (2 * len(topics)) + (1 * len(topics))
691696
packet_length += sum(len(topic) for topic, qos in topics)
@@ -707,9 +712,9 @@ def subscribe(self, topic, qos=0):
707712
while True:
708713
op = self._wait_for_msg()
709714
if op == 0x90:
710-
rc = self._sock.recv(4)
711-
assert rc[1] == packet[2] and rc[2] == packet[3]
712-
if rc[3] == 0x80:
715+
self._recv_into(buf, 4)
716+
assert buf[1] == packet[2] and buf[2] == packet[3]
717+
if buf[3] == 0x80:
713718
raise MMQTTException("SUBACK Failure!")
714719
for t, q in topics:
715720
if self.on_subscribe is not None:
@@ -751,6 +756,8 @@ def unsubscribe(self, topic):
751756
raise MMQTTException(
752757
"Topic must be subscribed to before attempting unsubscribe."
753758
)
759+
# Rx buffer
760+
buf = self._rx_buffer
754761
# Assemble packet
755762
packet_length = 2 + (2 * len(topics))
756763
packet_length += sum(len(topic) for topic in topics)
@@ -770,12 +777,12 @@ def unsubscribe(self, topic):
770777
while True:
771778
op = self._wait_for_msg()
772779
if op == 176:
773-
return_code = self._sock.recv(3)
774-
assert return_code[0] == 0x02
780+
self._recv_into(buf, 3)
781+
assert buf[0] == 0x02
775782
# [MQTT-3.32]
776783
assert (
777-
return_code[1] == packet_id_bytes[0]
778-
and return_code[2] == packet_id_bytes[1]
784+
buf[1] == packet_id_bytes[0]
785+
and buf[2] == packet_id_bytes[1]
779786
)
780787
for t in topics:
781788
if self.on_unsubscribe is not None:

0 commit comments

Comments
 (0)