From de5be4168d3c30900354ed6ecc53187684268b04 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 15 May 2013 17:30:06 +0200 Subject: [PATCH] Fixed RCON non-blocking sockets --- src/network/RCON.php | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/network/RCON.php b/src/network/RCON.php index b2313b0b8..08310c58a 100644 --- a/src/network/RCON.php +++ b/src/network/RCON.php @@ -47,8 +47,8 @@ class RCON{ if($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)){ console("[ERROR] RCON can't be started: ".socket_strerror(socket_last_error())); return; - } - @socket_set_nonblock($this->socket); + } + @socket_set_block($this->socket); for($n = 0; $n < $this->threads; ++$n){ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); } @@ -139,21 +139,26 @@ class RCONInstance extends Thread{ public function run(){ while($this->stop !== true){ usleep(1); - if(($client = socket_accept($this->socket)) !== false){ - socket_set_block($client); - socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); - $done = false; - for($n = 0; $n < $this->maxClients; ++$n){ - if($this->{"client".$n} === null){ - $this->{"client".$n} = $client; - $this->{"status".$n} = 0; - $this->{"timeout".$n} = microtime(true) + 5; - $done = true; - break; + $r = array($socket = $this->socket); + $w = null; + $e = null; + if(socket_select($r, $w, $e, 0) === 1){ + if(($client = socket_accept($this->socket)) !== false){ + socket_set_block($client); + socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); + $done = false; + for($n = 0; $n < $this->maxClients; ++$n){ + if($this->{"client".$n} === null){ + $this->{"client".$n} = $client; + $this->{"status".$n} = 0; + $this->{"timeout".$n} = microtime(true) + 5; + $done = true; + break; + } + } + if($done === false){ + @socket_close($client); } - } - if($done === false){ - @socket_close($client); } }