def deconstruct_datagram(cls, datagram: bytes) -> tuple: """Return a tuple containing the header and the rest of the datagram. # Arguments datagram (bytes): serialized PyGaSe package to deconstruct # Returns tuple: `(header, payload)` with `payload` being a bytestring of the rest of the datagram """ if datagram[:4] != PROTOCOL_ID: raise ProtocolIDMismatchError sequence = Sqn.from_sqn_bytes(datagram[4:6]) ack = Sqn.from_sqn_bytes(datagram[6:8]) ack_bitfield = bin(int.from_bytes(datagram[8:12], "big"))[2:].zfill(32) payload = datagram[12:] return (cls(sequence, ack, ack_bitfield), payload)
def from_datagram(cls, datagram: bytes) -> "ClientPackage": """Override #Package.from_datagram to include `time_order`.""" header, payload = Header.deconstruct_datagram(datagram) time_order = Sqn.from_sqn_bytes(payload[:2]) payload = payload[2:] events = cls._read_out_event_block(payload) result = cls(header, time_order, events) result._datagram = datagram # pylint: disable=protected-access return result
def test_bytes(self): for i in range(2 * Sqn._bytesize): b = Sqn(i).to_sqn_bytes() assert len(b) == Sqn._bytesize assert b.__class__ == bytes assert Sqn.from_sqn_bytes(b) == Sqn(i)