Exemple #1
0
 protected function processPacket()
 {
     @fread($this->fp, 1);
     if (strlen($packet = $this->thread->readMainToThreadPacket()) > 0) {
         $pid = ord($packet[0]);
         $buffer = substr($packet, 1);
         if ($pid === self::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $data = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->writeRaw($data);
         } elseif ($pid === self::PACKET_ENABLE_ENCRYPTION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $secret = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->enableEncryption($secret);
         } elseif ($pid === self::PACKET_SET_COMPRESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $threshold = Binary::readInt(substr($buffer, 4, 4));
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->setCompression($threshold);
         } elseif ($pid === self::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             if (isset($this->sessions[$id])) {
                 $this->close($this->sessions[$id]);
             }
         } elseif ($pid === self::PACKET_SHUTDOWN) {
             $this->shutdown = true;
             foreach ($this->sessions as $session) {
                 $session->close();
             }
         } elseif ($pid === self::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = true;
         }
         return true;
     }
     return false;
 }
 public function process()
 {
     if (count($this->identifiers) > 0) {
         foreach ($this->identifiers as $id => $player) {
             $player->handleACK($id);
         }
     }
     while (strlen($buffer = $this->thread->readThreadToMainPacket()) > 0) {
         $offset = 1;
         $pid = ord($buffer[0]);
         if ($pid === ServerManager::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             if (isset($this->sessionsPlayers[$id])) {
                 $payload = substr($buffer, $offset);
                 try {
                     $this->handlePacket($this->sessionsPlayers[$id], $payload);
                 } catch (\Exception $e) {
                     if (\pocketmine\DEBUG > 1) {
                         $logger = $this->server->getLogger();
                         if ($logger instanceof MainLogger) {
                             $logger->debug("DesktopPacket 0x" . bin2hex($payload));
                             $logger->logException($e);
                         }
                     }
                 }
             }
         } elseif ($pid === ServerManager::PACKET_OPEN_SESSION) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             if (isset($this->sessionsPlayers[$id])) {
                 continue;
             }
             $len = ord($buffer[$offset++]);
             $address = substr($buffer, $offset, $len);
             $offset += $len;
             $port = Binary::readShort(substr($buffer, $offset, 2));
             $identifier = "{$id}:{$address}:{$port}";
             $player = new DesktopPlayer($this, $identifier, $address, $port, $this->plugin);
             $this->sessions->attach($player, $id);
             $this->sessionsPlayers[$id] = $player;
             $this->plugin->getServer()->addPlayer($identifier, $player);
         } elseif ($pid === ServerManager::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, $offset, 4));
             $offset += 4;
             $flag = Binary::readInt(substr($buffer, $offset, 4));
             if (isset($this->sessionsPlayers[$id])) {
                 if ($flag === 0) {
                     $this->close($this->sessionsPlayers[$id]);
                 } else {
                     $this->closeSession($id);
                 }
             }
         }
     }
     return true;
 }
Exemple #3
0
 protected function getInt()
 {
     return Binary::readInt($this->get(4));
 }
 protected function processPacket()
 {
     @fread($this->fp, 1);
     if (strlen($packet = $this->thread->readMainToThreadPacket()) > 0) {
         $pid = ord($packet[0]);
         $buffer = substr($packet, 1);
         if ($pid === self::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $data = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->writeRaw($data);
         } elseif ($pid === self::PACKET_ENABLE_ENCRYPTION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $secret = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->enableEncryption($secret);
         } elseif ($pid === self::PACKET_SET_COMPRESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $threshold = Binary::readInt(substr($buffer, 4, 4));
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->setCompression($threshold);
         } elseif ($pid === self::PACKET_SET_OPTION) {
             $offset = 1;
             $len = ord($packet[$offset++]);
             $name = substr($packet, $offset, $len);
             $offset += $len;
             $value = substr($packet, $offset);
             switch ($name) {
                 case "name":
                     $this->serverdata = json_decode($value, true);
                     break;
             }
         } elseif ($pid === self::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             if (isset($this->sessions[$id])) {
                 $this->close($this->sessions[$id]);
             } else {
                 $this->closeSession($id, 1);
             }
         } elseif ($pid === self::PACKET_SHUTDOWN) {
             foreach ($this->sessions as $session) {
                 $session->close();
             }
             $this->shutdown();
             stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
             $this->shutdown = true;
         } elseif ($pid === self::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = true;
         }
         return true;
     }
     return false;
 }