mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-25 12:54:03 +00:00
NetworkSession: add a dedicated PrefixedLogger, clean up some boilerplate code
This commit is contained in:
parent
7ae84944ca
commit
5250a432d1
@ -83,6 +83,8 @@ use function time;
|
|||||||
use function ucfirst;
|
use function ucfirst;
|
||||||
|
|
||||||
class NetworkSession{
|
class NetworkSession{
|
||||||
|
/** @var \PrefixedLogger */
|
||||||
|
private $logger;
|
||||||
/** @var Server */
|
/** @var Server */
|
||||||
private $server;
|
private $server;
|
||||||
/** @var Player|null */
|
/** @var Player|null */
|
||||||
@ -130,6 +132,8 @@ class NetworkSession{
|
|||||||
$this->ip = $ip;
|
$this->ip = $ip;
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
|
|
||||||
|
$this->logger = new \PrefixedLogger($this->server->getLogger(), $this->getLogPrefix());
|
||||||
|
|
||||||
$this->compressedQueue = new \SplQueue();
|
$this->compressedQueue = new \SplQueue();
|
||||||
|
|
||||||
$this->connectTime = time();
|
$this->connectTime = time();
|
||||||
@ -139,6 +143,14 @@ class NetworkSession{
|
|||||||
$this->manager->add($this);
|
$this->manager->add($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getLogPrefix() : string{
|
||||||
|
return "NetworkSession: " . $this->getDisplayName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLogger() : \Logger{
|
||||||
|
return $this->logger;
|
||||||
|
}
|
||||||
|
|
||||||
protected function createPlayer() : void{
|
protected function createPlayer() : void{
|
||||||
$ev = new PlayerCreationEvent($this);
|
$ev = new PlayerCreationEvent($this);
|
||||||
$ev->call();
|
$ev->call();
|
||||||
@ -170,6 +182,7 @@ class NetworkSession{
|
|||||||
throw new \InvalidStateException("Player info has already been set");
|
throw new \InvalidStateException("Player info has already been set");
|
||||||
}
|
}
|
||||||
$this->info = $info;
|
$this->info = $info;
|
||||||
|
$this->logger->setPrefix($this->getLogPrefix());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isConnected() : bool{
|
public function isConnected() : bool{
|
||||||
@ -242,7 +255,7 @@ class NetworkSession{
|
|||||||
try{
|
try{
|
||||||
$payload = $this->cipher->decrypt($payload);
|
$payload = $this->cipher->decrypt($payload);
|
||||||
}catch(\UnexpectedValueException $e){
|
}catch(\UnexpectedValueException $e){
|
||||||
$this->server->getLogger()->debug("Encrypted packet from " . $this->getDisplayName() . ": " . bin2hex($payload));
|
$this->logger->debug("Encrypted packet: " . bin2hex($payload));
|
||||||
throw new BadPacketException("Packet decryption error: " . $e->getMessage(), 0, $e);
|
throw new BadPacketException("Packet decryption error: " . $e->getMessage(), 0, $e);
|
||||||
}finally{
|
}finally{
|
||||||
Timings::$playerNetworkReceiveDecryptTimer->stopTiming();
|
Timings::$playerNetworkReceiveDecryptTimer->stopTiming();
|
||||||
@ -253,7 +266,7 @@ class NetworkSession{
|
|||||||
try{
|
try{
|
||||||
$stream = new PacketStream(NetworkCompression::decompress($payload));
|
$stream = new PacketStream(NetworkCompression::decompress($payload));
|
||||||
}catch(\ErrorException $e){
|
}catch(\ErrorException $e){
|
||||||
$this->server->getLogger()->debug("Failed to decompress packet from " . $this->getDisplayName() . ": " . bin2hex($payload));
|
$this->logger->debug("Failed to decompress packet: " . bin2hex($payload));
|
||||||
//TODO: this isn't incompatible game version if we already established protocol version
|
//TODO: this isn't incompatible game version if we already established protocol version
|
||||||
throw new BadPacketException("Compressed packet batch decode error (incompatible game version?)", 0, $e);
|
throw new BadPacketException("Compressed packet batch decode error (incompatible game version?)", 0, $e);
|
||||||
}finally{
|
}finally{
|
||||||
@ -268,14 +281,14 @@ class NetworkSession{
|
|||||||
try{
|
try{
|
||||||
$pk = PacketPool::getPacket($stream->getString());
|
$pk = PacketPool::getPacket($stream->getString());
|
||||||
}catch(BinaryDataException $e){
|
}catch(BinaryDataException $e){
|
||||||
$this->server->getLogger()->debug("Packet batch from " . $this->getDisplayName() . ": " . bin2hex($stream->getBuffer()));
|
$this->logger->debug("Packet batch: " . bin2hex($stream->getBuffer()));
|
||||||
throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e);
|
throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
$this->handleDataPacket($pk);
|
$this->handleDataPacket($pk);
|
||||||
}catch(BadPacketException $e){
|
}catch(BadPacketException $e){
|
||||||
$this->server->getLogger()->debug($pk->getName() . " from " . $this->getDisplayName() . ": " . bin2hex($pk->getBuffer()));
|
$this->logger->debug($pk->getName() . ": " . bin2hex($pk->getBuffer()));
|
||||||
throw new BadPacketException("Error processing " . $pk->getName() . ": " . $e->getMessage(), 0, $e);
|
throw new BadPacketException("Error processing " . $pk->getName() . ": " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -298,13 +311,13 @@ class NetworkSession{
|
|||||||
$packet->decode();
|
$packet->decode();
|
||||||
if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
|
if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
|
||||||
$remains = substr($packet->getBuffer(), $packet->getOffset());
|
$remains = substr($packet->getBuffer(), $packet->getOffset());
|
||||||
$this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": " . bin2hex($remains));
|
$this->logger->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": " . bin2hex($remains));
|
||||||
}
|
}
|
||||||
|
|
||||||
$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 !$packet->handle($this->handler)){
|
||||||
$this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->getDisplayName() . ": " . bin2hex($packet->getBuffer()));
|
$this->logger->debug("Unhandled " . $packet->getName() . ": " . bin2hex($packet->getBuffer()));
|
||||||
}
|
}
|
||||||
}finally{
|
}finally{
|
||||||
$timings->stopTiming();
|
$timings->stopTiming();
|
||||||
@ -511,13 +524,11 @@ class NetworkSession{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->server->getLogger()->debug($this->getDisplayName() . " is NOT logged into Xbox Live");
|
|
||||||
if($this->info->getXuid() !== ""){
|
if($this->info->getXuid() !== ""){
|
||||||
$this->server->getLogger()->warning($this->getDisplayName() . " has an XUID, but their login keychain is not signed by Mojang");
|
$this->logger->warning("Found XUID, but login keychain is not signed by Mojang");
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
$this->server->getLogger()->debug($this->getDisplayName() . " is logged into Xbox Live");
|
|
||||||
}
|
}
|
||||||
|
$this->logger->debug("Xbox Live authenticated: " . ($this->authenticated ? "YES" : "NO"));
|
||||||
|
|
||||||
return $this->manager->kickDuplicates($this);
|
return $this->manager->kickDuplicates($this);
|
||||||
}
|
}
|
||||||
@ -530,7 +541,7 @@ class NetworkSession{
|
|||||||
$this->cipher = new NetworkCipher($encryptionKey);
|
$this->cipher = new NetworkCipher($encryptionKey);
|
||||||
|
|
||||||
$this->setHandler(new HandshakeSessionHandler($this));
|
$this->setHandler(new HandshakeSessionHandler($this));
|
||||||
$this->server->getLogger()->debug("Enabled encryption for " . $this->getDisplayName());
|
$this->logger->debug("Enabled encryption");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onLoginSuccess() : void{
|
public function onLoginSuccess() : void{
|
||||||
@ -540,7 +551,7 @@ class NetworkSession{
|
|||||||
$pk->status = PlayStatusPacket::LOGIN_SUCCESS;
|
$pk->status = PlayStatusPacket::LOGIN_SUCCESS;
|
||||||
$this->sendDataPacket($pk);
|
$this->sendDataPacket($pk);
|
||||||
|
|
||||||
$this->setHandler(new ResourcePacksSessionHandler($this->server, $this, $this->server->getResourcePackManager()));
|
$this->setHandler(new ResourcePacksSessionHandler($this, $this->server->getResourcePackManager()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onResourcePacksDone() : void{
|
public function onResourcePacksDone() : void{
|
||||||
|
@ -246,7 +246,7 @@ class ProcessLoginTask extends AsyncTask{
|
|||||||
/** @var NetworkSession $session */
|
/** @var NetworkSession $session */
|
||||||
$session = $this->fetchLocal(self::TLS_KEY_SESSION);
|
$session = $this->fetchLocal(self::TLS_KEY_SESSION);
|
||||||
if(!$session->isConnected()){
|
if(!$session->isConnected()){
|
||||||
$this->worker->getLogger()->error("Player " . $session->getDisplayName() . " was disconnected before their login could be verified");
|
$session->getLogger()->debug("Disconnected before login could be verified");
|
||||||
}elseif($session->setAuthenticationStatus($this->authenticated, $this->authRequired, $this->error)){
|
}elseif($session->setAuthenticationStatus($this->authenticated, $this->authRequired, $this->error)){
|
||||||
if(!$this->useEncryption){
|
if(!$this->useEncryption){
|
||||||
$session->onLoginSuccess();
|
$session->onLoginSuccess();
|
||||||
|
@ -32,7 +32,6 @@ use pocketmine\network\mcpe\protocol\ResourcePacksInfoPacket;
|
|||||||
use pocketmine\network\mcpe\protocol\ResourcePackStackPacket;
|
use pocketmine\network\mcpe\protocol\ResourcePackStackPacket;
|
||||||
use pocketmine\resourcepacks\ResourcePack;
|
use pocketmine\resourcepacks\ResourcePack;
|
||||||
use pocketmine\resourcepacks\ResourcePackManager;
|
use pocketmine\resourcepacks\ResourcePackManager;
|
||||||
use pocketmine\Server;
|
|
||||||
use function ceil;
|
use function ceil;
|
||||||
use function implode;
|
use function implode;
|
||||||
use function strpos;
|
use function strpos;
|
||||||
@ -45,8 +44,6 @@ use function substr;
|
|||||||
class ResourcePacksSessionHandler extends SessionHandler{
|
class ResourcePacksSessionHandler extends SessionHandler{
|
||||||
private const PACK_CHUNK_SIZE = 1048576; //1MB
|
private const PACK_CHUNK_SIZE = 1048576; //1MB
|
||||||
|
|
||||||
/** @var Server */
|
|
||||||
private $server;
|
|
||||||
/** @var NetworkSession */
|
/** @var NetworkSession */
|
||||||
private $session;
|
private $session;
|
||||||
/** @var ResourcePackManager */
|
/** @var ResourcePackManager */
|
||||||
@ -56,8 +53,7 @@ class ResourcePacksSessionHandler extends SessionHandler{
|
|||||||
private $downloadedChunks = [];
|
private $downloadedChunks = [];
|
||||||
|
|
||||||
|
|
||||||
public function __construct(Server $server, NetworkSession $session, ResourcePackManager $resourcePackManager){
|
public function __construct(NetworkSession $session, ResourcePackManager $resourcePackManager){
|
||||||
$this->server = $server;
|
|
||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
$this->resourcePackManager = $resourcePackManager;
|
$this->resourcePackManager = $resourcePackManager;
|
||||||
}
|
}
|
||||||
@ -70,7 +66,7 @@ class ResourcePacksSessionHandler extends SessionHandler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function disconnectWithError(string $error) : void{
|
private function disconnectWithError(string $error) : void{
|
||||||
$this->server->getLogger()->error("Error while downloading resource packs for " . $this->session->getDisplayName() . ": " . $error);
|
$this->session->getLogger()->error("Error downloading resource packs: " . $error);
|
||||||
$this->session->disconnect("disconnectionScreen.resourcePack");
|
$this->session->disconnect("disconnectionScreen.resourcePack");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
$actions[] = $action;
|
$actions[] = $action;
|
||||||
}
|
}
|
||||||
}catch(\UnexpectedValueException $e){
|
}catch(\UnexpectedValueException $e){
|
||||||
$this->player->getServer()->getLogger()->debug("Unhandled inventory action from " . $this->player->getName() . ": " . $e->getMessage());
|
$this->session->getLogger()->debug("Unhandled inventory action: " . $e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -220,7 +220,7 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
try{
|
try{
|
||||||
$this->craftingTransaction->execute();
|
$this->craftingTransaction->execute();
|
||||||
}catch(TransactionValidationException $e){
|
}catch(TransactionValidationException $e){
|
||||||
$this->player->getServer()->getLogger()->debug("Failed to execute crafting transaction for " . $this->player->getName() . ": " . $e->getMessage());
|
$this->session->getLogger()->debug("Failed to execute crafting transaction: " . $e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
}finally{
|
}finally{
|
||||||
$this->craftingTransaction = null;
|
$this->craftingTransaction = null;
|
||||||
@ -229,7 +229,7 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
}else{
|
}else{
|
||||||
//normal transaction fallthru
|
//normal transaction fallthru
|
||||||
if($this->craftingTransaction !== null){
|
if($this->craftingTransaction !== null){
|
||||||
$this->player->getServer()->getLogger()->debug("Got unexpected normal inventory action with incomplete crafting transaction from " . $this->player->getName() . ", refusing to execute crafting");
|
$this->session->getLogger()->debug("Got unexpected normal inventory action with incomplete crafting transaction, refusing to execute crafting");
|
||||||
$this->craftingTransaction = null;
|
$this->craftingTransaction = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -238,8 +238,8 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
try{
|
try{
|
||||||
$transaction->execute();
|
$transaction->execute();
|
||||||
}catch(TransactionValidationException $e){
|
}catch(TransactionValidationException $e){
|
||||||
$logger = $this->player->getServer()->getLogger();
|
$logger = $this->session->getLogger();
|
||||||
$logger->debug("Failed to execute inventory transaction from " . $this->player->getName() . ": " . $e->getMessage());
|
$logger->debug("Failed to execute inventory transaction: " . $e->getMessage());
|
||||||
$logger->debug("Actions: " . json_encode($data->getActions()));
|
$logger->debug("Actions: " . json_encode($data->getActions()));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -393,7 +393,7 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
//TODO: handle this when it doesn't spam every damn tick (yet another spam bug!!)
|
//TODO: handle this when it doesn't spam every damn tick (yet another spam bug!!)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->player->getServer()->getLogger()->debug("Unhandled/unknown player action type " . $packet->action . " from " . $this->player->getName());
|
$this->session->getLogger()->debug("Unhandled/unknown player action type " . $packet->action);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,7 +475,7 @@ class SimpleSessionHandler extends SessionHandler{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->player->getServer()->getLogger()->debug("Invalid sign update data from " . $this->player->getName() . ": " . base64_encode($packet->namedtag));
|
$this->session->getLogger()->debug("Invalid sign update data: " . base64_encode($packet->namedtag));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user