encode() 공개 메소드

public encode ( )
예제 #1
0
 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;
     }
 }
예제 #2
0
 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 = PacketReliability::UNRELIABLE;
                 $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->isTemporal = false;
                     $this->sessionManager->openSession($this);
                 }
             }
         } elseif ($id === CLIENT_DISCONNECT_DataPacket::$ID) {
             $this->disconnect("client 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 = PacketReliability::UNRELIABLE;
             $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->sessionManager->getLogger()->notice("Received packet before connection: " . bin2hex($packet->buffer));
     }
 }