How to use the gmqtt.mqtt.utils.unpack_variable_byte_integer function in gmqtt

To help you get started, we’ve selected a few gmqtt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wialon / gmqtt / gmqtt / mqtt / handler.py View on Github external
def _parse_properties(self, packet):
        if self.protocol_version < MQTTv50:
            # If protocol is version is less than 5.0, there is no properties in packet
            return {}, packet
        properties_len, left_packet = unpack_variable_byte_integer(packet)
        packet = left_packet[:properties_len]
        left_packet = left_packet[properties_len:]
        properties_dict = defaultdict(list)
        while packet:
            property_identifier, = struct.unpack("!B", packet[:1])
            property_obj = Property.factory(id_=property_identifier)
            if property_obj is None:
                logger.critical('[PROPERTIES] received invalid property id {}, disconnecting'.format(property_identifier))
                return None, None
            result, packet = property_obj.loads(packet[1:])
            for k, v in result.items():
                properties_dict[k].append(v)
        properties_dict = dict(properties_dict)
        return properties_dict, left_packet
github wialon / gmqtt / gmqtt / mqtt / property.py View on Github external
def loads(self, bytes_array):
        # returns dict with property name-value and remaining bytes which do not belong to this property
        if self.bytes_struct == 'u8':
            # First two bytes in UTF-8 encoded properties correspond to unicode string length
            value, left_str = unpack_utf8(bytes_array)
        elif self.bytes_struct == 'u8x2':
            value1, left_str = unpack_utf8(bytes_array)
            value2, left_str = unpack_utf8(left_str)
            value = (value1, value2)
        elif self.bytes_struct == 'b':
            str_len, = struct.unpack('!H', bytes_array[:2])
            value = bytes_array[2:2 + str_len]
            left_str = bytes_array[2 + str_len:]
        elif self.bytes_struct == 'vbi':
            value, left_str = unpack_variable_byte_integer(bytes_array)
        else:
            value, left_str = self.unpack_helper(self.bytes_struct, bytes_array)
        return {self.name: value}, left_str