public function handle($address, $port, $packet) { $offset = 2; $packetType = ord($packet[$offset++]); $sessionID = Binary::readInt(substr($packet, $offset, 4)); $offset += 4; $payload = substr($packet, $offset); switch ($packetType) { case self::HANDSHAKE: //Handshake $reply = chr(self::HANDSHAKE); $reply .= Binary::writeInt($sessionID); $reply .= self::getTokenString($this->token, $address) . ""; $this->server->getNetwork()->sendPacket($address, $port, $reply); break; case self::STATISTICS: //Stat $token = Binary::readInt(substr($payload, 0, 4)); if ($token !== self::getTokenString($this->token, $address) and $token !== self::getTokenString($this->lastToken, $address)) { break; } $reply = chr(self::STATISTICS); $reply .= Binary::writeInt($sessionID); if ($this->timeout < microtime(true)) { $this->regenerateInfo(); } if (strlen($payload) === 8) { $reply .= $this->longData; } else { $reply .= $this->shortData; } $this->server->getNetwork()->sendPacket($address, $port, $reply); break; } }
public function doSlowCleanUp() { for ($i = 0; $i < 1024; ++$i) { if ($this->locationTable[$i][0] === 0 or $this->locationTable[$i][1] === 0) { continue; } fseek($this->filePointer, $this->locationTable[$i][0] << 12); $chunk = fread($this->filePointer, $this->locationTable[$i][1] << 12); $length = Binary::readInt(substr($chunk, 0, 4)); if ($length <= 1) { $this->locationTable[$i] = [0, 0, 0]; //Non-generated chunk, remove it from index } try { $chunk = zlib_decode(substr($chunk, 5)); } catch (\Exception $e) { $this->locationTable[$i] = [0, 0, 0]; //Corrupted chunk, remove it continue; } $chunk = chr(self::COMPRESSION_ZLIB) . zlib_encode($chunk, ZLIB_ENCODING_DEFLATE, 9); $chunk = Binary::writeInt(strlen($chunk)) . $chunk; $sectors = (int) ceil(strlen($chunk) / 4096); if ($sectors > $this->locationTable[$i][1]) { $this->locationTable[$i][0] = $this->lastSector + 1; $this->lastSector += $sectors; } fseek($this->filePointer, $this->locationTable[$i][0] << 12); fwrite($this->filePointer, str_pad($chunk, $sectors << 12, "", STR_PAD_RIGHT)); } $this->writeLocationTable(); $n = $this->cleanGarbage(); $this->writeLocationTable(); return $n; }
public function processBatch(BatchPacket $packet, Player $p) { $str = zlib_decode($packet->payload, 1024 * 1024 * 64); //Max 64MB $len = strlen($str); $offset = 0; try { while ($offset < $len) { $pkLen = Binary::readInt(substr($str, $offset, 4)); $offset += 4; $buf = substr($str, $offset, $pkLen); $offset += $pkLen; if (($pk = $this->getPacket(ord($buf[0]))) !== null) { if ($pk::NETWORK_ID === Info::BATCH_PACKET) { throw new \InvalidStateException("Invalid BatchPacket inside BatchPacket"); } $pk->setBuffer($buf, 1); $pk->decode(); $p->handleDataPacket($pk); if ($pk->getOffset() <= 0) { return; } } } } catch (\Exception $e) { if (\BukkitPE\DEBUG > 1) { $logger = $this->server->getLogger(); if ($logger instanceof MainLogger) { $logger->debug("BatchPacket " . " 0x" . bin2hex($packet->payload)); $logger->logException($e); } } } }
public function getInt() { return Binary::readInt($this->get(4)); }
public function getInt() { return $this->endianness === self::BIG_ENDIAN ? Binary::readInt($this->get(4)) : Binary::readLInt($this->get(4)); }