PacketPool is now non-static

this allows greater flexibility for customisation, and will facilitate future multi version support.
This commit is contained in:
Dylan K. Taylor 2020-04-23 16:11:47 +01:00
parent f3d7c320a1
commit 095a21ea5a
3 changed files with 162 additions and 154 deletions

View File

@ -28,7 +28,6 @@ namespace pocketmine\network;
use pocketmine\event\server\NetworkInterfaceRegisterEvent; use pocketmine\event\server\NetworkInterfaceRegisterEvent;
use pocketmine\event\server\NetworkInterfaceUnregisterEvent; use pocketmine\event\server\NetworkInterfaceUnregisterEvent;
use pocketmine\network\mcpe\protocol\PacketPool;
use function base64_encode; use function base64_encode;
use function get_class; use function get_class;
use function preg_match; use function preg_match;
@ -64,7 +63,6 @@ class Network{
private $logger; private $logger;
public function __construct(\Logger $logger){ public function __construct(\Logger $logger){
PacketPool::init();
$this->sessionManager = new NetworkSessionManager(); $this->sessionManager = new NetworkSessionManager();
$this->logger = $logger; $this->logger = $logger;
} }

View File

@ -39,7 +39,7 @@ class PacketBatch extends NetworkBinaryStream{
* @throws BinaryDataException * @throws BinaryDataException
*/ */
public function getPacket() : Packet{ public function getPacket() : Packet{
return PacketPool::getPacket($this->getString()); return PacketPool::getInstance()->getPacket($this->getString());
} }
/** /**

View File

@ -27,170 +27,180 @@ use pocketmine\utils\Binary;
use pocketmine\utils\BinaryDataException; use pocketmine\utils\BinaryDataException;
class PacketPool{ class PacketPool{
/** @var self|null */
protected static $instance = null;
public static function getInstance() : self{
if(self::$instance === null){
self::$instance = new self;
}
return self::$instance;
}
/** @var \SplFixedArray<Packet> */ /** @var \SplFixedArray<Packet> */
protected static $pool; protected $pool;
public static function init() : void{ public function __construct(){
static::$pool = new \SplFixedArray(256); $this->pool = new \SplFixedArray(256);
static::registerPacket(new LoginPacket()); $this->registerPacket(new LoginPacket());
static::registerPacket(new PlayStatusPacket()); $this->registerPacket(new PlayStatusPacket());
static::registerPacket(new ServerToClientHandshakePacket()); $this->registerPacket(new ServerToClientHandshakePacket());
static::registerPacket(new ClientToServerHandshakePacket()); $this->registerPacket(new ClientToServerHandshakePacket());
static::registerPacket(new DisconnectPacket()); $this->registerPacket(new DisconnectPacket());
static::registerPacket(new ResourcePacksInfoPacket()); $this->registerPacket(new ResourcePacksInfoPacket());
static::registerPacket(new ResourcePackStackPacket()); $this->registerPacket(new ResourcePackStackPacket());
static::registerPacket(new ResourcePackClientResponsePacket()); $this->registerPacket(new ResourcePackClientResponsePacket());
static::registerPacket(new TextPacket()); $this->registerPacket(new TextPacket());
static::registerPacket(new SetTimePacket()); $this->registerPacket(new SetTimePacket());
static::registerPacket(new StartGamePacket()); $this->registerPacket(new StartGamePacket());
static::registerPacket(new AddPlayerPacket()); $this->registerPacket(new AddPlayerPacket());
static::registerPacket(new AddActorPacket()); $this->registerPacket(new AddActorPacket());
static::registerPacket(new RemoveActorPacket()); $this->registerPacket(new RemoveActorPacket());
static::registerPacket(new AddItemActorPacket()); $this->registerPacket(new AddItemActorPacket());
static::registerPacket(new TakeItemActorPacket()); $this->registerPacket(new TakeItemActorPacket());
static::registerPacket(new MoveActorAbsolutePacket()); $this->registerPacket(new MoveActorAbsolutePacket());
static::registerPacket(new MovePlayerPacket()); $this->registerPacket(new MovePlayerPacket());
static::registerPacket(new RiderJumpPacket()); $this->registerPacket(new RiderJumpPacket());
static::registerPacket(new UpdateBlockPacket()); $this->registerPacket(new UpdateBlockPacket());
static::registerPacket(new AddPaintingPacket()); $this->registerPacket(new AddPaintingPacket());
static::registerPacket(new TickSyncPacket()); $this->registerPacket(new TickSyncPacket());
static::registerPacket(new LevelSoundEventPacketV1()); $this->registerPacket(new LevelSoundEventPacketV1());
static::registerPacket(new LevelEventPacket()); $this->registerPacket(new LevelEventPacket());
static::registerPacket(new BlockEventPacket()); $this->registerPacket(new BlockEventPacket());
static::registerPacket(new ActorEventPacket()); $this->registerPacket(new ActorEventPacket());
static::registerPacket(new MobEffectPacket()); $this->registerPacket(new MobEffectPacket());
static::registerPacket(new UpdateAttributesPacket()); $this->registerPacket(new UpdateAttributesPacket());
static::registerPacket(new InventoryTransactionPacket()); $this->registerPacket(new InventoryTransactionPacket());
static::registerPacket(new MobEquipmentPacket()); $this->registerPacket(new MobEquipmentPacket());
static::registerPacket(new MobArmorEquipmentPacket()); $this->registerPacket(new MobArmorEquipmentPacket());
static::registerPacket(new InteractPacket()); $this->registerPacket(new InteractPacket());
static::registerPacket(new BlockPickRequestPacket()); $this->registerPacket(new BlockPickRequestPacket());
static::registerPacket(new ActorPickRequestPacket()); $this->registerPacket(new ActorPickRequestPacket());
static::registerPacket(new PlayerActionPacket()); $this->registerPacket(new PlayerActionPacket());
static::registerPacket(new ActorFallPacket()); $this->registerPacket(new ActorFallPacket());
static::registerPacket(new HurtArmorPacket()); $this->registerPacket(new HurtArmorPacket());
static::registerPacket(new SetActorDataPacket()); $this->registerPacket(new SetActorDataPacket());
static::registerPacket(new SetActorMotionPacket()); $this->registerPacket(new SetActorMotionPacket());
static::registerPacket(new SetActorLinkPacket()); $this->registerPacket(new SetActorLinkPacket());
static::registerPacket(new SetHealthPacket()); $this->registerPacket(new SetHealthPacket());
static::registerPacket(new SetSpawnPositionPacket()); $this->registerPacket(new SetSpawnPositionPacket());
static::registerPacket(new AnimatePacket()); $this->registerPacket(new AnimatePacket());
static::registerPacket(new RespawnPacket()); $this->registerPacket(new RespawnPacket());
static::registerPacket(new ContainerOpenPacket()); $this->registerPacket(new ContainerOpenPacket());
static::registerPacket(new ContainerClosePacket()); $this->registerPacket(new ContainerClosePacket());
static::registerPacket(new PlayerHotbarPacket()); $this->registerPacket(new PlayerHotbarPacket());
static::registerPacket(new InventoryContentPacket()); $this->registerPacket(new InventoryContentPacket());
static::registerPacket(new InventorySlotPacket()); $this->registerPacket(new InventorySlotPacket());
static::registerPacket(new ContainerSetDataPacket()); $this->registerPacket(new ContainerSetDataPacket());
static::registerPacket(new CraftingDataPacket()); $this->registerPacket(new CraftingDataPacket());
static::registerPacket(new CraftingEventPacket()); $this->registerPacket(new CraftingEventPacket());
static::registerPacket(new GuiDataPickItemPacket()); $this->registerPacket(new GuiDataPickItemPacket());
static::registerPacket(new AdventureSettingsPacket()); $this->registerPacket(new AdventureSettingsPacket());
static::registerPacket(new BlockActorDataPacket()); $this->registerPacket(new BlockActorDataPacket());
static::registerPacket(new PlayerInputPacket()); $this->registerPacket(new PlayerInputPacket());
static::registerPacket(new LevelChunkPacket()); $this->registerPacket(new LevelChunkPacket());
static::registerPacket(new SetCommandsEnabledPacket()); $this->registerPacket(new SetCommandsEnabledPacket());
static::registerPacket(new SetDifficultyPacket()); $this->registerPacket(new SetDifficultyPacket());
static::registerPacket(new ChangeDimensionPacket()); $this->registerPacket(new ChangeDimensionPacket());
static::registerPacket(new SetPlayerGameTypePacket()); $this->registerPacket(new SetPlayerGameTypePacket());
static::registerPacket(new PlayerListPacket()); $this->registerPacket(new PlayerListPacket());
static::registerPacket(new SimpleEventPacket()); $this->registerPacket(new SimpleEventPacket());
static::registerPacket(new EventPacket()); $this->registerPacket(new EventPacket());
static::registerPacket(new SpawnExperienceOrbPacket()); $this->registerPacket(new SpawnExperienceOrbPacket());
static::registerPacket(new ClientboundMapItemDataPacket()); $this->registerPacket(new ClientboundMapItemDataPacket());
static::registerPacket(new MapInfoRequestPacket()); $this->registerPacket(new MapInfoRequestPacket());
static::registerPacket(new RequestChunkRadiusPacket()); $this->registerPacket(new RequestChunkRadiusPacket());
static::registerPacket(new ChunkRadiusUpdatedPacket()); $this->registerPacket(new ChunkRadiusUpdatedPacket());
static::registerPacket(new ItemFrameDropItemPacket()); $this->registerPacket(new ItemFrameDropItemPacket());
static::registerPacket(new GameRulesChangedPacket()); $this->registerPacket(new GameRulesChangedPacket());
static::registerPacket(new CameraPacket()); $this->registerPacket(new CameraPacket());
static::registerPacket(new BossEventPacket()); $this->registerPacket(new BossEventPacket());
static::registerPacket(new ShowCreditsPacket()); $this->registerPacket(new ShowCreditsPacket());
static::registerPacket(new AvailableCommandsPacket()); $this->registerPacket(new AvailableCommandsPacket());
static::registerPacket(new CommandRequestPacket()); $this->registerPacket(new CommandRequestPacket());
static::registerPacket(new CommandBlockUpdatePacket()); $this->registerPacket(new CommandBlockUpdatePacket());
static::registerPacket(new CommandOutputPacket()); $this->registerPacket(new CommandOutputPacket());
static::registerPacket(new UpdateTradePacket()); $this->registerPacket(new UpdateTradePacket());
static::registerPacket(new UpdateEquipPacket()); $this->registerPacket(new UpdateEquipPacket());
static::registerPacket(new ResourcePackDataInfoPacket()); $this->registerPacket(new ResourcePackDataInfoPacket());
static::registerPacket(new ResourcePackChunkDataPacket()); $this->registerPacket(new ResourcePackChunkDataPacket());
static::registerPacket(new ResourcePackChunkRequestPacket()); $this->registerPacket(new ResourcePackChunkRequestPacket());
static::registerPacket(new TransferPacket()); $this->registerPacket(new TransferPacket());
static::registerPacket(new PlaySoundPacket()); $this->registerPacket(new PlaySoundPacket());
static::registerPacket(new StopSoundPacket()); $this->registerPacket(new StopSoundPacket());
static::registerPacket(new SetTitlePacket()); $this->registerPacket(new SetTitlePacket());
static::registerPacket(new AddBehaviorTreePacket()); $this->registerPacket(new AddBehaviorTreePacket());
static::registerPacket(new StructureBlockUpdatePacket()); $this->registerPacket(new StructureBlockUpdatePacket());
static::registerPacket(new ShowStoreOfferPacket()); $this->registerPacket(new ShowStoreOfferPacket());
static::registerPacket(new PurchaseReceiptPacket()); $this->registerPacket(new PurchaseReceiptPacket());
static::registerPacket(new PlayerSkinPacket()); $this->registerPacket(new PlayerSkinPacket());
static::registerPacket(new SubClientLoginPacket()); $this->registerPacket(new SubClientLoginPacket());
static::registerPacket(new AutomationClientConnectPacket()); $this->registerPacket(new AutomationClientConnectPacket());
static::registerPacket(new SetLastHurtByPacket()); $this->registerPacket(new SetLastHurtByPacket());
static::registerPacket(new BookEditPacket()); $this->registerPacket(new BookEditPacket());
static::registerPacket(new NpcRequestPacket()); $this->registerPacket(new NpcRequestPacket());
static::registerPacket(new PhotoTransferPacket()); $this->registerPacket(new PhotoTransferPacket());
static::registerPacket(new ModalFormRequestPacket()); $this->registerPacket(new ModalFormRequestPacket());
static::registerPacket(new ModalFormResponsePacket()); $this->registerPacket(new ModalFormResponsePacket());
static::registerPacket(new ServerSettingsRequestPacket()); $this->registerPacket(new ServerSettingsRequestPacket());
static::registerPacket(new ServerSettingsResponsePacket()); $this->registerPacket(new ServerSettingsResponsePacket());
static::registerPacket(new ShowProfilePacket()); $this->registerPacket(new ShowProfilePacket());
static::registerPacket(new SetDefaultGameTypePacket()); $this->registerPacket(new SetDefaultGameTypePacket());
static::registerPacket(new RemoveObjectivePacket()); $this->registerPacket(new RemoveObjectivePacket());
static::registerPacket(new SetDisplayObjectivePacket()); $this->registerPacket(new SetDisplayObjectivePacket());
static::registerPacket(new SetScorePacket()); $this->registerPacket(new SetScorePacket());
static::registerPacket(new LabTablePacket()); $this->registerPacket(new LabTablePacket());
static::registerPacket(new UpdateBlockSyncedPacket()); $this->registerPacket(new UpdateBlockSyncedPacket());
static::registerPacket(new MoveActorDeltaPacket()); $this->registerPacket(new MoveActorDeltaPacket());
static::registerPacket(new SetScoreboardIdentityPacket()); $this->registerPacket(new SetScoreboardIdentityPacket());
static::registerPacket(new SetLocalPlayerAsInitializedPacket()); $this->registerPacket(new SetLocalPlayerAsInitializedPacket());
static::registerPacket(new UpdateSoftEnumPacket()); $this->registerPacket(new UpdateSoftEnumPacket());
static::registerPacket(new NetworkStackLatencyPacket()); $this->registerPacket(new NetworkStackLatencyPacket());
static::registerPacket(new ScriptCustomEventPacket()); $this->registerPacket(new ScriptCustomEventPacket());
static::registerPacket(new SpawnParticleEffectPacket()); $this->registerPacket(new SpawnParticleEffectPacket());
static::registerPacket(new AvailableActorIdentifiersPacket()); $this->registerPacket(new AvailableActorIdentifiersPacket());
static::registerPacket(new LevelSoundEventPacketV2()); $this->registerPacket(new LevelSoundEventPacketV2());
static::registerPacket(new NetworkChunkPublisherUpdatePacket()); $this->registerPacket(new NetworkChunkPublisherUpdatePacket());
static::registerPacket(new BiomeDefinitionListPacket()); $this->registerPacket(new BiomeDefinitionListPacket());
static::registerPacket(new LevelSoundEventPacket()); $this->registerPacket(new LevelSoundEventPacket());
static::registerPacket(new LevelEventGenericPacket()); $this->registerPacket(new LevelEventGenericPacket());
static::registerPacket(new LecternUpdatePacket()); $this->registerPacket(new LecternUpdatePacket());
static::registerPacket(new VideoStreamConnectPacket()); $this->registerPacket(new VideoStreamConnectPacket());
static::registerPacket(new AddEntityPacket()); $this->registerPacket(new AddEntityPacket());
static::registerPacket(new RemoveEntityPacket()); $this->registerPacket(new RemoveEntityPacket());
static::registerPacket(new ClientCacheStatusPacket()); $this->registerPacket(new ClientCacheStatusPacket());
static::registerPacket(new OnScreenTextureAnimationPacket()); $this->registerPacket(new OnScreenTextureAnimationPacket());
static::registerPacket(new MapCreateLockedCopyPacket()); $this->registerPacket(new MapCreateLockedCopyPacket());
static::registerPacket(new StructureTemplateDataRequestPacket()); $this->registerPacket(new StructureTemplateDataRequestPacket());
static::registerPacket(new StructureTemplateDataResponsePacket()); $this->registerPacket(new StructureTemplateDataResponsePacket());
static::registerPacket(new UpdateBlockPropertiesPacket()); $this->registerPacket(new UpdateBlockPropertiesPacket());
static::registerPacket(new ClientCacheBlobStatusPacket()); $this->registerPacket(new ClientCacheBlobStatusPacket());
static::registerPacket(new ClientCacheMissResponsePacket()); $this->registerPacket(new ClientCacheMissResponsePacket());
static::registerPacket(new EducationSettingsPacket()); $this->registerPacket(new EducationSettingsPacket());
static::registerPacket(new EmotePacket()); $this->registerPacket(new EmotePacket());
static::registerPacket(new MultiplayerSettingsPacket()); $this->registerPacket(new MultiplayerSettingsPacket());
static::registerPacket(new SettingsCommandPacket()); $this->registerPacket(new SettingsCommandPacket());
static::registerPacket(new AnvilDamagePacket()); $this->registerPacket(new AnvilDamagePacket());
static::registerPacket(new CompletedUsingItemPacket()); $this->registerPacket(new CompletedUsingItemPacket());
static::registerPacket(new NetworkSettingsPacket()); $this->registerPacket(new NetworkSettingsPacket());
static::registerPacket(new PlayerAuthInputPacket()); $this->registerPacket(new PlayerAuthInputPacket());
} }
public static function registerPacket(Packet $packet) : void{ public function registerPacket(Packet $packet) : void{
static::$pool[$packet->pid()] = clone $packet; $this->pool[$packet->pid()] = clone $packet;
} }
public static function getPacketById(int $pid) : Packet{ public function getPacketById(int $pid) : Packet{
return isset(static::$pool[$pid]) ? clone static::$pool[$pid] : new UnknownPacket(); return isset($this->pool[$pid]) ? clone $this->pool[$pid] : new UnknownPacket();
} }
/** /**
* @throws BinaryDataException * @throws BinaryDataException
*/ */
public static function getPacket(string $buffer) : Packet{ public function getPacket(string $buffer) : Packet{
$offset = 0; $offset = 0;
$pk = static::getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK); $pk = $this->getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK);
$pk->getBinaryStream()->setBuffer($buffer, $offset); $pk->getBinaryStream()->setBuffer($buffer, $offset);
return $pk; return $pk;