Use short class names for unhandled packet logging, added some documentation

This commit is contained in:
Dylan K. Taylor 2017-04-27 12:16:24 +01:00
parent 6ef132e468
commit 84ec944b6b
4 changed files with 20 additions and 9 deletions

View File

@ -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();

View File

@ -292,6 +292,4 @@ interface NetworkSession{
public function handleStopSound(StopSoundPacket $packet) : bool;
public function handleSetTitle(SetTitlePacket $packet) : bool;
public function handleUnknown(UnknownPacket $packet) : bool;
}

View File

@ -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(){

View File

@ -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;
}
}