Fixed RCON non-blocking sockets

This commit is contained in:
Shoghi Cervantes Pueyo 2013-05-15 17:30:06 +02:00
parent 33bd66c1da
commit de5be4168d

View File

@ -47,8 +47,8 @@ class RCON{
if($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)){ 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())); console("[ERROR] RCON can't be started: ".socket_strerror(socket_last_error()));
return; return;
} }
@socket_set_nonblock($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);
} }
@ -139,21 +139,26 @@ class RCONInstance extends Thread{
public function run(){ public function run(){
while($this->stop !== true){ while($this->stop !== true){
usleep(1); usleep(1);
if(($client = socket_accept($this->socket)) !== false){ $r = array($socket = $this->socket);
socket_set_block($client); $w = null;
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1); $e = null;
$done = false; if(socket_select($r, $w, $e, 0) === 1){
for($n = 0; $n < $this->maxClients; ++$n){ if(($client = socket_accept($this->socket)) !== false){
if($this->{"client".$n} === null){ socket_set_block($client);
$this->{"client".$n} = $client; socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
$this->{"status".$n} = 0; $done = false;
$this->{"timeout".$n} = microtime(true) + 5; for($n = 0; $n < $this->maxClients; ++$n){
$done = true; if($this->{"client".$n} === null){
break; $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);
} }
} }