44 private \SplFixedArray $packetPool;
46 public function __construct(
50 $this->registerPackets();
60 $pk = $this->getPacketFromPool($payload);
64 $reader =
new PacketSerializer($payload);
70 $remains = substr($reader->getBuffer(), $reader->getOffset());
71 $this->server->getLogger()->debug(
"Still " . strlen($remains) .
" bytes unread in " . get_class($pk) .
" from $address");
73 return $this->handle($pk, $address);
76 private function handle(OfflineMessage $packet, InternetAddress $address) : bool{
77 if($packet instanceof UnconnectedPing){
78 $this->
server->sendPacket(UnconnectedPong::create($packet->sendPingTime, $this->server->getID(), $this->server->getName()), $address);
79 }elseif($packet instanceof OpenConnectionRequest1){
80 if(!$this->protocolAcceptor->accepts($packet->protocol)){
81 $this->
server->sendPacket(IncompatibleProtocolVersion::create($this->protocolAcceptor->getPrimaryVersion(), $this->server->getID()), $address);
82 $this->
server->getLogger()->notice(
"Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)");
85 $this->
server->sendPacket(OpenConnectionReply1::create($this->
server->getID(),
false, $packet->mtuSize + 28), $address);
87 }elseif($packet instanceof OpenConnectionRequest2){
88 if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){
89 if($packet->mtuSize < Session::MIN_MTU_SIZE){
90 $this->
server->getLogger()->debug(
"Not creating session for $address due to bad MTU size $packet->mtuSize");
93 $existingSession = $this->
server->getSessionByAddress($address);
94 if($existingSession !==
null && $existingSession->isConnected()){
97 $this->
server->getLogger()->debug(
"Not creating session for $address due to session already opened");
100 $mtuSize = min($packet->mtuSize, $this->server->getMaxMtuSize());
101 $this->
server->sendPacket(OpenConnectionReply2::create($this->
server->getID(), $address, $mtuSize,
false), $address);
102 $this->
server->createSession($address, $packet->clientID, $mtuSize);
104 $this->
server->getLogger()->debug(
"Not creating session for $address due to mismatched port, expected " . $this->
server->getPort() .
", got " . $packet->serverAddress->getPort());
116 private function registerPacket(
int $id,
string $class) : void{
117 $this->packetPool[$id] = new $class;
120 public function getPacketFromPool(
string $buffer) : ?OfflineMessage{
121 $pk = $this->packetPool[ord($buffer[0])];
129 private function registerPackets() : void{
130 $this->packetPool = new \SplFixedArray(256);
132 $this->registerPacket(MessageIdentifiers::ID_UNCONNECTED_PING, UnconnectedPing::class);
133 $this->registerPacket(MessageIdentifiers::ID_UNCONNECTED_PING_OPEN_CONNECTIONS, UnconnectedPingOpenConnections::class);
134 $this->registerPacket(MessageIdentifiers::ID_OPEN_CONNECTION_REQUEST_1, OpenConnectionRequest1::class);
135 $this->registerPacket(MessageIdentifiers::ID_OPEN_CONNECTION_REQUEST_2, OpenConnectionRequest2::class);