DataPacket no longer keeps its own serializer

since a while ago, we're anyway just discarding the internal buffer anyway when the packet is repeatedly encoded, so this doesn't serve any advantage anymore.
We do need a system to be able to reuse encoded packet buffers, but right now we're not reusing them anyway.
This commit is contained in:
Dylan K. Taylor
2021-04-09 15:37:58 +01:00
parent 19f536d68a
commit 0312b62c8a
7 changed files with 29 additions and 50 deletions

View File

@ -44,21 +44,6 @@ abstract class DataPacket implements Packet{
/** @var int */
public $recipientSubId = 0;
/** @var PacketSerializer */
private $buf;
public function __construct(){
$this->buf = new PacketSerializer();
}
public function getSerializer() : PacketSerializer{
return $this->buf;
}
public function setSerializer(PacketSerializer $serializer) : void{
$this->buf = $serializer;
}
public function pid() : int{
return $this::NETWORK_ID;
}
@ -74,11 +59,10 @@ abstract class DataPacket implements Packet{
/**
* @throws PacketDecodeException
*/
final public function decode() : void{
$this->buf->rewind();
final public function decode(PacketSerializer $in) : void{
try{
$this->decodeHeader($this->buf);
$this->decodePayload($this->buf);
$this->decodeHeader($in);
$this->decodePayload($in);
}catch(BinaryDataException | PacketDecodeException $e){
throw PacketDecodeException::wrap($e, $this->getName());
}
@ -108,10 +92,9 @@ abstract class DataPacket implements Packet{
*/
abstract protected function decodePayload(PacketSerializer $in) : void;
final public function encode() : void{
$this->buf = new PacketSerializer();
$this->encodeHeader($this->buf);
$this->encodePayload($this->buf);
final public function encode(PacketSerializer $out) : void{
$this->encodeHeader($out);
$this->encodePayload($out);
}
protected function encodeHeader(PacketSerializer $out) : void{

View File

@ -27,10 +27,6 @@ use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
interface Packet{
public function getSerializer() : PacketSerializer;
public function setSerializer(PacketSerializer $serializer) : void;
public function pid() : int;
public function getName() : string;
@ -40,9 +36,9 @@ interface Packet{
/**
* @throws PacketDecodeException
*/
public function decode() : void;
public function decode(PacketSerializer $in) : void;
public function encode() : void;
public function encode(PacketSerializer $out) : void;
/**
* Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\utils\Binary;
use pocketmine\utils\BinaryDataException;
class PacketPool{
@ -216,10 +216,7 @@ class PacketPool{
* @throws BinaryDataException
*/
public function getPacket(string $buffer) : Packet{
$serializer = new PacketSerializer($buffer);
$pk = $this->getPacketById($serializer->getUnsignedVarInt() & DataPacket::PID_MASK);
$pk->setSerializer($serializer);
return $pk;
$offset = 0;
return $this->getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK);
}
}

View File

@ -39,14 +39,15 @@ class PacketBatch{
/**
* @return \Generator|Packet[]
* @phpstan-return \Generator<int, Packet, void, void>
* @phpstan-return \Generator<int, array{Packet, string}, void, void>
* @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());
$buffer = $serializer->getString();
yield $c => [$packetPool->getPacket($buffer), $buffer];
}catch(BinaryDataException $e){
throw new PacketDecodeException("Error decoding packet $c of batch: " . $e->getMessage(), 0, $e);
}
@ -66,8 +67,9 @@ class PacketBatch{
public static function fromPackets(Packet ...$packets) : self{
$serializer = new PacketSerializer();
foreach($packets as $packet){
$packet->encode();
$serializer->putString($packet->getSerializer()->getBuffer());
$subSerializer = new PacketSerializer();
$packet->encode($subSerializer);
$serializer->putString($subSerializer->getBuffer());
}
return new self($serializer->getBuffer());
}