def handlePacket(self): packet = self.server.readThreadToMainPacket() if packet is None: return if len(packet) > 0: id = ord(packet[0]) offset = 1 if id == PyRakLib.PACKET_ENCAPSULATED: offset += 1 length = ord(packet[offset]) identifier = packet[offset:offset + length] offset += length + 1 flags = ord(packet[offset]) buffer = packet[offset:] self.instance.handleEncapsulated( identifier, EncapsulatedPacket.fromBinary(buffer, True), flags) elif id == PyRakLib.PACKET_RAW: length = ord(packet[offset]) offset += 1 address = packet[offset:offset + length] offset += length port = Binary.readShort(packet[offset:offset + 2]) offset += 2 payload = packet[offset:] self.instance.handleRaw(address, port, payload) elif id == PyRakLib.PACKET_SET_OPTION: length = ord(packet[offset]) offset += 1 name = packet[offset:offset + length] offset += length value = packet[offset:] self.instance.handleOption(name, value) elif id == PyRakLib.PACKET_OPEN_SESSION: offset += 1 length = ord(packet[offset]) identifier = packet[offset:offset + length] offset += length + 1 length = ord(packet[offset]) address = packet[offset:offset + length] offset += len port = Binary.readShort(packet[offset:offset + 2]) offset += 2 clientID = Binary.readLong(packet[offset:offset + 8]) self.instance.openSession(identifier, address, port, clientID) elif id == PyRakLib.PACKET_CLOSE_SESSION: length = ord(packet[offset]) offset += 1 identifier = packet[offset:offset + length] offset += length length = ord(packet[offset]) offset += 1 reason = packet[offset:offset + length] self.instance.closeSession(identifier, reason) elif id == PyRakLib.PACKET_INVALID_SESSION: offset += 1 length = ord(packet[offset]) identifier = packet[offset:offset + length] self.instance.closeSession(identifier, "Invalid session") elif id == PyRakLib.PACKET_ACK_NOTIFICATION: offset += 1 length = ord(packet[offset]) identifier = packet[offset:offset + length] offset += length identifierACK = Binary.readInt(packet[offset:offset + 4]) self.instance.notifyACK(identifier, identifierACK) return True return False
def getLong(self) -> int: return Binary.readLong(self.get(8))