def _yield_messages(self, sock): try: buffer = UnpackableBuffer() unpacker = Unpacker(buffer) chunk_size = 0 while True: while chunk_size == 0: # Determine the chunk size and skip noop buffer.receive(sock, 2) chunk_size = buffer.pop_u16() if chunk_size == 0: log.debug("[#%04X] S: <NOOP>", sock.getsockname()[1]) buffer.receive(sock, chunk_size + 2) chunk_size = buffer.pop_u16() if chunk_size == 0: # chunk_size was the end marker for the message size, tag = unpacker.unpack_structure_header() fields = [unpacker.unpack() for _ in range(size)] yield tag, fields # Reset for new message unpacker.reset() except OSError as error: self.on_error(error)
def _yield_messages(self, sock): try: buffer = UnpackableBuffer() chunk_loader = self._load_chunks(sock, buffer) unpacker = Unpacker(buffer) details = [] while True: unpacker.reset() details[:] = () chunk_size = -1 while chunk_size != 0: chunk_size = next(chunk_loader) summary_signature = None summary_metadata = None size, signature = unpacker.unpack_structure_header() if size > 1: raise ProtocolError("Expected one field") if signature == b"\x71": data = unpacker.unpack() details.append(data) else: summary_signature = signature summary_metadata = unpacker.unpack_map() yield details, summary_signature, summary_metadata except OSError as error: self.on_error(error)
def assert_packable(cls, value, packed_value): stream_out = BytesIO() packer = Packer(stream_out) packer.pack(value) packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value %r is %r instead of expected %r" % (value, packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == value except AssertionError: raise AssertionError("Unpacked value %r is not equal to original %r" % (unpacked, value))
def _yield_messages(self, sock): try: buffer = UnpackableBuffer() chunk_loader = self._load_chunks(sock, buffer) unpacker = Unpacker(buffer) while True: unpacker.reset() chunk_size = -1 while chunk_size != 0: chunk_size = next(chunk_loader) size, tag = unpacker.unpack_structure_header() fields = [unpacker.unpack() for _ in range(size)] yield tag, fields except OSError as error: self.on_error(error)
def pop_message(self): data = bytearray() while True: chunk = self.pop_chunk() print("CHUNK %r" % chunk) if chunk: data.extend(chunk) elif data: break # end of message else: continue # NOOP header = data[0] n_fields = header % 0x10 tag = data[1] buffer = UnpackableBuffer(data[2:]) unpacker = Unpacker(buffer) fields = [unpacker.unpack() for _ in range(n_fields)] return tag, fields
def test_list_stream(self): packed_value = b"\xD7\x01\x02\x03\xDF" unpacked_value = [1, 2, 3] stream_out = BytesIO() packer = Packer(stream_out) packer.pack_list_stream_header() packer.pack(1) packer.pack(2) packer.pack(3) packer.pack_end_of_stream() packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value is %r instead of expected %r" % (packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == unpacked_value except AssertionError: raise AssertionError("Unpacked value %r is not equal to expected %r" % (unpacked, unpacked_value))
def test_map_stream(self): packed_value = b"\xDB\x81A\x01\x81B\x02\xDF" unpacked_value = {u"A": 1, u"B": 2} stream_out = BytesIO() packer = Packer(stream_out) packer.pack_map_stream_header() packer.pack(u"A") packer.pack(1) packer.pack(u"B") packer.pack(2) packer.pack_end_of_stream() packed = stream_out.getvalue() try: assert packed == packed_value except AssertionError: raise AssertionError("Packed value is %r instead of expected %r" % (packed, packed_value)) unpacked = Unpacker(UnpackableBuffer(packed)).unpack() try: assert unpacked == unpacked_value except AssertionError: raise AssertionError("Unpacked value %r is not equal to expected %r" % (unpacked, unpacked_value))