From 84ec944b6b94ad5b765f3d820c2c865296083df9 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 27 Apr 2017 12:16:24 +0100 Subject: [PATCH] Use short class names for unhandled packet logging, added some documentation --- src/pocketmine/Player.php | 7 +------ src/pocketmine/network/mcpe/NetworkSession.php | 2 -- .../network/mcpe/protocol/DataPacket.php | 14 ++++++++++++++ .../network/mcpe/protocol/UnknownPacket.php | 6 +++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 27f71bef3..c7674a53f 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -3346,11 +3346,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade return false; } - public function handleUnknown(UnknownPacket $packet) : bool{ - $this->server->getLogger()->debug("Received unknown packet from " . $this->getName() . ": 0x" . bin2hex($packet->payload)); - return true; - } - /** * Called when a packet is received from the client. This method will call DataPacketReceiveEvent. * @@ -3369,7 +3364,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet)); if(!$ev->isCancelled() and !$packet->handle($this)){ - $this->server->getLogger()->debug("Unhandled " . get_class($packet) . " received from " . $this->getName() . ": 0x" . bin2hex($packet->buffer)); + $this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->getName() . ": 0x" . bin2hex($packet->buffer)); } $timings->stopTiming(); diff --git a/src/pocketmine/network/mcpe/NetworkSession.php b/src/pocketmine/network/mcpe/NetworkSession.php index 1d1892e0c..959823e1c 100644 --- a/src/pocketmine/network/mcpe/NetworkSession.php +++ b/src/pocketmine/network/mcpe/NetworkSession.php @@ -292,6 +292,4 @@ interface NetworkSession{ public function handleStopSound(StopSoundPacket $packet) : bool; public function handleSetTitle(SetTitlePacket $packet) : bool; - - public function handleUnknown(UnknownPacket $packet) : bool; } \ No newline at end of file diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index a320f5898..f530eb179 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -40,6 +40,10 @@ abstract class DataPacket extends BinaryStream{ return $this::NETWORK_ID; } + public function getName() : string{ + return (new \ReflectionClass($this))->getShortName(); + } + public function canBeBatched() : bool{ return true; } @@ -52,6 +56,16 @@ abstract class DataPacket extends BinaryStream{ abstract public function decode(); + /** + * Performs handling for this packet. Usually you'll want an appropriately named method in the NetworkSession for this. + * + * This method returns a bool to indicate whether the packet was handled or not. If the packet was unhandled, a debug message will be logged with a hexdump of the packet. + * Typically this method returns the return value of the handler in the supplied NetworkSession. See other packets for examples how to implement this. + * + * @param NetworkSession $session + * + * @return bool true if the packet was handled successfully, false if not. + */ abstract public function handle(NetworkSession $session) : bool; public function reset(){ diff --git a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php index 87034a35e..ebedebe64 100644 --- a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php @@ -37,6 +37,10 @@ class UnknownPacket extends DataPacket{ return self::NETWORK_ID; } + public function getName() : string{ + return "unknown packet"; + } + public function decode(){ $this->offset -= 1; //Rewind one byte so we can read the PID $this->payload = $this->get(true); @@ -48,6 +52,6 @@ class UnknownPacket extends DataPacket{ } public function handle(NetworkSession $session) : bool{ - return $session->handleUnknown($this); + return false; } } \ No newline at end of file