PacketBatch: added a getPackets() method which encapsulates some logic

This commit is contained in:
Dylan K. Taylor
2020-07-01 13:35:34 +01:00
parent 29612cded3
commit 30591d047c
3 changed files with 87 additions and 18 deletions

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\serializer;
use pocketmine\network\mcpe\protocol\Packet;
use pocketmine\network\mcpe\protocol\PacketDecodeException;
use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\utils\BinaryDataException;
@@ -48,6 +49,19 @@ class PacketBatch{
return $packetPool->getPacket($this->serializer->getString());
}
/**
* @return \Generator|Packet[]
* @phpstan-return \Generator<int, Packet, void, void>
*/
public function getPackets(PacketPool $packetPool, int $max) : \Generator{
for($c = 0; $c < $max and !$this->serializer->feof(); ++$c){
yield $c => $packetPool->getPacket($this->serializer->getString());
}
if(!$this->serializer->feof()){
throw new PacketDecodeException("Reached limit of $max packets in a single batch");
}
}
/**
* Constructs a packet batch from the given list of packets.
*