RCONInstance: replace hardcoded status values with constants

I thought I'd seen the worst of PM, but I haven't.........
This commit is contained in:
Dylan K. Taylor 2018-05-07 13:44:59 +01:00
parent b14dfa9f7e
commit 83008440c0

View File

@ -27,6 +27,10 @@ use pocketmine\Thread;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
class RCONInstance extends Thread{ class RCONInstance extends Thread{
private const STATUS_DISCONNECTED = -1;
private const STATUS_AUTHENTICATING = 0;
private const STATUS_CONNECTED = 1;
public $stop; public $stop;
public $cmd; public $cmd;
public $response; public $response;
@ -54,7 +58,7 @@ class RCONInstance extends Thread{
$this->maxClients = $maxClients; $this->maxClients = $maxClients;
for($n = 0; $n < $this->maxClients; ++$n){ for($n = 0; $n < $this->maxClients; ++$n){
$this->{"client" . $n} = null; $this->{"client" . $n} = null;
$this->{"status" . $n} = 0; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
$this->{"timeout" . $n} = 0; $this->{"timeout" . $n} = 0;
} }
@ -111,7 +115,7 @@ class RCONInstance extends Thread{
for($n = 0; $n < $this->maxClients; ++$n){ for($n = 0; $n < $this->maxClients; ++$n){
if($this->{"client" . $n} === null){ if($this->{"client" . $n} === null){
$this->{"client" . $n} = $client; $this->{"client" . $n} = $client;
$this->{"status" . $n} = 0; $this->{"status" . $n} = self::STATUS_AUTHENTICATING;
$this->{"timeout" . $n} = microtime(true) + 5; $this->{"timeout" . $n} = microtime(true) + 5;
$done = true; $done = true;
break; break;
@ -126,14 +130,14 @@ class RCONInstance extends Thread{
for($n = 0; $n < $this->maxClients; ++$n){ for($n = 0; $n < $this->maxClients; ++$n){
$client = &$this->{"client" . $n}; $client = &$this->{"client" . $n};
if($client !== null){ if($client !== null){
if($this->{"status" . $n} !== -1 and !$this->stop){ if($this->{"status" . $n} !== self::STATUS_DISCONNECTED and !$this->stop){
if($this->{"status" . $n} === 0 and $this->{"timeout" . $n} < microtime(true)){ //Timeout if($this->{"status" . $n} === self::STATUS_AUTHENTICATING and $this->{"timeout" . $n} < microtime(true)){ //Timeout
$this->{"status" . $n} = -1; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
continue; continue;
} }
$p = $this->readPacket($client, $size, $requestID, $packetType, $payload); $p = $this->readPacket($client, $size, $requestID, $packetType, $payload);
if($p === false){ if($p === false){
$this->{"status" . $n} = -1; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
continue; continue;
}elseif($p === null){ }elseif($p === null){
continue; continue;
@ -141,8 +145,8 @@ class RCONInstance extends Thread{
switch($packetType){ switch($packetType){
case 3: //Login case 3: //Login
if($this->{"status" . $n} !== 0){ if($this->{"status" . $n} !== self::STATUS_AUTHENTICATING){
$this->{"status" . $n} = -1; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
continue; continue;
} }
if($payload === $this->password){ if($payload === $this->password){
@ -155,16 +159,16 @@ class RCONInstance extends Thread{
$this->waiting = false; $this->waiting = false;
$this->response = ""; $this->response = "";
$this->writePacket($client, $requestID, 2, ""); $this->writePacket($client, $requestID, 2, "");
$this->{"status" . $n} = 1; $this->{"status" . $n} = self::STATUS_CONNECTED;
}else{ }else{
$this->{"status" . $n} = -1; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
$this->writePacket($client, -1, 2, ""); $this->writePacket($client, -1, 2, "");
continue; continue;
} }
break; break;
case 2: //Command case 2: //Command
if($this->{"status" . $n} !== 1){ if($this->{"status" . $n} !== self::STATUS_CONNECTED){
$this->{"status" . $n} = -1; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
continue; continue;
} }
if(strlen($payload) > 0){ if(strlen($payload) > 0){
@ -187,7 +191,7 @@ class RCONInstance extends Thread{
@socket_set_block($client); @socket_set_block($client);
@socket_read($client, 1); @socket_read($client, 1);
@socket_close($client); @socket_close($client);
$this->{"status" . $n} = 0; $this->{"status" . $n} = self::STATUS_DISCONNECTED;
$this->{"client" . $n} = null; $this->{"client" . $n} = null;
} }
} }