Skip to content

Use socket.recv #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def connect(self, clean_session=True):
while True:
op = self._wait_for_msg()
if op == 32:
rc = self._sock.read(3)
rc = self._sock.recv(3)
assert rc[0] == 0x02
if rc[2] != 0x00:
raise MMQTTException(CONNACK_ERRORS[rc[2]])
Expand Down Expand Up @@ -313,7 +313,7 @@ def ping(self):
while True:
op = self._wait_for_msg(0.5)
if op == 208:
ping_resp = self._sock.read(2)
ping_resp = self._sock.recv(2)
if ping_resp[0] != 0x00:
raise MMQTTException('PINGRESP not returned from broker.')
return
Expand Down Expand Up @@ -394,9 +394,9 @@ def publish(self, topic, msg, retain=False, qos=0):
while True:
op = self._wait_for_msg()
if op == 0x40:
sz = self._sock.read(1)
sz = self._sock.recv(1)
assert sz == b"\x02"
rcv_pid = self._sock.read(2)
rcv_pid = self._sock.recv(2)
rcv_pid = rcv_pid[0] << 0x08 | rcv_pid[1]
if pid == rcv_pid:
if self.on_publish is not None:
Expand Down Expand Up @@ -472,7 +472,7 @@ def subscribe(self, topic, qos=0):
while True:
op = self._wait_for_msg()
if op == 0x90:
rc = self._sock.read(4)
rc = self._sock.recv(4)
assert rc[1] == packet[2] and rc[2] == packet[3]
if rc[3] == 0x80:
raise MMQTTException('SUBACK Failure!')
Expand Down Expand Up @@ -529,7 +529,7 @@ def unsubscribe(self, topic):
while True:
op = self._wait_for_msg()
if op == 176:
return_code = self._sock.read(3)
return_code = self._sock.recv(3)
assert return_code[0] == 0x02
# [MQTT-3.32]
assert return_code[1] == packet_id_bytes[0] and return_code[2] == packet_id_bytes[1]
Expand Down Expand Up @@ -640,27 +640,27 @@ def _wait_for_msg(self, timeout=30):
"""Reads and processes network events.
Returns response code if successful.
"""
res = self._sock.read(1)
res = self._sock.recv(1)
self._sock.settimeout(timeout)
if res in [None, b""]:
return None
if res == MQTT_PINGRESP:
sz = self._sock.read(1)[0]
sz = self._sock.recv(1)[0]
assert sz == 0
return None
if res[0] & 0xf0 != 0x30:
return res[0]
sz = self._recv_len()
topic_len = self._sock.read(2)
topic_len = self._sock.recv(2)
topic_len = (topic_len[0] << 8) | topic_len[1]
topic = self._sock.read(topic_len)
topic = self._sock.recv(topic_len)
topic = str(topic, 'utf-8')
sz -= topic_len + 2
if res[0] & 0x06:
pid = self._sock.read(2)
pid = self._sock.recv(2)
pid = pid[0] << 0x08 | pid[1]
sz -= 0x02
msg = self._sock.read(sz)
msg = self._sock.recv(sz)
if self.on_message is not None:
self.on_message(self, topic, str(msg, 'utf-8'))
if res[0] & 0x06 == 0x02:
Expand All @@ -675,7 +675,7 @@ def _recv_len(self):
n = 0
sh = 0
while True:
b = self._sock.read(1)[0]
b = self._sock.recv(1)[0]
n |= (b & 0x7f) << sh
if not b & 0x80:
return n
Expand Down