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

View File

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