remove NullPacketHandler

this is a waste of LOC
This commit is contained in:
Dylan K. Taylor 2020-04-29 13:14:23 +01:00
parent b6214744d5
commit 549940d8a7
3 changed files with 10 additions and 49 deletions

View File

@ -46,7 +46,6 @@ use pocketmine\network\mcpe\handler\DeathPacketHandler;
use pocketmine\network\mcpe\handler\HandshakePacketHandler; use pocketmine\network\mcpe\handler\HandshakePacketHandler;
use pocketmine\network\mcpe\handler\InGamePacketHandler; use pocketmine\network\mcpe\handler\InGamePacketHandler;
use pocketmine\network\mcpe\handler\LoginPacketHandler; use pocketmine\network\mcpe\handler\LoginPacketHandler;
use pocketmine\network\mcpe\handler\NullPacketHandler;
use pocketmine\network\mcpe\handler\PacketHandler; use pocketmine\network\mcpe\handler\PacketHandler;
use pocketmine\network\mcpe\handler\PreSpawnPacketHandler; use pocketmine\network\mcpe\handler\PreSpawnPacketHandler;
use pocketmine\network\mcpe\handler\ResourcePacksPacketHandler; use pocketmine\network\mcpe\handler\ResourcePacksPacketHandler;
@ -126,8 +125,8 @@ class NetworkSession{
/** @var int|null */ /** @var int|null */
private $ping = null; private $ping = null;
/** @var PacketHandler */ /** @var PacketHandler|null */
private $handler; private $handler = null;
/** @var bool */ /** @var bool */
private $connected = true; private $connected = true;
@ -258,14 +257,16 @@ class NetworkSession{
$this->ping = $ping; $this->ping = $ping;
} }
public function getHandler() : PacketHandler{ public function getHandler() : ?PacketHandler{
return $this->handler; return $this->handler;
} }
public function setHandler(PacketHandler $handler) : void{ public function setHandler(?PacketHandler $handler) : void{
if($this->connected){ //TODO: this is fine since we can't handle anything from a disconnected session, but it might produce surprises in some cases if($this->connected){ //TODO: this is fine since we can't handle anything from a disconnected session, but it might produce surprises in some cases
$this->handler = $handler; $this->handler = $handler;
$this->handler->setUp(); if($this->handler !== null){
$this->handler->setUp();
}
} }
} }
@ -350,7 +351,7 @@ class NetworkSession{
$ev = new DataPacketReceiveEvent($this, $packet); $ev = new DataPacketReceiveEvent($this, $packet);
$ev->call(); $ev->call();
if(!$ev->isCancelled() and !$packet->handle($this->handler)){ if(!$ev->isCancelled() and ($this->handler === null or !$packet->handle($this->handler))){
$this->logger->debug("Unhandled " . $packet->getName() . ": " . base64_encode($stream->getBuffer())); $this->logger->debug("Unhandled " . $packet->getName() . ": " . base64_encode($stream->getBuffer()));
} }
}finally{ }finally{
@ -459,7 +460,7 @@ class NetworkSession{
$this->disconnectGuard = true; $this->disconnectGuard = true;
$func(); $func();
$this->disconnectGuard = false; $this->disconnectGuard = false;
$this->setHandler(NullPacketHandler::getInstance()); $this->setHandler(null);
$this->connected = false; $this->connected = false;
$this->manager->remove($this); $this->manager->remove($this);
$this->logger->info("Session closed due to $reason"); $this->logger->info("Session closed due to $reason");

View File

@ -195,7 +195,7 @@ class LoginPacketHandler extends PacketHandler{
*/ */
protected function processLogin(LoginPacket $packet, bool $authRequired) : void{ protected function processLogin(LoginPacket $packet, bool $authRequired) : void{
$this->server->getAsyncPool()->submitTask(new ProcessLoginTask($packet, $authRequired, $this->authCallback)); $this->server->getAsyncPool()->submitTask(new ProcessLoginTask($packet, $authRequired, $this->authCallback));
$this->session->setHandler(NullPacketHandler::getInstance()); //drop packets received during login verification $this->session->setHandler(null); //drop packets received during login verification
} }
protected function isCompatibleProtocol(int $protocolVersion) : bool{ protected function isCompatibleProtocol(int $protocolVersion) : bool{

View File

@ -1,40 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\handler;
/**
* Handler which simply ignores all packets received.
*/
final class NullPacketHandler extends PacketHandler{
/** @var self|null */
private static $instance = null;
public static function getInstance() : self{
return self::$instance ?? (self::$instance = new self);
}
private function __construct(){
}
}