def to_binary(self): buffer = b"" buffer += chr((self.reliability << self.RELIABILITY_SHIFT) | ( self.SPLIT_FLAG if self.has_split else 0)).encode("UTF-8") buffer += Binary.write_short(len(self.buffer) << 3) buffer += Binary.write_l_triad( self.message_index) if PacketReliability.is_reliable( self.reliability) else b"" buffer += Binary.write_l_triad( self.sequence_index) if PacketReliability.is_sequenced( self.reliability) else b"" buffer += Binary.write_l_triad(self.order_index + chr( self.order_channel)) if PacketReliability.is_sequenced_or_ordered( self.reliability) else b"" buffer += Binary.write_int(self.split_count) + Binary.write_short( self.split_id) + Binary.write_int( self.split_index) if self.has_split else b"" return buffer + self.buffer
def _encodePayload(self) -> None: payload = b"" self.packets.sort(key=int) count = len(self.packets) records = 0 if count > 0: pointer = 1 start = self.packets[0] last = self.packets[0] while pointer < count: pointer += 1 current = self.packets[pointer] diff = current - last if diff == 1: last = current elif diff > 1: if start is last: payload += chr(self.RECORD_TYPE_SINGLE) payload += Binary.write_l_triad(start) start = last = current else: payload += chr(self.RECORD_TYPE_RANGE) payload += Binary.write_l_triad(start) payload += Binary.write_l_triad(last) start = last = current records += 1 if start is last: payload += chr(self.RECORD_TYPE_SINGLE) payload += Binary.write_l_triad(start) else: payload += chr(self.RECORD_TYPE_RANGE) payload += Binary.write_l_triad(start) payload += Binary.write_l_triad(last) records += 1 self.put_short(records) self.buffer += payload