Skip to content

Commit 4a5965b

Browse files
committed
umqtt.simple: publish: Do proper varlen message size encoding.
Using 2 static bytes for encoding didn't work well with some brokers, e.g. Amazon AWS. Also, extend max message size to 2M.
1 parent 38885fb commit 4a5965b

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

umqtt.simple/umqtt/simple.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,20 @@ def ping(self):
5555
self.sock.close()
5656

5757
def publish(self, topic, msg, retain=False, qos=0):
58-
pkt = bytearray(b"\x30\0\0")
58+
pkt = bytearray(b"\x30\0\0\0")
5959
pkt[0] |= qos << 1 | retain
6060
sz = 2 + len(topic) + len(msg)
6161
if qos > 0:
6262
sz += 2
63-
assert sz <= 16383
64-
pkt[1] = (sz & 0x7f) | 0x80
65-
pkt[2] = sz >> 7
63+
assert sz < 2097152
64+
i = 1
65+
while sz > 0x7f:
66+
pkt[i] = (sz & 0x7f) | 0x80
67+
sz >>= 7
68+
i += 1
69+
pkt[i] = sz
6670
#print(hex(len(pkt)), hexlify(pkt, ":"))
67-
self.sock.write(pkt)
71+
self.sock.write(pkt, i + 1)
6872
self._send_str(topic)
6973
if qos > 0:
7074
self.pid += 1

0 commit comments

Comments
 (0)