Get rid of global buffer

Closes #75
pull/94/head
Pavel Djundik 9 years ago
parent caf5296b5b
commit d42aae5046

@ -23,13 +23,6 @@
*/ */
class GoldSourceRcon class GoldSourceRcon
{ {
/**
* Points to buffer class
*
* @var Buffer
*/
private $Buffer;
/** /**
* Points to socket class * Points to socket class
* *
@ -41,9 +34,8 @@
private $RconRequestId; private $RconRequestId;
private $RconChallenge; private $RconChallenge;
public function __construct( $Buffer, $Socket ) public function __construct( $Socket )
{ {
$this->Buffer = $Buffer;
$this->Socket = $Socket; $this->Socket = $Socket;
} }
@ -75,14 +67,14 @@
public function Read( $Length = 1400 ) public function Read( $Length = 1400 )
{ {
// GoldSource RCON has same structure as Query // GoldSource RCON has same structure as Query
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
if( $this->Buffer->GetByte( ) !== SourceQuery::S2A_RCON ) if( $Buffer->GetByte( ) !== SourceQuery::S2A_RCON )
{ {
return false; return false;
} }
$Buffer = $this->Buffer->Get( ); $Buffer = $Buffer->Get( );
$Trimmed = Trim( $Buffer ); $Trimmed = Trim( $Buffer );
if( $Trimmed === 'Bad rcon_password.' ) if( $Trimmed === 'Bad rcon_password.' )
@ -102,11 +94,11 @@
{ {
$this->Socket->Read( ); $this->Socket->Read( );
$ReadMore = $this->Buffer->Remaining( ) > 0 && $this->Buffer->GetByte( ) === SourceQuery::S2A_RCON; $ReadMore = $Buffer->Remaining( ) > 0 && $Buffer->GetByte( ) === SourceQuery::S2A_RCON;
if( $ReadMore ) if( $ReadMore )
{ {
$Packet = $this->Buffer->Get( ); $Packet = $Buffer->Get( );
$Buffer .= SubStr( $Packet, 0, -2 ); $Buffer .= SubStr( $Packet, 0, -2 );
// Let's assume if this packet is not long enough, there are no more after this one // Let's assume if this packet is not long enough, there are no more after this one
@ -115,7 +107,7 @@
} }
while( $ReadMore ); while( $ReadMore );
$this->Buffer->Set( Trim( $Buffer ) ); $Buffer->Set( Trim( $Buffer ) );
} }
public function Command( $Command ) public function Command( $Command )
@ -126,9 +118,9 @@
} }
$this->Write( 0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0" ); $this->Write( 0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0" );
$this->Read( ); $Buffer = $this->Read( );
return $this->Buffer->Get( ); return $Buffer->Get( );
} }
public function Authorize( $Password ) public function Authorize( $Password )
@ -136,14 +128,14 @@
$this->RconPassword = $Password; $this->RconPassword = $Password;
$this->Write( 0, 'challenge rcon' ); $this->Write( 0, 'challenge rcon' );
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
if( $this->Buffer->Get( 14 ) !== 'challenge rcon' ) if( $Buffer->Get( 14 ) !== 'challenge rcon' )
{ {
return false; return false;
} }
$this->RconChallenge = Trim( $this->Buffer->Get( ) ); $this->RconChallenge = Trim( $Buffer->Get( ) );
return true; return true;
} }

@ -32,18 +32,6 @@
public $Port; public $Port;
public $Timeout; public $Timeout;
/**
* Points to buffer class
*
* @var Buffer
*/
private $Buffer;
public function __construct( $Buffer )
{
$this->Buffer = $Buffer;
}
public function Close( ) public function Close( )
{ {
if( $this->Socket ) if( $this->Socket )
@ -80,22 +68,25 @@
return $Length === FWrite( $this->Socket, $Command, $Length ); return $Length === FWrite( $this->Socket, $Command, $Length );
} }
/**
* Reads from socket and returns Buffer.
*
* @throws InvalidPacketException
*
* @return Buffer Buffer
*/
public function Read( $Length = 1400 ) public function Read( $Length = 1400 )
{ {
$this->ReadBuffer( FRead( $this->Socket, $Length ), $Length ); $Buffer = new Buffer( );
} $Buffer->Set( FRead( $this->Socket, $Length ) );
protected function ReadBuffer( $Buffer, $Length ) if( $Buffer->Remaining( ) === 0 )
{
$this->Buffer->Set( $Buffer );
if( $this->Buffer->Remaining( ) === 0 )
{ {
// TODO: Should we throw an exception here? // TODO: Should we throw an exception here?
return; return;
} }
$Header = $this->Buffer->GetLong( ); $Header = $Buffer->GetLong( );
if( $Header === -1 ) // Single packet if( $Header === -1 ) // Single packet
{ {
@ -103,19 +94,19 @@
} }
else if( $Header === -2 ) // Split packet else if( $Header === -2 ) // Split packet
{ {
$Packets = Array( ); $Packets = [];
$IsCompressed = false; $IsCompressed = false;
$ReadMore = false; $ReadMore = false;
do do
{ {
$RequestID = $this->Buffer->GetLong( ); $RequestID = $Buffer->GetLong( );
switch( $this->Engine ) switch( $this->Engine )
{ {
case SourceQuery::GOLDSOURCE: case SourceQuery::GOLDSOURCE:
{ {
$PacketCountAndNumber = $this->Buffer->GetByte( ); $PacketCountAndNumber = $Buffer->GetByte( );
$PacketCount = $PacketCountAndNumber & 0xF; $PacketCount = $PacketCountAndNumber & 0xF;
$PacketNumber = $PacketCountAndNumber >> 4; $PacketNumber = $PacketCountAndNumber >> 4;
@ -124,31 +115,31 @@
case SourceQuery::SOURCE: case SourceQuery::SOURCE:
{ {
$IsCompressed = ( $RequestID & 0x80000000 ) !== 0; $IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
$PacketCount = $this->Buffer->GetByte( ); $PacketCount = $Buffer->GetByte( );
$PacketNumber = $this->Buffer->GetByte( ) + 1; $PacketNumber = $Buffer->GetByte( ) + 1;
if( $IsCompressed ) if( $IsCompressed )
{ {
$this->Buffer->GetLong( ); // Split size $Buffer->GetLong( ); // Split size
$PacketChecksum = $this->Buffer->GetUnsignedLong( ); $PacketChecksum = $Buffer->GetUnsignedLong( );
} }
else else
{ {
$this->Buffer->GetShort( ); // Split size $Buffer->GetShort( ); // Split size
} }
break; break;
} }
} }
$Packets[ $PacketNumber ] = $this->Buffer->Get( ); $Packets[ $PacketNumber ] = $Buffer->Get( );
$ReadMore = $PacketCount > sizeof( $Packets ); $ReadMore = $PacketCount > sizeof( $Packets );
} }
while( $ReadMore && $this->Sherlock( $Length ) ); while( $ReadMore && $this->Sherlock( $Buffer, $Length ) );
$Buffer = Implode( $Packets ); $Data = Implode( $Packets );
// TODO: Test this // TODO: Test this
if( $IsCompressed ) if( $IsCompressed )
@ -159,23 +150,25 @@
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
} }
$Buffer = bzdecompress( $Buffer ); $Data = bzdecompress( $Data );
if( CRC32( $Buffer ) !== $PacketChecksum ) if( CRC32( $Data ) !== $PacketChecksum )
{ {
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
} }
} }
$this->Buffer->Set( SubStr( $Buffer, 4 ) ); $Buffer->Set( SubStr( $Data, 4 ) );
} }
else else
{ {
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
return $Buffer;
} }
private function Sherlock( $Length ) private function Sherlock( $Buffer, $Length )
{ {
$Data = FRead( $this->Socket, $Length ); $Data = FRead( $this->Socket, $Length );
@ -184,8 +177,8 @@
return false; return false;
} }
$this->Buffer->Set( $Data ); $Buffer->Set( $Data );
return $this->Buffer->GetLong( ) === -2; return $Buffer->GetLong( ) === -2;
} }
} }

@ -81,13 +81,6 @@
*/ */
private $Rcon; private $Rcon;
/**
* Points to buffer class
*
* @var Buffer
*/
private $Buffer;
/** /**
* Points to socket class * Points to socket class
* *
@ -118,8 +111,7 @@
public function __construct( ) public function __construct( )
{ {
$this->Buffer = new Buffer( ); $this->Socket = new Socket( );
$this->Socket = new Socket( $this->Buffer );
} }
public function __destruct( ) public function __destruct( )
@ -176,8 +168,6 @@
$this->Connected = false; $this->Connected = false;
$this->Challenge = 0; $this->Challenge = 0;
$this->Buffer->Reset( );
$this->Socket->Close( ); $this->Socket->Close( );
if( $this->Rcon ) if( $this->Rcon )
@ -207,9 +197,9 @@
} }
$this->Socket->Write( self::A2S_PING ); $this->Socket->Write( self::A2S_PING );
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
return $this->Buffer->GetByte( ) === self::S2A_PING; return $Buffer->GetByte( ) === self::S2A_PING;
} }
/** /**
@ -230,9 +220,9 @@
} }
$this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" ); $this->Socket->Write( self::A2S_INFO, "Source Engine Query\0" );
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
$Type = $this->Buffer->GetByte( ); $Type = $Buffer->GetByte( );
if( $Type === 0 ) if( $Type === 0 )
{ {
@ -248,32 +238,32 @@
* Because it sends answer for both protocols * Because it sends answer for both protocols
*/ */
$Server[ 'Address' ] = $this->Buffer->GetString( ); $Server[ 'Address' ] = $Buffer->GetString( );
$Server[ 'HostName' ] = $this->Buffer->GetString( ); $Server[ 'HostName' ] = $Buffer->GetString( );
$Server[ 'Map' ] = $this->Buffer->GetString( ); $Server[ 'Map' ] = $Buffer->GetString( );
$Server[ 'ModDir' ] = $this->Buffer->GetString( ); $Server[ 'ModDir' ] = $Buffer->GetString( );
$Server[ 'ModDesc' ] = $this->Buffer->GetString( ); $Server[ 'ModDesc' ] = $Buffer->GetString( );
$Server[ 'Players' ] = $this->Buffer->GetByte( ); $Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); $Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Protocol' ] = $this->Buffer->GetByte( ); $Server[ 'Protocol' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); $Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); $Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; $Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'IsMod' ] = $this->Buffer->GetByte( ) === 1; $Server[ 'IsMod' ] = $Buffer->GetByte( ) === 1;
if( $Server[ 'IsMod' ] ) if( $Server[ 'IsMod' ] )
{ {
$Mod[ 'Url' ] = $this->Buffer->GetString( ); $Mod[ 'Url' ] = $Buffer->GetString( );
$Mod[ 'Download' ] = $this->Buffer->GetString( ); $Mod[ 'Download' ] = $Buffer->GetString( );
$this->Buffer->Get( 1 ); // NULL byte $Buffer->Get( 1 ); // NULL byte
$Mod[ 'Version' ] = $this->Buffer->GetLong( ); $Mod[ 'Version' ] = $Buffer->GetLong( );
$Mod[ 'Size' ] = $this->Buffer->GetLong( ); $Mod[ 'Size' ] = $Buffer->GetLong( );
$Mod[ 'ServerSide' ] = $this->Buffer->GetByte( ) === 1; $Mod[ 'ServerSide' ] = $Buffer->GetByte( ) === 1;
$Mod[ 'CustomDLL' ] = $this->Buffer->GetByte( ) === 1; $Mod[ 'CustomDLL' ] = $Buffer->GetByte( ) === 1;
} }
$Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; $Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
$Server[ 'Bots' ] = $this->Buffer->GetByte( ); $Server[ 'Bots' ] = $Buffer->GetByte( );
if( isset( $Mod ) ) if( isset( $Mod ) )
{ {
@ -288,69 +278,69 @@
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
$Server[ 'Protocol' ] = $this->Buffer->GetByte( ); $Server[ 'Protocol' ] = $Buffer->GetByte( );
$Server[ 'HostName' ] = $this->Buffer->GetString( ); $Server[ 'HostName' ] = $Buffer->GetString( );
$Server[ 'Map' ] = $this->Buffer->GetString( ); $Server[ 'Map' ] = $Buffer->GetString( );
$Server[ 'ModDir' ] = $this->Buffer->GetString( ); $Server[ 'ModDir' ] = $Buffer->GetString( );
$Server[ 'ModDesc' ] = $this->Buffer->GetString( ); $Server[ 'ModDesc' ] = $Buffer->GetString( );
$Server[ 'AppID' ] = $this->Buffer->GetShort( ); $Server[ 'AppID' ] = $Buffer->GetShort( );
$Server[ 'Players' ] = $this->Buffer->GetByte( ); $Server[ 'Players' ] = $Buffer->GetByte( );
$Server[ 'MaxPlayers' ] = $this->Buffer->GetByte( ); $Server[ 'MaxPlayers' ] = $Buffer->GetByte( );
$Server[ 'Bots' ] = $this->Buffer->GetByte( ); $Server[ 'Bots' ] = $Buffer->GetByte( );
$Server[ 'Dedicated' ] = Chr( $this->Buffer->GetByte( ) ); $Server[ 'Dedicated' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Os' ] = Chr( $this->Buffer->GetByte( ) ); $Server[ 'Os' ] = Chr( $Buffer->GetByte( ) );
$Server[ 'Password' ] = $this->Buffer->GetByte( ) === 1; $Server[ 'Password' ] = $Buffer->GetByte( ) === 1;
$Server[ 'Secure' ] = $this->Buffer->GetByte( ) === 1; $Server[ 'Secure' ] = $Buffer->GetByte( ) === 1;
// The Ship (they violate query protocol spec by modifying the response) // The Ship (they violate query protocol spec by modifying the response)
if( $Server[ 'AppID' ] === 2400 ) if( $Server[ 'AppID' ] === 2400 )
{ {
$Server[ 'GameMode' ] = $this->Buffer->GetByte( ); $Server[ 'GameMode' ] = $Buffer->GetByte( );
$Server[ 'WitnessCount' ] = $this->Buffer->GetByte( ); $Server[ 'WitnessCount' ] = $Buffer->GetByte( );
$Server[ 'WitnessTime' ] = $this->Buffer->GetByte( ); $Server[ 'WitnessTime' ] = $Buffer->GetByte( );
} }
$Server[ 'Version' ] = $this->Buffer->GetString( ); $Server[ 'Version' ] = $Buffer->GetString( );
// Extra Data Flags // Extra Data Flags
if( $this->Buffer->Remaining( ) > 0 ) if( $Buffer->Remaining( ) > 0 )
{ {
$Server[ 'ExtraDataFlags' ] = $Flags = $this->Buffer->GetByte( ); $Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte( );
// The server's game port // The server's game port
if( $Flags & 0x80 ) if( $Flags & 0x80 )
{ {
$Server[ 'GamePort' ] = $this->Buffer->GetShort( ); $Server[ 'GamePort' ] = $Buffer->GetShort( );
} }
// The server's SteamID - does this serve any purpose? // The server's SteamID - does this serve any purpose?
if( $Flags & 0x10 ) if( $Flags & 0x10 )
{ {
$Server[ 'ServerID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); // TODO: verify this $Server[ 'ServerID' ] = $Buffer->GetUnsignedLong( ) | ( $Buffer->GetUnsignedLong( ) << 32 ); // TODO: verify this
} }
// The spectator port and then the spectator server name // The spectator port and then the spectator server name
if( $Flags & 0x40 ) if( $Flags & 0x40 )
{ {
$Server[ 'SpecPort' ] = $this->Buffer->GetShort( ); $Server[ 'SpecPort' ] = $Buffer->GetShort( );
$Server[ 'SpecName' ] = $this->Buffer->GetString( ); $Server[ 'SpecName' ] = $Buffer->GetString( );
} }
// The game tag data string for the server // The game tag data string for the server
if( $Flags & 0x20 ) if( $Flags & 0x20 )
{ {
$Server[ 'GameTags' ] = $this->Buffer->GetString( ); $Server[ 'GameTags' ] = $Buffer->GetString( );
} }
// GameID -- alternative to AppID? // GameID -- alternative to AppID?
if( $Flags & 0x01 ) if( $Flags & 0x01 )
{ {
$Server[ 'GameID' ] = $this->Buffer->GetUnsignedLong( ) | ( $this->Buffer->GetUnsignedLong( ) << 32 ); $Server[ 'GameID' ] = $Buffer->GetUnsignedLong( ) | ( $Buffer->GetUnsignedLong( ) << 32 );
} }
if( $this->Buffer->Remaining( ) > 0 ) if( $Buffer->Remaining( ) > 0 )
{ {
throw new InvalidPacketException( 'GetInfo: unread data? ' . $this->Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.', throw new InvalidPacketException( 'GetInfo: unread data? ' . $Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.',
InvalidPacketException::BUFFER_NOT_EMPTY ); InvalidPacketException::BUFFER_NOT_EMPTY );
} }
} }
@ -384,10 +374,10 @@
case self::GETCHALLENGE_ALL_CLEAR: case self::GETCHALLENGE_ALL_CLEAR:
{ {
$this->Socket->Write( self::A2S_PLAYER, $this->Challenge ); $this->Socket->Write( self::A2S_PLAYER, $this->Challenge );
$this->Socket->Read( 14000 ); // Moronic Arma 3 developers do not split their packets, so we have to read more data $Buffer = $this->Socket->Read( 14000 ); // Moronic Arma 3 developers do not split their packets, so we have to read more data
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol // This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
$Type = $this->Buffer->GetByte( ); $Type = $Buffer->GetByte( );
if( $Type === 0 ) if( $Type === 0 )
{ {
@ -402,15 +392,15 @@
} }
} }
$Players = Array( ); $Players = [];
$Count = $this->Buffer->GetByte( ); $Count = $Buffer->GetByte( );
while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
{ {
$Player[ 'Id' ] = $this->Buffer->GetByte( ); // PlayerID, is it just always 0? $Player[ 'Id' ] = $Buffer->GetByte( ); // PlayerID, is it just always 0?
$Player[ 'Name' ] = $this->Buffer->GetString( ); $Player[ 'Name' ] = $Buffer->GetString( );
$Player[ 'Frags' ] = $this->Buffer->GetLong( ); $Player[ 'Frags' ] = $Buffer->GetLong( );
$Player[ 'Time' ] = (int)$this->Buffer->GetFloat( ); $Player[ 'Time' ] = (int)$Buffer->GetFloat( );
$Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] ); $Player[ 'TimeF' ] = GMDate( ( $Player[ 'Time' ] > 3600 ? "H:i:s" : "i:s" ), $Player[ 'Time' ] );
$Players[ ] = $Player; $Players[ ] = $Player;
@ -445,9 +435,9 @@
case self::GETCHALLENGE_ALL_CLEAR: case self::GETCHALLENGE_ALL_CLEAR:
{ {
$this->Socket->Write( self::A2S_RULES, $this->Challenge ); $this->Socket->Write( self::A2S_RULES, $this->Challenge );
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
$Type = $this->Buffer->GetByte( ); $Type = $Buffer->GetByte( );
if( $Type === 0 ) if( $Type === 0 )
{ {
@ -462,13 +452,13 @@
} }
} }
$Rules = Array( ); $Rules = [];
$Count = $this->Buffer->GetShort( ); $Count = $Buffer->GetShort( );
while( $Count-- > 0 && $this->Buffer->Remaining( ) > 0 ) while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
{ {
$Rule = $this->Buffer->GetString( ); $Rule = $Buffer->GetString( );
$Value = $this->Buffer->GetString( ); $Value = $Buffer->GetString( );
if( !Empty( $Rule ) ) if( !Empty( $Rule ) )
{ {
@ -502,15 +492,15 @@
} }
$this->Socket->Write( $Header, 0xFFFFFFFF ); $this->Socket->Write( $Header, 0xFFFFFFFF );
$this->Socket->Read( ); $Buffer = $this->Socket->Read( );
$Type = $this->Buffer->GetByte( ); $Type = $Buffer->GetByte( );
switch( $Type ) switch( $Type )
{ {
case self::S2A_CHALLENGE: case self::S2A_CHALLENGE:
{ {
$this->Challenge = $this->Buffer->Get( 4 ); $this->Challenge = $Buffer->Get( 4 );
return self::GETCHALLENGE_ALL_CLEAR; return self::GETCHALLENGE_ALL_CLEAR;
} }
@ -555,13 +545,13 @@
{ {
case SourceQuery::GOLDSOURCE: case SourceQuery::GOLDSOURCE:
{ {
$this->Rcon = new GoldSourceRcon( $this->Buffer, $this->Socket ); $this->Rcon = new GoldSourceRcon( $this->Socket );
break; break;
} }
case SourceQuery::SOURCE: case SourceQuery::SOURCE:
{ {
$this->Rcon = new SourceRcon( $this->Buffer, $this->Socket ); $this->Rcon = new SourceRcon( $this->Socket );
break; break;
} }

@ -27,13 +27,6 @@
*/ */
class SourceRcon class SourceRcon
{ {
/**
* Points to buffer class
*
* @var Buffer
*/
private $Buffer;
/** /**
* Points to socket class * Points to socket class
* *
@ -44,9 +37,8 @@
private $RconSocket; private $RconSocket;
private $RconRequestId; private $RconRequestId;
public function __construct( $Buffer, $Socket ) public function __construct( $Socket )
{ {
$this->Buffer = $Buffer;
$this->Socket = $Socket; $this->Socket = $Socket;
} }
@ -92,50 +84,52 @@
public function Read( ) public function Read( )
{ {
$this->Buffer->Set( FRead( $this->RconSocket, 4 ) ); $Buffer = new Buffer( );
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
if( $this->Buffer->Remaining( ) < 4 ) if( $Buffer->Remaining( ) < 4 )
{ {
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
} }
$PacketSize = $this->Buffer->GetLong( ); $PacketSize = $Buffer->GetLong( );
$this->Buffer->Set( FRead( $this->RconSocket, $PacketSize ) ); $Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
$Buffer = $this->Buffer->Get( ); $Data = $Buffer->Get( );
$Remaining = $PacketSize - StrLen( $Buffer ); $Remaining = $PacketSize - StrLen( $Data );
while( $Remaining > 0 ) while( $Remaining > 0 )
{ {
$Buffer2 = FRead( $this->RconSocket, $Remaining ); $Data2 = FRead( $this->RconSocket, $Remaining );
$PacketSize = StrLen( $Buffer2 ); $PacketSize = StrLen( $Data2 );
if( $PacketSize === 0 ) if( $PacketSize === 0 )
{ {
throw new InvalidPacketException( 'Read ' . strlen( $Buffer ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY ); throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
break; break;
} }
$Buffer .= $Buffer2; $Data .= $Data2;
$Remaining -= $PacketSize; $Remaining -= $PacketSize;
} }
$this->Buffer->Set( $Buffer ); $Buffer->Set( $Data );
return $Buffer;
} }
public function Command( $Command ) public function Command( $Command )
{ {
$this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command ); $this->Write( SourceQuery::SERVERDATA_EXECCOMMAND, $Command );
$Buffer = $this->Read( );
$this->Read( ); $Buffer->GetLong( ); // RequestID
$this->Buffer->GetLong( ); // RequestID
$Type = $this->Buffer->GetLong( ); $Type = $Buffer->GetLong( );
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE ) if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
{ {
@ -146,57 +140,57 @@
return false; return false;
} }
$Buffer = $this->Buffer->Get( ); $Data = $Buffer->Get( );
// We do this stupid hack to handle split packets // We do this stupid hack to handle split packets
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses // See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if( StrLen( $Buffer ) >= 4000 ) if( StrLen( $Data ) >= 4000 )
{ {
do do
{ {
$this->Write( SourceQuery::SERVERDATA_RESPONSE_VALUE ); $this->Write( SourceQuery::SERVERDATA_RESPONSE_VALUE );
$this->Read( ); $Buffer = $this->Read( );
$this->Buffer->GetLong( ); // RequestID $Buffer->GetLong( ); // RequestID
if( $this->Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE ) if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
{ {
break; break;
} }
$Buffer2 = $this->Buffer->Get( ); $Data2 = $Buffer->Get( );
if( $Buffer2 === "\x00\x01\x00\x00\x00\x00" ) if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
{ {
break; break;
} }
$Buffer .= $Buffer2; $Data .= $Data2;
} }
while( true ); while( true );
} }
return rtrim( $Buffer, "\0" ); return rtrim( $Data, "\0" );
} }
public function Authorize( $Password ) public function Authorize( $Password )
{ {
$this->Write( SourceQuery::SERVERDATA_AUTH, $Password ); $this->Write( SourceQuery::SERVERDATA_AUTH, $Password );
$this->Read( ); $Buffer = $this->Read( );
$RequestID = $this->Buffer->GetLong( ); $RequestID = $Buffer->GetLong( );
$Type = $this->Buffer->GetLong( ); $Type = $Buffer->GetLong( );
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again // If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments // More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE ) if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE )
{ {
$this->Read( ); $Buffer = $this->Read( );
$RequestID = $this->Buffer->GetLong( ); $RequestID = $Buffer->GetLong( );
$Type = $this->Buffer->GetLong( ); $Type = $Buffer->GetLong( );
} }
if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE ) if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE )

Loading…
Cancel
Save