Fixed crashes starting RCON

This commit is contained in:
Dylan K. Taylor 2017-06-21 18:15:57 +01:00
parent c09a5ab301
commit 8cd31c2dc4
2 changed files with 19 additions and 8 deletions

View File

@ -1486,7 +1486,18 @@ 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, $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)); try{
$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)
);
}catch(\Throwable $e){
$this->getLogger()->critical("RCON can't be started: " . $e->getMessage());
}
} }
$this->entityMetadata = new EntityMetadataStore(); $this->entityMetadata = new EntityMetadataStore();

View File

@ -47,23 +47,23 @@ class RCON{
$this->password = (string) $password; $this->password = (string) $password;
$this->server->getLogger()->info("Starting remote control listener"); $this->server->getLogger()->info("Starting remote control listener");
if($this->password === ""){ if($this->password === ""){
$this->server->getLogger()->critical("RCON can't be started: Empty password"); throw new \InvalidArgumentException("Empty password");
return;
} }
$this->threads = (int) max(1, $threads); $this->threads = (int) max(1, $threads);
$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)){
$this->server->getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error())); if($this->socket === false or !@socket_bind($this->socket, $interface, (int) $port) or !@socket_listen($this->socket)){
$this->threads = 0; throw new \RuntimeException(trim(socket_strerror(socket_last_error())));
return;
} }
socket_set_block($this->socket); socket_set_block($this->socket);
for($n = 0; $n < $this->threads; ++$n){ for($n = 0; $n < $this->threads; ++$n){
$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);
$this->server->getLogger()->info("RCON running on $addr:$port"); $this->server->getLogger()->info("RCON running on $addr:$port");
} }