def fromBinary(self, buffer): offset = 0 packet = EncapsulatedPacket() header = buffer[offset] offset += 1 packet.reliability = (header & 224) >> 5 packet.split = (header & BitFlags.Split) > 0 length = Binary.readShort(buffer[offset:offset + 2]) length >>= 3 offset += 2 if length == 0: raise Exception("Got an empty encapsulated packet") if Reliability().isReliable(packet.reliability): packet.messageIndex = Binary.readLTriad(buffer[offset:offset + 3]) offset += 3 if Reliability().isSequenced(packet.reliability): packet.sequenceIndex = Binary.readLTriad(buffer[offset:offset + 3]) offset += 3 if Reliability().isSequencedOrOrdered(packet.reliability): packet.orderIndex = Binary.readLTriad(buffer[offset:offset + 3]) offset += 3 packet.orderChannel = Binary.readByte(buffer[offset:offset + 1]) offset += 1 if packet.split: packet.splitCount = Binary.readInt(buffer[offset:offset + 4]) offset += 4 packet.splitId = Binary.readShort(buffer[offset:offset + 2]) offset += 2 packet.splitIndex = Binary.readInt(buffer[offset:offset + 4]) offset += 4 packet.buffer = buffer[offset:offset + length] offset += length return packet
def getTotalLength(self): length = 3 length += 3 if Reliability.isReliable(self.reliability) else 0 length += 3 if Reliability.isSequenced(self.reliability) else 0 length += 4 if Reliability.isSequencedOrOrdered( self.reliability) else 0 length += 10 if self.splitCount > 0 else 0 length += len(self.buffer) return length
def addEncapsulatedToQueue(self, packet, flags=priority["Normal"]): if Reliability.isReliable(packet.reliability): packet.messageIndex = self.messageIndex self.messageIndex += 1 if packet.reliability == Reliability.reliableOrdered: packet.orderIndex = self.channelIndex[packet.orderChannel] self.channelIndex[packet.orderChannel] += 1 if packet.getTotalLength() > self.mtuSize: buffers = {} i = 0 splitIndex = 0 while i < len(packet.buffer): buffers[splitIndex] = packet.buffer[i:i + self.mtuSize] splitIndex += 1 i += self.mtuSize for index, buffer in buffers.items(): pk = EncapsulatedPacket() pk.splitId = self.splitId pk.splitCount = len(buffers) pk.reliability = packet.reliability pk.splitIndex = index pk.buffer = buffer if index != 0: pk.messageIndex = self.messageIndex self.messageIndex += 1 if pk.reliability == Reliability.reliableOrdered: pk.orderChannel = packet.orderChannel pk.orderIndex = packet.orderIndex self.addToQueue(pk, flags) self.splitId += 1 else: self.addToQueue(packet, flags)
def receivePacket(self, packet): if not Reliability.isReliable(packet.reliability): self.handlePacket(packet) else: holeCount = self.lastReliableIndex - packet.messageIndex if holeCount == 0: self.handlePacket(packet) self.lastReliableIndex += 1
def toBinary(self): buffer = b"" header = self.reliability << 5 if self.split: header |= BitFlags.Split buffer += Binary.writeByte(header) buffer += Binary.writeShort(len(self.buffer) << 3) if Reliability().isReliable(self.reliability): buffer += Binary.writeLTriad(self.messageIndex) if Reliability().isSequenced(self.reliability): buffer += Binary.writeLTriad(self.sequenceIndex) if Reliability().isSequencedOrOrdered(self.reliability): buffer += Binary.writeLTriad(self.orderIndex) buffer += Binary.writeByte(self.orderChannel) if self.split: buffer += Binary.writeInt(self.splitCount) buffer += Binary.writeShort(self.splitId) buffer += Binary.writeInt(self.splitIndex) return buffer + self.buffer
def toBinary(self): stream = BinaryStream() header = self.reliability << 5 if self.splitCount > 0: header |= 0x10 stream.putByte(header) stream.putShort(len(self.buffer) << 3) if Reliability.isReliable(self.reliability): stream.putLTriad(self.messageIndex) if Reliability.isSequenced(self.reliability): stream.putLTriad(self.sequenceIndex) if Reliability.isSequencedOrOrdered(self.reliability): stream.putLTriad(self.orderIndex) stream.putByte(self.orderChannel) if self.splitCount > 0: stream.putInt(self.splitCount) stream.putShort(self.splitId) stream.putInt(self.splitIndex) stream.put(self.buffer) return stream
def fromBinary(stream): packet = EncapsulatedPacket() header = stream.getByte() packet.reliability = (header & 224) >> 5 split = (header & 0x10) > 0 length = stream.getShort() >> 3 if length == 0: raise Exception("Got an empty encapsulated packet") if Reliability.isReliable(packet.reliability): packet.messageIndex = stream.getLTriad() if Reliability.isSequenced(packet.reliability): packet.sequenceIndex = stream.getLTriad() if Reliability.isSequencedOrOrdered(packet.reliability): packet.orderIndex = stream.getLTriad() packet.orderChannel = stream.getByte() if split: packet.splitCount = stream.getInt() packet.splitId = stream.getShort() packet.splitIndex = stream.getInt() packet.buffer = stream.buffer[stream.offset:] stream.offset += length return packet