Exemple #1
0
 protected function getVarInt()
 {
     return Binary::readVarInt($this->buffer, $this->offset);
 }
Exemple #2
0
 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}");
         }
     }
 }