|
|
|
@ -32,7 +32,7 @@
|
|
|
|
|
*/
|
|
|
|
|
private BaseSocket $Socket;
|
|
|
|
|
|
|
|
|
|
/** @var resource */
|
|
|
|
|
/** @var ?resource */
|
|
|
|
|
private $RconSocket;
|
|
|
|
|
private int $RconRequestId = 0;
|
|
|
|
|
|
|
|
|
@ -45,7 +45,7 @@
|
|
|
|
|
{
|
|
|
|
|
if( $this->RconSocket )
|
|
|
|
|
{
|
|
|
|
|
FClose( $this->RconSocket );
|
|
|
|
|
fclose( $this->RconSocket );
|
|
|
|
|
|
|
|
|
|
$this->RconSocket = null;
|
|
|
|
|
}
|
|
|
|
@ -57,34 +57,35 @@
|
|
|
|
|
{
|
|
|
|
|
if( !$this->RconSocket )
|
|
|
|
|
{
|
|
|
|
|
$this->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 || !$this->RconSocket )
|
|
|
|
|
if( $ErrNo || !$RconSocket )
|
|
|
|
|
{
|
|
|
|
|
throw new SocketException( 'Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
|
|
|
|
|
Stream_Set_Blocking( $this->RconSocket, true );
|
|
|
|
|
$this->RconSocket = $RconSocket;
|
|
|
|
|
stream_set_timeout( $this->RconSocket, $this->Socket->Timeout );
|
|
|
|
|
stream_set_blocking( $this->RconSocket, true );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Write( int $Header, string $String = '' ) : bool
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
|
|
|
|
|
$Length = StrLen( $Command );
|
|
|
|
|
$Command = pack( 'V', strlen( $Command ) ) . $Command;
|
|
|
|
|
$Length = strlen( $Command );
|
|
|
|
|
|
|
|
|
|
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
|
|
|
|
return $Length === fwrite( $this->RconSocket, $Command, $Length );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Read( ) : Buffer
|
|
|
|
|
{
|
|
|
|
|
$Buffer = new Buffer( );
|
|
|
|
|
$Buffer->Set( FRead( $this->RconSocket, 4 ) );
|
|
|
|
|
$Buffer->Set( fread( $this->RconSocket, 4 ) );
|
|
|
|
|
|
|
|
|
|
if( $Buffer->Remaining( ) < 4 )
|
|
|
|
|
{
|
|
|
|
@ -93,23 +94,21 @@
|
|
|
|
|
|
|
|
|
|
$PacketSize = $Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
$Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
|
|
|
|
|
$Buffer->Set( fread( $this->RconSocket, $PacketSize ) );
|
|
|
|
|
|
|
|
|
|
$Data = $Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
$Remaining = $PacketSize - StrLen( $Data );
|
|
|
|
|
$Remaining = $PacketSize - strlen( $Data );
|
|
|
|
|
|
|
|
|
|
while( $Remaining > 0 )
|
|
|
|
|
{
|
|
|
|
|
$Data2 = FRead( $this->RconSocket, $Remaining );
|
|
|
|
|
$Data2 = fread( $this->RconSocket, $Remaining );
|
|
|
|
|
|
|
|
|
|
$PacketSize = StrLen( $Data2 );
|
|
|
|
|
$PacketSize = strlen( $Data2 );
|
|
|
|
|
|
|
|
|
|
if( $PacketSize === 0 )
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidPacketException( 'Read ' . strlen( $Data ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Data .= $Data2;
|
|
|
|
@ -143,7 +142,7 @@
|
|
|
|
|
|
|
|
|
|
// We do this stupid hack to handle split packets
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
|
|