|
|
@ -32,7 +32,7 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private BaseSocket $Socket;
|
|
|
|
private BaseSocket $Socket;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var resource */
|
|
|
|
/** @var ?resource */
|
|
|
|
private $RconSocket;
|
|
|
|
private $RconSocket;
|
|
|
|
private int $RconRequestId = 0;
|
|
|
|
private int $RconRequestId = 0;
|
|
|
|
|
|
|
|
|
|
|
@ -45,7 +45,7 @@
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if( $this->RconSocket )
|
|
|
|
if( $this->RconSocket )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FClose( $this->RconSocket );
|
|
|
|
fclose( $this->RconSocket );
|
|
|
|
|
|
|
|
|
|
|
|
$this->RconSocket = null;
|
|
|
|
$this->RconSocket = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -57,7 +57,7 @@
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if( !$this->RconSocket )
|
|
|
|
if( !$this->RconSocket )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$RconSocket = @FSockOpen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
|
|
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
|
|
|
|
|
|
|
|
|
|
if( $ErrNo || !$RconSocket )
|
|
|
|
if( $ErrNo || !$RconSocket )
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -65,27 +65,27 @@
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->RconSocket = $RconSocket;
|
|
|
|
$this->RconSocket = $RconSocket;
|
|
|
|
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
|
|
|
|
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
|
|
|
|
Stream_Set_Blocking( $this->RconSocket, true );
|
|
|
|
stream_set_blocking( $this->RconSocket, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function Write( int $Header, string $String = '' ) : bool
|
|
|
|
public function Write( int $Header, string $String = '' ) : bool
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Pack the packet together
|
|
|
|
// Pack the packet together
|
|
|
|
$Command = Pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
|
|
|
$Command = pack( 'VV', ++$this->RconRequestId, $Header ) . $String . "\x00\x00";
|
|
|
|
|
|
|
|
|
|
|
|
// Prepend packet length
|
|
|
|
// Prepend packet length
|
|
|
|
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
|
|
|
|
$Command = pack( 'V', strlen( $Command ) ) . $Command;
|
|
|
|
$Length = StrLen( $Command );
|
|
|
|
$Length = strlen( $Command );
|
|
|
|
|
|
|
|
|
|
|
|
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
|
|
|
return $Length === fwrite( $this->RconSocket, $Command, $Length );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function Read( ) : Buffer
|
|
|
|
public function Read( ) : Buffer
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$Buffer = new Buffer( );
|
|
|
|
$Buffer = new Buffer( );
|
|
|
|
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
|
|
|
$Buffer->Set( fread( $this->RconSocket, 4 ) );
|
|
|
|
|
|
|
|
|
|
|
|
if( $Buffer->Remaining( ) < 4 )
|
|
|
|
if( $Buffer->Remaining( ) < 4 )
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -94,23 +94,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
$PacketSize = $Buffer->GetLong( );
|
|
|
|
$PacketSize = $Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
|
|
$Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
|
|
|
|
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
|
|
|
|
|
|
|
|
|
|
|
|
$Data = $Buffer->Get( );
|
|
|
|
$Data = $Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
|
|
$Remaining = $PacketSize - StrLen( $Data );
|
|
|
|
$Remaining = $PacketSize - strlen( $Data );
|
|
|
|
|
|
|
|
|
|
|
|
while( $Remaining > 0 )
|
|
|
|
while( $Remaining > 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$Data2 = FRead( $this->RconSocket, $Remaining );
|
|
|
|
$Data2 = fread( $this->RconSocket, $Remaining );
|
|
|
|
|
|
|
|
|
|
|
|
$PacketSize = StrLen( $Data2 );
|
|
|
|
$PacketSize = strlen( $Data2 );
|
|
|
|
|
|
|
|
|
|
|
|
if( $PacketSize === 0 )
|
|
|
|
if( $PacketSize === 0 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
|
|
|
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$Data .= $Data2;
|
|
|
|
$Data .= $Data2;
|
|
|
@ -144,7 +142,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
// 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( $Data ) >= 4000 )
|
|
|
|
if( strlen( $Data ) >= 4000 )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
|
|
|
$this->Write( SourceQuery::SERVERDATA_REQUESTVALUE );
|
|
|
|
|
|
|
|
|
|
|
|