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