diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 62fe1e182..d51cfd50e 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1486,7 +1486,18 @@ class Server{ $this->scheduler = new ServerScheduler(); 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(); diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index e6ebfcfa9..9f90ca11a 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -47,23 +47,23 @@ class RCON{ $this->password = (string) $password; $this->server->getLogger()->info("Starting remote control listener"); if($this->password === ""){ - $this->server->getLogger()->critical("RCON can't be started: Empty password"); - - return; + throw new \InvalidArgumentException("Empty password"); } + $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)){ - $this->server->getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error())); - $this->threads = 0; - return; + + if($this->socket === false or !@socket_bind($this->socket, $interface, (int) $port) or !@socket_listen($this->socket)){ + throw new \RuntimeException(trim(socket_strerror(socket_last_error()))); } + socket_set_block($this->socket); for($n = 0; $n < $this->threads; ++$n){ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); } + socket_getsockname($this->socket, $addr, $port); $this->server->getLogger()->info("RCON running on $addr:$port"); }