Fixed RCON usage

This commit is contained in:
Shoghi Cervantes 2014-08-10 18:35:01 +02:00
parent fee7f5060b
commit b17ce16262
3 changed files with 20 additions and 19 deletions

View File

@ -1430,7 +1430,7 @@ class Server{
$this->scheduler = new ServerScheduler(); $this->scheduler = new ServerScheduler();
if($this->getConfigBoolean("enable-rcon", false) === true){ 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); $this->maxPlayers = $this->getConfigInt("max-players", 20);

View File

@ -33,20 +33,21 @@ use pocketmine\utils\TextFormat;
class RCON{ class RCON{
/** @var Server */
private $server;
private $socket; private $socket;
private $password; private $password;
/** @var RCONInstance[] */ /** @var RCONInstance[] */
private $workers; private $workers;
private $threads;
private $clientsPerThread; 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->workers = [];
$this->password = (string) $password; $this->password = (string) $password;
MainLogger::getLogger()->info("Starting remote control listener"); $this->server->getLogger()->info("Starting remote control listener");
if($this->password === ""){ 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; return;
} }
@ -54,7 +55,7 @@ class RCON{
$this->clientsPerThread = (int) max(1, $clientsPerThread); $this->clientsPerThread = (int) max(1, $clientsPerThread);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $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)){ 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; return;
} }
@ -64,8 +65,8 @@ class RCON{
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
} }
@socket_getsockname($this->socket, $addr, $port); @socket_getsockname($this->socket, $addr, $port);
MainLogger::getLogger()->info("RCON running on $addr:$port"); $this->server->getLogger()->info("RCON running on $addr:$port");
Server::getInstance()->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this, "check")), 3); $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this, "check")), 3);
} }
public function stop(){ public function stop(){
@ -84,14 +85,14 @@ class RCON{
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
}elseif($this->workers[$n]->isWaiting()){ }elseif($this->workers[$n]->isWaiting()){
if($this->workers[$n]->response !== ""){ if($this->workers[$n]->response !== ""){
MainLogger::getLogger()->info($this->workers[$n]->response); $this->server->getLogger()->info($this->workers[$n]->response);
$this->workers[$n]->synchronized(function($thread){ $this->workers[$n]->synchronized(function(RCONInstance $thread){
$thread->notify(); $thread->notify();
}, $this->workers[$n]); }, $this->workers[$n]);
}else{ }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]->response = TextFormat::clean($response->getMessage());
$this->workers[$n]->synchronized(function($thread){ $this->workers[$n]->synchronized(function(RCONInstance $thread){
$thread->notify(); $thread->notify();
}, $this->workers[$n]); }, $this->workers[$n]);
} }

View File

@ -133,9 +133,9 @@ class RCONInstance extends \Thread{
if($payload === $this->password){ if($payload === $this->password){
@socket_getpeername($client, $addr, $port); @socket_getpeername($client, $addr, $port);
$this->response = "[INFO] Successful Rcon connection from: /$addr:$port"; $this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
$this->synchronized(function($thread){ $this->synchronized(function(){
$thread->wait(); $this->wait();
}, $this); });
$this->response = ""; $this->response = "";
$this->writePacket($client, $requestID, 2, ""); $this->writePacket($client, $requestID, 2, "");
$this->{"status" . $n} = 1; $this->{"status" . $n} = 1;
@ -152,9 +152,9 @@ class RCONInstance extends \Thread{
} }
if(strlen($payload) > 0){ if(strlen($payload) > 0){
$this->cmd = ltrim($payload); $this->cmd = ltrim($payload);
$this->synchronized(function($thread){ $this->synchronized(function(){
$thread->wait(); $this->wait();
}, $this); });
$this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response))); $this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
$this->response = ""; $this->response = "";
$this->cmd = ""; $this->cmd = "";