Exemplo n.º 1
0
    def get_message_length(self, buff, serializer):
        if getattr(self, "_length_field_", None) is None:
            return len(buff)

        serializer = serializer or self._serializer_
        buf_len = len(buff)
        length_field = self._length_field_
        start_pos = offset_of(self, length_field, serializer)
        end_pos = start_pos + field_size_of(self, length_field, serializer)
        if buf_len < end_pos:
            return None

        value = self
        field_type = None
        attr_parts = length_field.split(".")
        for attr_part in attr_parts[:-1]:
            value = getattr(self, attr_part)
        for field_info in value.get_struct().get_fields():
            if field_info.name == attr_parts[-1]:
                field_type = field_info.type
                break
        else:
            raise RuntimeError('length field named "%s" not found' %
                               length_field)
        expected_len, _ = serializer.loads(field_type, buff[start_pos:end_pos])
        if buf_len < expected_len:
            return None
        return expected_len
Exemplo n.º 2
0
 def test_field_size_of_array(self):
     self.assertEqual(field_size_of(self.foo, "uint32_list", BinarySerializer()), 20)
     self.assertEqual(field_size_of(self.foo, "string_list", BinarySerializer()), 14)
Exemplo n.º 3
0
 def test_field_size_of_number(self):
     self.assertEqual(field_size_of(self.foo, "version", BinarySerializer()), 4)
     self.assertEqual(field_size_of(self.foo, "timestamp", BinarySerializer()), 4)
     self.assertEqual(field_size_of(self.foo, "double_data", BinarySerializer()), 8)
Exemplo n.º 4
0
 def test_field_size_of_string(self):
     self.foo.dumps(BinarySerializer())
     self.assertEqual(field_size_of(self.foo, "cmd", BinarySerializer()), 16)
     self.assertEqual(field_size_of(self.foo, "username", BinarySerializer()), 4)
Exemplo n.º 5
0
 def test_size_of_nested(self):
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.len", BinarySerializer()), 1)
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.string", BinarySerializer()), 7)
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string", BinarySerializer()), 8)