PacketStream: added fromPackets() sugar

This commit is contained in:
Dylan K. Taylor 2019-05-06 19:54:42 +01:00
parent 3415edf600
commit ce61c6e0fd
4 changed files with 18 additions and 12 deletions

View File

@ -1492,10 +1492,7 @@ class Server{
}
$recipients = $ev->getTargets();
$stream = new PacketStream();
foreach($ev->getPackets() as $packet){
$stream->putPacket($packet);
}
$stream = PacketStream::fromPackets(...$ev->getPackets());
if(NetworkCompression::$THRESHOLD < 0 or strlen($stream->getBuffer()) < NetworkCompression::$THRESHOLD){
foreach($recipients as $target){

View File

@ -115,11 +115,8 @@ class CraftingManager{
$pk->addFurnaceRecipe($recipe);
}
$batch = new PacketStream();
$batch->putPacket($pk);
$this->craftingDataCache = new CompressBatchPromise();
$this->craftingDataCache->resolve(NetworkCompression::compress($batch->getBuffer()));
$this->craftingDataCache->resolve(NetworkCompression::compress(PacketStream::fromPackets($pk)->getBuffer()));
Timings::$craftingDataCacheRebuildTimer->stopTiming();
}

View File

@ -57,10 +57,7 @@ class ChunkRequestTask extends AsyncTask{
$pk->chunkZ = $this->chunkZ;
$pk->data = $this->chunk;
$stream = new PacketStream();
$stream->putPacket($pk);
$this->setResult(NetworkCompression::compress($stream->getBuffer(), $this->compressionLevel));
$this->setResult(NetworkCompression::compress(PacketStream::fromPackets($pk)->getBuffer(), $this->compressionLevel));
}
public function onError() : void{

View File

@ -43,4 +43,19 @@ class PacketStream extends NetworkBinaryStream{
public function getPacket() : Packet{
return PacketPool::getPacket($this->getString());
}
/**
* Constructs a packet batch from the given list of packets.
*
* @param Packet ...$packets
*
* @return PacketStream
*/
public static function fromPackets(Packet ...$packets) : self{
$result = new self();
foreach($packets as $packet){
$result->putPacket($packet);
}
return $result;
}
}