Minor improvements to Batch encoding

This commit is contained in:
Dylan K. Taylor
2017-04-15 19:40:06 +01:00
parent b55929b382
commit c5eccc8e1c
3 changed files with 39 additions and 18 deletions

View File

@ -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");