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; }
protected function getShort() { return Binary::readShort($this->get(2)); }
public function process() { $length = Binary::readVarIntSession($this); if ($length === false or $this->status === -1) { $this->close("Connection closed"); return; } elseif ($length <= 0 or $length > 131070) { $this->close("Invalid length"); return; } $offset = 0; $buffer = $this->read($length); if ($this->threshold !== null) { $dataLength = Binary::readVarInt($buffer, $offset); if ($dataLength !== 0) { if ($dataLength < $this->threshold) { $this->close("Invalid compression threshold"); } else { $buffer = zlib_decode(substr($buffer, $offset)); $offset = 0; } } else { $buffer = substr($buffer, $offset); $offset = 0; } } if ($this->status === 2) { //Login $this->manager->sendPacket($this->identifier, $buffer); } elseif ($this->status === 1) { $pid = Binary::readVarInt($buffer, $offset); if ($pid === 0x0) { $sample = []; foreach ($this->manager->sample as $id => $name) { $sample[] = ["name" => $name, "id" => $id]; } $data = ["version" => ["name" => Info::VERSION, "protocol" => Info::PROTOCOL], "players" => ["max" => $this->manager->getServerData()["MaxPlayers"], "online" => $this->manager->getServerData()["OnlinePlayers"], "sample" => $sample], "description" => json_decode(TextFormat::toJSON($this->manager->description))]; if ($this->manager->favicon !== null) { $data["favicon"] = $this->manager->favicon; } $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $data = Binary::writeVarInt(0x0) . Binary::writeVarInt(strlen($data)) . $data; $this->writeRaw($data); } elseif ($pid === 0x1) { $packet = new PingPacket(); $packet->read($buffer, $offset); $this->writePacket($packet); $this->status = -1; } } elseif ($this->status === 0) { $pid = Binary::readVarInt($buffer, $offset); if ($pid === 0x0) { $protocol = Binary::readVarInt($buffer, $offset); $len = Binary::readVarInt($buffer, $offset); $hostname = substr($buffer, $offset, $len); $offset += $len; $serverPort = Binary::readShort(substr($buffer, $offset, 2)); $offset += 2; $nextState = Binary::readVarInt($buffer, $offset); if ($nextState === 1) { $this->status = 1; } elseif ($nextState === 2) { $this->status = -1; if ($protocol < Info::PROTOCOL) { $packet = new LoginDisconnectPacket(); $packet->reson = TextFormat::toJSON(TextFormat::BOLD . "Outdated client!" . TextFormat::RESET . "\n\nPlease use " . Info::VERSION); $this->writePacket($packet); } elseif ($protocol > Info::PROTOCOL) { $packet = new LoginDisconnectPacket(); $packet->reson = TextFormat::toJSON(TextFormat::BOLD . "Outdated server!" . TextFormat::RESET . "\n\nI'm using " . Info::VERSION); $this->writePacket($packet); } else { $this->manager->openSession($this); $this->status = 2; } } else { $this->close(); } } else { $this->close("Unexpected packet {$pid}"); } } }