public function encode()
{
parent::encode();
$this->putAddress($this->address, $this->port, 4);
$this->putShort(0);
for ($i = 0; $i < 10; ++$i) {
$this->putAddress($this->systemAddresses[$i][0], $this->systemAddresses[$i][1], $this->systemAddresses[$i][2]);
}
$this->putLong($this->sendPing);
$this->putLong($this->sendPong);
}
private function handleEncapsulatedPacketRoute(EncapsulatedPacket $packet) { if ($this->sessionManager === \null) { return; } if ($packet->hasSplit) { if ($this->state === self::STATE_CONNECTED) { $this->handleSplit($packet); } return; } $id = \ord($packet->buffer[0]); if ($id < 0x80) { //internal data packet if ($this->state === self::STATE_CONNECTING_2) { if ($id === CLIENT_CONNECT_DataPacket::$ID) { $dataPacket = new CLIENT_CONNECT_DataPacket(); $dataPacket->buffer = $packet->buffer; $dataPacket->decode(); $pk = new SERVER_HANDSHAKE_DataPacket(); $pk->address = $this->address; $pk->port = $this->port; $pk->sendPing = $dataPacket->sendPing; $pk->sendPong = bcadd($pk->sendPing, "1000"); $pk->encode(); $sendPacket = new EncapsulatedPacket(); $sendPacket->reliability = 0; $sendPacket->buffer = $pk->buffer; $this->addToQueue($sendPacket, RakLib::PRIORITY_IMMEDIATE); } elseif ($id === CLIENT_HANDSHAKE_DataPacket::$ID) { $dataPacket = new CLIENT_HANDSHAKE_DataPacket(); $dataPacket->buffer = $packet->buffer; $dataPacket->decode(); if ($dataPacket->port === $this->sessionManager->getPort() or !$this->sessionManager->portChecking) { $this->state = self::STATE_CONNECTED; //FINALLY! $this->sessionManager->openSession($this); foreach ($this->preJoinQueue as $p) { $this->sessionManager->streamEncapsulated($this, $p); } $this->preJoinQueue = []; } } } elseif ($id === CLIENT_DISCONNECT_DataPacket::$ID) { $this->disconnect("客户端断开连接"); } elseif ($id === PING_DataPacket::$ID) { $dataPacket = new PING_DataPacket(); $dataPacket->buffer = $packet->buffer; $dataPacket->decode(); $pk = new PONG_DataPacket(); $pk->pingID = $dataPacket->pingID; $pk->encode(); $sendPacket = new EncapsulatedPacket(); $sendPacket->reliability = 0; $sendPacket->buffer = $pk->buffer; $this->addToQueue($sendPacket); } //TODO: add PING/PONG (0x00/0x03) automatic latency measure } elseif ($this->state === self::STATE_CONNECTED) { $this->sessionManager->streamEncapsulated($this, $packet); //TODO: stream channels } else { $this->preJoinQueue[] = $packet; } }