def test_pack(): from pyec.packet.varint import pack assert pack(5030) == b'NM' assert pack(32) == b'A' assert pack(512) == b'\x08\x01' assert pack(5123) == b'P\x07'
def test_round_trip(): from pyec.packet.varint import pack, unpack_bytes import random for _ in range(20): x = random.randrange(100000) assert unpack_bytes(pack(x))[0] == x
def serialize(self): """Packets serialize to the format: pid + data Where pid is the ld-varint-encoded packet id. The data is composed of fields.""" pid = PacketMeta.REGISTRY.get_id(self.__class__) data = [] for field in self._fields: f = self.__class__.__dict__[field] val = getattr(self, field) data.append(f._serialize(val)) data = b''.join(data) return pack(len(pid)) + pid.encode() + data
def _serialize(self, inst): data = self._ld_serialize(inst) return pack(len(data)) + data
def _serialize(self, inst): return pack(inst)