Skip to content

Commit

Permalink
Fixed RCON usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Aug 10, 2014
1 parent fee7f50 commit b17ce16
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/pocketmine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ public function __construct(\SplClassLoader $autoloader, \ThreadedLogger $logger
$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);
Expand Down
25 changes: 13 additions & 12 deletions src/pocketmine/network/rcon/RCON.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,29 @@


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;
}
$this->threads = (int) max(1, $threads);
$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;
}
Expand All @@ -64,8 +65,8 @@ public function __construct($password, $port = 19132, $interface = "0.0.0.0", $t
$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(){
Expand All @@ -84,14 +85,14 @@ public function check(){
$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]);
}
Expand Down
12 changes: 6 additions & 6 deletions src/pocketmine/network/rcon/RCONInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public function run(){
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;
Expand All @@ -152,9 +152,9 @@ public function run(){
}
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 = "";
Expand Down

0 comments on commit b17ce16

Please sign in to comment.