/** * @return bool */ public function handlePacket() { if (strlen($packet = $this->server->readThreadToMainPacket()) > 0) { $id = ord($packet[0]); $offset = 1; if ($id === RakLib::PACKET_ENCAPSULATED) { $len = ord($packet[$offset++]); $identifier = substr($packet, $offset, $len); $offset += $len; $flags = ord($packet[$offset++]); $buffer = substr($packet, $offset); $this->instance->handleEncapsulated($identifier, EncapsulatedPacket::fromBinary($buffer, true), $flags); } elseif ($id === RakLib::PACKET_RAW) { $len = ord($packet[$offset++]); $address = substr($packet, $offset, $len); $offset += $len; $port = unpack("n", substr($packet, $offset, 2))[1]; $offset += 2; $payload = substr($packet, $offset); $this->instance->handleRaw($address, $port, $payload); } elseif ($id === RakLib::PACKET_SET_OPTION) { $len = ord($packet[$offset++]); $name = substr($packet, $offset, $len); $offset += $len; $value = substr($packet, $offset); $this->instance->handleOption($name, $value); } elseif ($id === RakLib::PACKET_OPEN_SESSION) { $len = ord($packet[$offset++]); $identifier = substr($packet, $offset, $len); $offset += $len; $len = ord($packet[$offset++]); $address = substr($packet, $offset, $len); $offset += $len; $port = unpack("n", substr($packet, $offset, 2))[1]; $offset += 2; $clientID = Binary::readLong(substr($packet, $offset, 8)); $this->instance->openSession($identifier, $address, $port, $clientID); } elseif ($id === RakLib::PACKET_CLOSE_SESSION) { $len = ord($packet[$offset++]); $identifier = substr($packet, $offset, $len); $offset += $len; $len = ord($packet[$offset++]); $reason = substr($packet, $offset, $len); $this->instance->closeSession($identifier, $reason); } elseif ($id === RakLib::PACKET_INVALID_SESSION) { $len = ord($packet[$offset++]); $identifier = substr($packet, $offset, $len); $this->instance->closeSession($identifier, "Invalid session"); } elseif ($id === RakLib::PACKET_ACK_NOTIFICATION) { $len = ord($packet[$offset++]); $identifier = substr($packet, $offset, $len); $offset += $len; $identifierACK = PHP_INT_SIZE === 8 ? unpack("N", substr($packet, $offset, 4))[1] << 32 >> 32 : unpack("N", substr($packet, $offset, 4))[1]; $this->instance->notifyACK($identifier, $identifierACK); } return true; } return false; }