@@ -57,20 +57,31 @@ def connect(self, clean_session=True):
57
57
if self .ssl :
58
58
import ussl
59
59
self .sock = ussl .wrap_socket (self .sock , ** self .ssl_params )
60
- msg = bytearray (b"\x10 \0 \0 \x04 MQTT\x04 \x02 \0 \0 " )
61
- msg [1 ] = 10 + 2 + len (self .client_id )
62
- msg [9 ] = clean_session << 1
60
+ premsg = bytearray (b"\x10 \0 \0 \0 \0 \0 " )
61
+ msg = bytearray (b"\x04 MQTT\x04 \x02 \0 \0 " )
62
+
63
+ sz = 10 + 2 + len (self .client_id )
64
+ msg [6 ] = clean_session << 1
63
65
if self .user is not None :
64
- msg [ 1 ] += 2 + len (self .user ) + 2 + len (self .pswd )
65
- msg [9 ] |= 0xC0
66
+ sz += 2 + len (self .user ) + 2 + len (self .pswd )
67
+ msg [6 ] |= 0xC0
66
68
if self .keepalive :
67
69
assert self .keepalive < 65536
68
- msg [10 ] |= self .keepalive >> 8
69
- msg [11 ] |= self .keepalive & 0x00FF
70
+ msg [7 ] |= self .keepalive >> 8
71
+ msg [8 ] |= self .keepalive & 0x00FF
70
72
if self .lw_topic :
71
- msg [1 ] += 2 + len (self .lw_topic ) + 2 + len (self .lw_msg )
72
- msg [9 ] |= 0x4 | (self .lw_qos & 0x1 ) << 3 | (self .lw_qos & 0x2 ) << 3
73
- msg [9 ] |= self .lw_retain << 5
73
+ sz += 2 + len (self .lw_topic ) + 2 + len (self .lw_msg )
74
+ msg [6 ] |= 0x4 | (self .lw_qos & 0x1 ) << 3 | (self .lw_qos & 0x2 ) << 3
75
+ msg [6 ] |= self .lw_retain << 5
76
+
77
+ i = 1
78
+ while sz > 0x7f :
79
+ premsg [i ] = (sz & 0x7f ) | 0x80
80
+ sz >>= 7
81
+ i += 1
82
+ premsg [i ] = sz
83
+
84
+ self .sock .write (premsg , i + 2 )
74
85
self .sock .write (msg )
75
86
#print(hex(len(msg)), hexlify(msg, ":"))
76
87
self ._send_str (self .client_id )
0 commit comments