From 6de0b48c1ff873d8ddfc46c9a4cd7f176b7d1f68 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 17 May 2019 18:04:51 +0100 Subject: [PATCH] PacketBatch: Always encode packets freshly, never reuse buffers this causes bugs when a packet is modified during events and then re-sent to a player. Since we can't control this, we can't allow this kind of buffer reuse. The only notable case where this will cause loss of performance is when broadcasting a series of packet(s) which accumulate to less than 256 bytes, which is reasonably cheap to encode anyway. In addition, removing this caching is one roadblock to moving this serialization to native code, which will make it vastly faster. --- src/pocketmine/network/mcpe/PacketBatch.php | 4 +--- src/pocketmine/network/mcpe/protocol/DataPacket.php | 8 -------- src/pocketmine/network/mcpe/protocol/Packet.php | 2 -- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/pocketmine/network/mcpe/PacketBatch.php b/src/pocketmine/network/mcpe/PacketBatch.php index 4f710c84d..2e3d8e046 100644 --- a/src/pocketmine/network/mcpe/PacketBatch.php +++ b/src/pocketmine/network/mcpe/PacketBatch.php @@ -30,9 +30,7 @@ use pocketmine\utils\BinaryDataException; class PacketBatch extends NetworkBinaryStream{ public function putPacket(Packet $packet) : void{ - if(!$packet->isEncoded()){ - $packet->encode(); - } + $packet->encode(); $this->putString($packet->getBuffer()); } diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index ffea84b63..e6563630f 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -39,9 +39,6 @@ abstract class DataPacket extends NetworkBinaryStream implements Packet{ public const NETWORK_ID = 0; - /** @var bool */ - private $isEncoded = false; - /** @var int */ public $senderSubId = 0; /** @var int */ @@ -104,11 +101,6 @@ abstract class DataPacket extends NetworkBinaryStream implements Packet{ $this->reset(); $this->encodeHeader(); $this->encodePayload(); - $this->isEncoded = true; - } - - final public function isEncoded() : bool{ - return $this->isEncoded; } protected function encodeHeader() : void{ diff --git a/src/pocketmine/network/mcpe/protocol/Packet.php b/src/pocketmine/network/mcpe/protocol/Packet.php index 959103fbc..5ca1ba074 100644 --- a/src/pocketmine/network/mcpe/protocol/Packet.php +++ b/src/pocketmine/network/mcpe/protocol/Packet.php @@ -61,8 +61,6 @@ interface Packet{ public function encode() : void; - public function isEncoded() : bool; - /** * Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for * this.