buffer = $buffer; } /** * @return \Generator|Packet[] * @phpstan-return \Generator * @throws PacketDecodeException */ public function getPackets(PacketPool $packetPool, int $max) : \Generator{ $serializer = new PacketSerializer($this->buffer); for($c = 0; $c < $max and !$serializer->feof(); ++$c){ try{ yield $c => $packetPool->getPacket($serializer->getString()); }catch(BinaryDataException $e){ throw new PacketDecodeException("Error decoding packet $c of batch: " . $e->getMessage(), 0, $e); } } if(!$serializer->feof()){ throw new PacketDecodeException("Reached limit of $max packets in a single batch"); } } /** * Constructs a packet batch from the given list of packets. * * @param Packet ...$packets * * @return PacketBatch */ public static function fromPackets(Packet ...$packets) : self{ $serializer = new PacketSerializer(); foreach($packets as $packet){ $packet->encode(); $serializer->putString($packet->getSerializer()->getBuffer()); } return new self($serializer->getBuffer()); } public function getBuffer() : string{ return $this->buffer; } }