From b17ce162628000355e3afc0d8131864b8efd5049 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 10 Aug 2014 18:35:01 +0200 Subject: [PATCH] Fixed RCON usage --- src/pocketmine/Server.php | 2 +- src/pocketmine/network/rcon/RCON.php | 25 ++++++++++---------- src/pocketmine/network/rcon/RCONInstance.php | 12 +++++----- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 2c19835de..d78fb75bf 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1430,7 +1430,7 @@ class Server{ $this->scheduler = new ServerScheduler(); if($this->getConfigBoolean("enable-rcon", false) === true){ - $this->rcon = new RCON($this->getConfigString("rcon.password", ""), $this->getConfigInt("rcon.port", $this->getPort()), ($ip = $this->getIp()) != "" ? $ip : "0.0.0.0", $this->getConfigInt("rcon.threads", 1), $this->getConfigInt("rcon.clients-per-thread", 50)); + $this->rcon = new RCON($this, $this->getConfigString("rcon.password", ""), $this->getConfigInt("rcon.port", $this->getPort()), ($ip = $this->getIp()) != "" ? $ip : "0.0.0.0", $this->getConfigInt("rcon.threads", 1), $this->getConfigInt("rcon.clients-per-thread", 50)); } $this->maxPlayers = $this->getConfigInt("max-players", 20); diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index c49bd2950..ba43c0ac3 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -33,20 +33,21 @@ use pocketmine\utils\TextFormat; class RCON{ + /** @var Server */ + private $server; private $socket; private $password; /** @var RCONInstance[] */ private $workers; - private $threads; private $clientsPerThread; - private $rconSender; - public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ + public function __construct(Server $server, $password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){ + $this->server = $server; $this->workers = []; $this->password = (string) $password; - MainLogger::getLogger()->info("Starting remote control listener"); + $this->server->getLogger()->info("Starting remote control listener"); if($this->password === ""){ - MainLogger::getLogger()->critical("RCON can't be started: Empty password"); + $this->server->getLogger()->critical("RCON can't be started: Empty password"); return; } @@ -54,7 +55,7 @@ class RCON{ $this->clientsPerThread = (int) max(1, $clientsPerThread); $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)){ - MainLogger::getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error())); + $this->server->getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error())); return; } @@ -64,8 +65,8 @@ class RCON{ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); } @socket_getsockname($this->socket, $addr, $port); - MainLogger::getLogger()->info("RCON running on $addr:$port"); - Server::getInstance()->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this, "check")), 3); + $this->server->getLogger()->info("RCON running on $addr:$port"); + $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this, "check")), 3); } public function stop(){ @@ -84,14 +85,14 @@ class RCON{ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); }elseif($this->workers[$n]->isWaiting()){ if($this->workers[$n]->response !== ""){ - MainLogger::getLogger()->info($this->workers[$n]->response); - $this->workers[$n]->synchronized(function($thread){ + $this->server->getLogger()->info($this->workers[$n]->response); + $this->workers[$n]->synchronized(function(RCONInstance $thread){ $thread->notify(); }, $this->workers[$n]); }else{ - Server::getInstance()->dispatchCommand($response = new RemoteConsoleCommandSender(), $this->workers[$n]->cmd); + $this->server->dispatchCommand($response = new RemoteConsoleCommandSender(), $this->workers[$n]->cmd); $this->workers[$n]->response = TextFormat::clean($response->getMessage()); - $this->workers[$n]->synchronized(function($thread){ + $this->workers[$n]->synchronized(function(RCONInstance $thread){ $thread->notify(); }, $this->workers[$n]); } diff --git a/src/pocketmine/network/rcon/RCONInstance.php b/src/pocketmine/network/rcon/RCONInstance.php index fcf6aa461..3b2c788de 100644 --- a/src/pocketmine/network/rcon/RCONInstance.php +++ b/src/pocketmine/network/rcon/RCONInstance.php @@ -133,9 +133,9 @@ class RCONInstance extends \Thread{ if($payload === $this->password){ @socket_getpeername($client, $addr, $port); $this->response = "[INFO] Successful Rcon connection from: /$addr:$port"; - $this->synchronized(function($thread){ - $thread->wait(); - }, $this); + $this->synchronized(function(){ + $this->wait(); + }); $this->response = ""; $this->writePacket($client, $requestID, 2, ""); $this->{"status" . $n} = 1; @@ -152,9 +152,9 @@ class RCONInstance extends \Thread{ } if(strlen($payload) > 0){ $this->cmd = ltrim($payload); - $this->synchronized(function($thread){ - $thread->wait(); - }, $this); + $this->synchronized(function(){ + $this->wait(); + }); $this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response))); $this->response = ""; $this->cmd = "";