PacketBatch: get rid of putPacket()

This commit is contained in:
Dylan K. Taylor 2020-07-22 15:24:08 +01:00
parent 2fcee432c1
commit 798efc370c
2 changed files with 6 additions and 16 deletions

View File

@ -37,11 +37,6 @@ class PacketBatch{
$this->serializer = new PacketSerializer($buffer ?? ""); $this->serializer = new PacketSerializer($buffer ?? "");
} }
public function putPacket(Packet $packet) : void{
$packet->encode();
$this->serializer->putString($packet->getSerializer()->getBuffer());
}
/** /**
* @throws BinaryDataException * @throws BinaryDataException
*/ */
@ -72,7 +67,8 @@ class PacketBatch{
public static function fromPackets(Packet ...$packets) : self{ public static function fromPackets(Packet ...$packets) : self{
$result = new self(); $result = new self();
foreach($packets as $packet){ foreach($packets as $packet){
$result->putPacket($packet); $packet->encode();
$result->serializer->putString($packet->getSerializer()->getBuffer());
} }
return $result; return $result;
} }

View File

@ -23,21 +23,18 @@ declare(strict_types=1);
namespace pocketmine\mcpe\protocol\serializer; namespace pocketmine\mcpe\protocol\serializer;
use pocketmine\network\mcpe\protocol\Packet; use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\protocol\PacketDecodeException; use pocketmine\network\mcpe\protocol\PacketDecodeException;
use pocketmine\network\mcpe\protocol\PacketPool; use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch; use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\protocol\TestPacket; use pocketmine\network\mcpe\protocol\TestPacket;
use function array_fill;
class PacketBatchTest extends TestCase{ class PacketBatchTest extends TestCase{
public function testDecodeTooBig() : void{ public function testDecodeTooBig() : void{
$limit = 10; $limit = 10;
$write = new PacketBatch(); $write = PacketBatch::fromPackets(...array_fill(0, $limit + 1, new TestPacket()));
for($i = 0; $i < $limit + 1; $i++){
$write->putPacket(new TestPacket());
}
$read = new PacketBatch($write->getBuffer()); $read = new PacketBatch($write->getBuffer());
$this->expectException(PacketDecodeException::class); $this->expectException(PacketDecodeException::class);
$readCount = 0; $readCount = 0;
@ -48,10 +45,7 @@ class PacketBatchTest extends TestCase{
public function testDecodeAtLimit() : void{ public function testDecodeAtLimit() : void{
$limit = 10; $limit = 10;
$write = new PacketBatch(); $write = PacketBatch::fromPackets(...array_fill(0, $limit, new TestPacket()));
for($i = 0; $i < $limit; $i++){
$write->putPacket(new TestPacket());
}
$read = new PacketBatch($write->getBuffer()); $read = new PacketBatch($write->getBuffer());
$readCount = 0; $readCount = 0;
foreach($read->getPackets(PacketPool::getInstance(), $limit) as $packet){ foreach($read->getPackets(PacketPool::getInstance(), $limit) as $packet){