mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-09 03:06:55 +00:00
Minor improvements to Batch encoding
This commit is contained in:
@ -25,11 +25,14 @@ namespace pocketmine\network\mcpe\protocol;
|
||||
|
||||
|
||||
use pocketmine\network\mcpe\NetworkSession;
|
||||
use pocketmine\utils\Binary;
|
||||
use pocketmine\utils\BinaryStream;
|
||||
|
||||
class BatchPacket extends DataPacket{
|
||||
const NETWORK_ID = 0xfe;
|
||||
|
||||
public $payload;
|
||||
public $compressed = false;
|
||||
|
||||
public function canBeBatched() : bool{
|
||||
return false;
|
||||
@ -45,9 +48,30 @@ class BatchPacket extends DataPacket{
|
||||
|
||||
public function encode(){
|
||||
$this->reset();
|
||||
assert($this->compressed);
|
||||
$this->put($this->payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataPacket|string $packet
|
||||
*/
|
||||
public function addPacket($packet){
|
||||
if($packet instanceof DataPacket){
|
||||
if(!$packet->isEncoded){
|
||||
$packet->encode();
|
||||
}
|
||||
$packet = $packet->buffer;
|
||||
}
|
||||
|
||||
$this->payload .= Binary::writeUnsignedVarInt(strlen($packet)) . $packet;
|
||||
}
|
||||
|
||||
public function compress(int $level = 7){
|
||||
assert(!$this->compressed);
|
||||
$this->payload = zlib_encode($this->payload, ZLIB_ENCODING_DEFLATE, $level);
|
||||
$this->compressed = true;
|
||||
}
|
||||
|
||||
public function handle(NetworkSession $session) : bool{
|
||||
if(strlen($this->payload) < 2){
|
||||
throw new \InvalidStateException("Not enough bytes in payload, expected zlib header");
|
||||
|
Reference in New Issue
Block a user