mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-05-18 12:53:34 +02:00
Add more type info
This commit is contained in:
@@ -25,24 +25,25 @@
|
||||
*/
|
||||
abstract class BaseSocket
|
||||
{
|
||||
/** @var resource */
|
||||
public $Socket;
|
||||
public $Engine;
|
||||
public int $Engine;
|
||||
|
||||
public $Address;
|
||||
public $Port;
|
||||
public $Timeout;
|
||||
public string $Address;
|
||||
public int $Port;
|
||||
public int $Timeout;
|
||||
|
||||
public function __destruct( )
|
||||
{
|
||||
$this->Close( );
|
||||
}
|
||||
|
||||
abstract public function Close( );
|
||||
abstract public function Open( $Address, $Port, $Timeout, $Engine );
|
||||
abstract public function Write( $Header, $String = '' );
|
||||
abstract public function Read( $Length = 1400 );
|
||||
abstract public function Close( ) : void;
|
||||
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
|
||||
abstract public function Write( int $Header, string $String = '' ) : bool;
|
||||
abstract public function Read( int $Length = 1400 ) : Buffer;
|
||||
|
||||
protected function ReadInternal( $Buffer, $Length, $SherlockFunction )
|
||||
protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
|
||||
{
|
||||
if( $Buffer->Remaining( ) === 0 )
|
||||
{
|
||||
|
||||
+16
-38
@@ -25,31 +25,23 @@
|
||||
{
|
||||
/**
|
||||
* Buffer
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Buffer;
|
||||
private string $Buffer = '';
|
||||
|
||||
/**
|
||||
* Buffer length
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $Length;
|
||||
private int $Length = 0;
|
||||
|
||||
/**
|
||||
* Current position in buffer
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $Position;
|
||||
private int $Position = 0;
|
||||
|
||||
/**
|
||||
* Sets buffer
|
||||
*
|
||||
* @param string $Buffer Buffer
|
||||
*/
|
||||
public function Set( $Buffer )
|
||||
public function Set( string $Buffer ) : void
|
||||
{
|
||||
$this->Buffer = $Buffer;
|
||||
$this->Length = StrLen( $Buffer );
|
||||
@@ -61,7 +53,7 @@
|
||||
*
|
||||
* @return int Remaining bytes in buffer
|
||||
*/
|
||||
public function Remaining( )
|
||||
public function Remaining( ) : int
|
||||
{
|
||||
return $this->Length - $this->Position;
|
||||
}
|
||||
@@ -70,10 +62,8 @@
|
||||
* Gets data from buffer
|
||||
*
|
||||
* @param int $Length Bytes to read
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get( $Length = -1 )
|
||||
public function Get( int $Length = -1 ) : string
|
||||
{
|
||||
if( $Length === 0 )
|
||||
{
|
||||
@@ -100,20 +90,16 @@
|
||||
|
||||
/**
|
||||
* Get byte from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetByte( )
|
||||
public function GetByte( ) : int
|
||||
{
|
||||
return Ord( $this->Get( 1 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetShort( )
|
||||
public function GetShort( ) : int
|
||||
{
|
||||
if( $this->Remaining( ) < 2 )
|
||||
{
|
||||
@@ -122,15 +108,13 @@
|
||||
|
||||
$Data = UnPack( 'v', $this->Get( 2 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get long from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetLong( )
|
||||
public function GetLong( ) : int
|
||||
{
|
||||
if( $this->Remaining( ) < 4 )
|
||||
{
|
||||
@@ -139,15 +123,13 @@
|
||||
|
||||
$Data = UnPack( 'l', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get float from buffer
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function GetFloat( )
|
||||
public function GetFloat( ) : float
|
||||
{
|
||||
if( $this->Remaining( ) < 4 )
|
||||
{
|
||||
@@ -156,15 +138,13 @@
|
||||
|
||||
$Data = UnPack( 'f', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (float)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unsigned long from buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetUnsignedLong( )
|
||||
public function GetUnsignedLong( ) : int
|
||||
{
|
||||
if( $this->Remaining( ) < 4 )
|
||||
{
|
||||
@@ -173,15 +153,13 @@
|
||||
|
||||
$Data = UnPack( 'V', $this->Get( 4 ) );
|
||||
|
||||
return $Data[ 1 ];
|
||||
return (int)$Data[ 1 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one string from buffer ending with null byte
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function GetString( )
|
||||
public function GetString( ) : string
|
||||
{
|
||||
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
|
||||
|
||||
|
||||
@@ -28,30 +28,30 @@
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var Socket
|
||||
* @var BaseSocket
|
||||
*/
|
||||
private $Socket;
|
||||
|
||||
private $RconPassword;
|
||||
private $RconChallenge;
|
||||
private string $RconPassword = '';
|
||||
private string $RconChallenge = '';
|
||||
|
||||
public function __construct( $Socket )
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
$this->RconChallenge = 0;
|
||||
$this->RconPassword = 0;
|
||||
$this->RconChallenge = '';
|
||||
$this->RconPassword = '';
|
||||
}
|
||||
|
||||
public function Open( )
|
||||
public function Open( ) : void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
||||
$Length = StrLen( $Command );
|
||||
@@ -64,7 +64,7 @@
|
||||
* @throws AuthenticationException
|
||||
* @return Buffer
|
||||
*/
|
||||
public function Read( $Length = 1400 )
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
// GoldSource RCON has same structure as Query
|
||||
$Buffer = $this->Socket->Read( );
|
||||
@@ -115,7 +115,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( $Command )
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
if( !$this->RconChallenge )
|
||||
{
|
||||
@@ -128,7 +128,7 @@
|
||||
return $Buffer->Get( );
|
||||
}
|
||||
|
||||
public function Authorize( $Password )
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->RconPassword = $Password;
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@
|
||||
*/
|
||||
class Socket extends BaseSocket
|
||||
{
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->Socket )
|
||||
if( $this->Socket !== null )
|
||||
{
|
||||
FClose( $this->Socket );
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
public function Open( $Address, $Port, $Timeout, $Engine )
|
||||
public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void
|
||||
{
|
||||
$this->Timeout = $Timeout;
|
||||
$this->Engine = $Engine;
|
||||
@@ -53,7 +53,7 @@
|
||||
Stream_Set_Blocking( $this->Socket, true );
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
|
||||
$Length = StrLen( $Command );
|
||||
@@ -68,7 +68,7 @@
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*/
|
||||
public function Read( $Length = 1400 )
|
||||
public function Read( int $Length = 1400 ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( FRead( $this->Socket, $Length ) );
|
||||
@@ -78,7 +78,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Sherlock( $Buffer, $Length )
|
||||
public function Sherlock( Buffer $Buffer, int $Length ) : bool
|
||||
{
|
||||
$Data = FRead( $this->Socket, $Length );
|
||||
|
||||
|
||||
+18
-33
@@ -76,31 +76,23 @@
|
||||
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var BaseSocket
|
||||
*/
|
||||
private $Socket;
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/**
|
||||
* True if connection is open, false if not
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $Connected;
|
||||
private bool $Connected = false;
|
||||
|
||||
/**
|
||||
* Contains challenge
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $Challenge;
|
||||
private string $Challenge = '';
|
||||
|
||||
/**
|
||||
* Use old method for getting challenge number
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $UseOldGetChallengeMethod;
|
||||
private bool $UseOldGetChallengeMethod = false;
|
||||
|
||||
public function __construct( BaseSocket $Socket = null )
|
||||
{
|
||||
@@ -123,16 +115,16 @@
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function Connect( $Address, $Port, $Timeout = 3, $Engine = self::SOURCE )
|
||||
public function Connect( string $Address, int $Port, int $Timeout = 3, int $Engine = self::SOURCE ) : void
|
||||
{
|
||||
$this->Disconnect( );
|
||||
|
||||
if( !is_int( $Timeout ) || $Timeout < 0 )
|
||||
if( $Timeout < 0 )
|
||||
{
|
||||
throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
|
||||
throw new InvalidArgumentException( 'Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
|
||||
}
|
||||
|
||||
$this->Socket->Open( $Address, (int)$Port, $Timeout, (int)$Engine );
|
||||
$this->Socket->Open( $Address, $Port, $Timeout, $Engine );
|
||||
|
||||
$this->Connected = true;
|
||||
}
|
||||
@@ -144,7 +136,7 @@
|
||||
*
|
||||
* @returns bool Previous value
|
||||
*/
|
||||
public function SetUseOldGetChallengeMethod( $Value )
|
||||
public function SetUseOldGetChallengeMethod( bool $Value ) : bool
|
||||
{
|
||||
$Previous = $this->UseOldGetChallengeMethod;
|
||||
|
||||
@@ -156,7 +148,7 @@
|
||||
/**
|
||||
* Closes all open connections
|
||||
*/
|
||||
public function Disconnect( )
|
||||
public function Disconnect( ) : void
|
||||
{
|
||||
$this->Connected = false;
|
||||
$this->Challenge = '';
|
||||
@@ -180,7 +172,7 @@
|
||||
*
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function Ping( )
|
||||
public function Ping( ) : bool
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -201,7 +193,7 @@
|
||||
*
|
||||
* @return array Returns an array with information on success
|
||||
*/
|
||||
public function GetInfo( )
|
||||
public function GetInfo( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -246,16 +238,12 @@
|
||||
$Mod[ 'Size' ] = $Buffer->GetLong( );
|
||||
$Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
|
||||
$Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'Mod' ] = $Mod;
|
||||
}
|
||||
|
||||
$Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
|
||||
$Server[ 'Bots' ] = $Buffer->GetByte( );
|
||||
|
||||
if( isset( $Mod ) )
|
||||
{
|
||||
$Server[ 'Mod' ] = $Mod;
|
||||
}
|
||||
|
||||
return $Server;
|
||||
}
|
||||
|
||||
@@ -368,7 +356,7 @@
|
||||
*
|
||||
* @return array Returns an array with players on success
|
||||
*/
|
||||
public function GetPlayers( )
|
||||
public function GetPlayers( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -414,7 +402,7 @@
|
||||
*
|
||||
* @return array Returns an array with rules on success
|
||||
*/
|
||||
public function GetRules( )
|
||||
public function GetRules( ) : array
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -453,12 +441,9 @@
|
||||
/**
|
||||
* Get challenge (used for players/rules packets)
|
||||
*
|
||||
* @param $Header
|
||||
* @param $ExpectedResult
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
private function GetChallenge( $Header, $ExpectedResult )
|
||||
private function GetChallenge( int $Header, int $ExpectedResult ) : void
|
||||
{
|
||||
if( $this->Challenge )
|
||||
{
|
||||
@@ -509,7 +494,7 @@
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function SetRconPassword( $Password )
|
||||
public function SetRconPassword( string $Password ) : void
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
@@ -551,7 +536,7 @@
|
||||
*
|
||||
* @return string Answer from server in string
|
||||
*/
|
||||
public function Rcon( $Command )
|
||||
public function Rcon( string $Command ) : string
|
||||
{
|
||||
if( !$this->Connected )
|
||||
{
|
||||
|
||||
+10
-11
@@ -29,20 +29,19 @@
|
||||
{
|
||||
/**
|
||||
* Points to socket class
|
||||
*
|
||||
* @var Socket
|
||||
*/
|
||||
private $Socket;
|
||||
private BaseSocket $Socket;
|
||||
|
||||
/** @var resource */
|
||||
private $RconSocket;
|
||||
private $RconRequestId;
|
||||
private int $RconRequestId = 0;
|
||||
|
||||
public function __construct( $Socket )
|
||||
public function __construct( BaseSocket $Socket )
|
||||
{
|
||||
$this->Socket = $Socket;
|
||||
}
|
||||
|
||||
public function Close( )
|
||||
public function Close( ) : void
|
||||
{
|
||||
if( $this->RconSocket )
|
||||
{
|
||||
@@ -54,7 +53,7 @@
|
||||
$this->RconRequestId = 0;
|
||||
}
|
||||
|
||||
public function Open( )
|
||||
public function Open( ) : void
|
||||
{
|
||||
if( !$this->RconSocket )
|
||||
{
|
||||
@@ -70,7 +69,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
public function Write( $Header, $String = '' )
|
||||
public function Write( int $Header, string $String = '' ) : bool
|
||||
{
|
||||
// Pack the packet together
|
||||
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
||||
@@ -82,7 +81,7 @@
|
||||
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
||||
}
|
||||
|
||||
public function Read( )
|
||||
public function Read( ) : Buffer
|
||||
{
|
||||
$Buffer = new Buffer( );
|
||||
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
||||
@@ -122,7 +121,7 @@
|
||||
return $Buffer;
|
||||
}
|
||||
|
||||
public function Command( $Command )
|
||||
public function Command( string $Command ) : string
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
|
||||
$Buffer = $this->Read( );
|
||||
@@ -174,7 +173,7 @@
|
||||
return rtrim( $Data, "\0" );
|
||||
}
|
||||
|
||||
public function Authorize( $Password )
|
||||
public function Authorize( string $Password ) : void
|
||||
{
|
||||
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
|
||||
$Buffer = $this->Read( );
|
||||
|
||||
Reference in New Issue
Block a user