From 310de5a2b211d31cf775d42c38a3e70cf251554c Mon Sep 17 00:00:00 2001 From: marshall Date: Mon, 16 Mar 2020 20:00:45 +0800 Subject: [PATCH] RCON: set SO_REUSEADDR to fix RCON start failure after restart (#3357) --- src/pocketmine/network/rcon/RCON.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index 617f2ea9c..61a123e37 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -41,14 +41,17 @@ use function socket_getsockname; use function socket_last_error; use function socket_listen; use function socket_set_block; +use function socket_set_option; use function socket_strerror; use function socket_write; use function trim; use const AF_INET; use const AF_UNIX; +use const SO_REUSEADDR; use const SOCK_STREAM; use const SOCKET_ENOPROTOOPT; use const SOCKET_EPROTONOSUPPORT; +use const SOL_SOCKET; use const SOL_TCP; class RCON{ @@ -74,6 +77,10 @@ class RCON{ $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + if(!socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)){ + throw new \RuntimeException("Unable to set option on socket: " . trim(socket_strerror(socket_last_error()))); + } + if($this->socket === false or !@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){ throw new \RuntimeException(trim(socket_strerror(socket_last_error()))); }