Skip to content

Commit

Permalink
Handle disconnect packet.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed May 8, 2019
1 parent 5f7fbad commit 28067c6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions mqttools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,33 @@ def pack_disconnect():
return packed


def unpack_disconnect(payload):
if payload.is_data_available():
reason = payload.read(1)[0]
else:
reason = 0

try:
reason = DisconnectReasonCode(reason)
except ValueError:
pass

if payload.is_data_available():
properties = unpack_properties(
'DISCONNECT',
[
PropertyIds.SESSION_EXPIRY_INTERVAL,
PropertyIds.SERVER_REFERENCE,
PropertyIds.REASON_STRING,
PropertyIds.USER_PROPERTY
],
payload)
else:
properties = {}

return reason, properties


def pack_subscribe(topic, qos, packet_identifier):
packed_topic = pack_string(topic)
packed = pack_fixed_header(ControlPacketType.SUBSCRIBE,
Expand Down Expand Up @@ -1376,6 +1403,20 @@ def on_unsuback(self, payload):
def on_pingresp(self):
self._pingresp_event.set()

def on_disconnect(self, payload):
try:
reason, properties = unpack_disconnect(payload)
except MalformedPacketError:
LOGGER.debug('Discarding malformed DISCONNECT packet.')
return

if reason != DisconnectReasonCode.NORMAL_DISCONNECTION:
LOGGER.info("Abnormal disconnect reason %s.", reason)

if PropertyIds.REASON_STRING in properties:
reason_string = properties[PropertyIds.REASON_STRING]
LOGGER.info("Disconnect reason string '%s'.", reason_string)

async def reader_loop(self):
while True:
packet_type, flags, payload = await self._read_packet()
Expand All @@ -1398,6 +1439,8 @@ async def reader_loop(self):
self.on_unsuback(payload)
elif packet_type == ControlPacketType.PINGRESP:
self.on_pingresp()
elif packet_type == ControlPacketType.DISCONNECT:
self.on_disconnect()
else:
LOGGER.warning("Unsupported packet type %s with data %s.",
control_packet_type_to_string(packet_type),
Expand Down

0 comments on commit 28067c6

Please sign in to comment.