async def from_stream(cls, reader: ReaderAdapter, fixed_header: MQTTFixedHeader): # protocol name protocol_name = await decode_string(reader) # protocol level protocol_level_byte = await read_or_raise(reader, 1) protocol_level = bytes_to_int(protocol_level_byte) # flags flags_byte = await read_or_raise(reader, 1) flags = bytes_to_int(flags_byte) # keep-alive keep_alive_byte = await read_or_raise(reader, 2) keep_alive = bytes_to_int(keep_alive_byte) return cls(flags, keep_alive, protocol_name, protocol_level)
async def from_stream( cls, reader: ReaderAdapter, fixed_header: MQTTFixedHeader, variable_header: MQTTVariableHeader, ): return_codes = [] bytes_to_read = fixed_header.remaining_length - variable_header.bytes_length for i in range(0, bytes_to_read): try: return_code_byte = await read_or_raise(reader, 1) return_code = bytes_to_int(return_code_byte) return_codes.append(return_code) except NoDataException: break return cls(return_codes)
async def from_stream( cls, reader: asyncio.StreamReader, fixed_header: MQTTFixedHeader, variable_header: MQTTVariableHeader, ): topics = [] payload_length = fixed_header.remaining_length - variable_header.bytes_length read_bytes = 0 while read_bytes < payload_length: try: topic = await decode_string(reader) qos_byte = await read_or_raise(reader, 1) qos = bytes_to_int(qos_byte) topics.append((topic, qos)) read_bytes += 2 + len(topic.encode("utf-8")) + 1 except NoDataException: break return cls(topics)
def test_bytes_to_int(self): ret = bytes_to_int(b"\x7f") self.assertEqual(ret, 127) ret = bytes_to_int(b"\xff\xff") self.assertEqual(ret, 65535)
async def from_stream(cls, reader: ReaderAdapter, fixed_header: MQTTFixedHeader): data = await read_or_raise(reader, 2) session_parent = data[0] & 0x01 return_code = bytes_to_int(data[1]) return cls(session_parent, return_code)