|
|
@ -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,35 +57,34 @@
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if( !$this->RconSocket )
|
|
|
|
if( !$this->RconSocket )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$RconSocket = @fsockopen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
|
|
$this->RconSocket = @FSockOpen( $this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
|
|
|
|
|
|
|
|
|
|
if( $ErrNo || !$RconSocket )
|
|
|
|
if( $ErrNo || !$this->RconSocket )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
|
|
|
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$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,21 +93,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
$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;
|
|
|
@ -142,7 +143,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 );
|
|
|
|
|
|
|
|
|
|
|
|