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.
This commit is contained in:
Dylan K. Taylor 2019-05-17 18:04:51 +01:00
parent 64f7f558a4
commit 6de0b48c1f
3 changed files with 1 additions and 13 deletions

View File

@ -30,9 +30,7 @@ use pocketmine\utils\BinaryDataException;
class PacketBatch extends NetworkBinaryStream{ class PacketBatch extends NetworkBinaryStream{
public function putPacket(Packet $packet) : void{ public function putPacket(Packet $packet) : void{
if(!$packet->isEncoded()){ $packet->encode();
$packet->encode();
}
$this->putString($packet->getBuffer()); $this->putString($packet->getBuffer());
} }

View File

@ -39,9 +39,6 @@ abstract class DataPacket extends NetworkBinaryStream implements Packet{
public const NETWORK_ID = 0; public const NETWORK_ID = 0;
/** @var bool */
private $isEncoded = false;
/** @var int */ /** @var int */
public $senderSubId = 0; public $senderSubId = 0;
/** @var int */ /** @var int */
@ -104,11 +101,6 @@ abstract class DataPacket extends NetworkBinaryStream implements Packet{
$this->reset(); $this->reset();
$this->encodeHeader(); $this->encodeHeader();
$this->encodePayload(); $this->encodePayload();
$this->isEncoded = true;
}
final public function isEncoded() : bool{
return $this->isEncoded;
} }
protected function encodeHeader() : void{ protected function encodeHeader() : void{

View File

@ -61,8 +61,6 @@ interface Packet{
public function encode() : void; 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 * Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for
* this. * this.