Minor cosmetic changes, throw an error when rcon read fails (#42)

pull/56/head
xPaw 10 years ago
parent 3bae84ca68
commit 50729990c6

@ -1,43 +1,43 @@
<?php <?php
/** /**
* Class written by xPaw * Class written by xPaw
* *
* Website: http://xpaw.me * Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class * GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/ */
namespace xPaw\SourceQuery\Exception; namespace xPaw\SourceQuery\Exception;
abstract class SourceQueryException extends \Exception abstract class SourceQueryException extends \Exception
{ {
// Base exception class
} }
class InvalidArgumentException extends SourceQueryException class InvalidArgumentException extends SourceQueryException
{ {
const TIMEOUT_NOT_INTEGER = 1; const TIMEOUT_NOT_INTEGER = 1;
} }
class TimeoutException extends SourceQueryException class TimeoutException extends SourceQueryException
{ {
const TIMEOUT_CONNECT = 1; const TIMEOUT_CONNECT = 1;
} }
class InvalidPacketException extends SourceQueryException class InvalidPacketException extends SourceQueryException
{ {
const PACKET_HEADER_MISMATCH = 1; const PACKET_HEADER_MISMATCH = 1;
const BUFFER_NOT_EMPTY = 2; const BUFFER_EMPTY = 2;
const BUFFER_NOT_EMPTY = 3;
const CHECKSUM_MISMATCH = 3; const CHECKSUM_MISMATCH = 4;
} }
class AuthenticationException extends SourceQueryException class AuthenticationException extends SourceQueryException
{ {
const BAD_PASSWORD = 1; const BAD_PASSWORD = 1;
const BANNED = 2; const BANNED = 2;
} }
class SocketException extends SourceQueryException class SocketException extends SourceQueryException
{ {
const COULD_NOT_CREATE_SOCKET = 1; const COULD_NOT_CREATE_SOCKET = 1;
} }

@ -71,13 +71,13 @@
$Buffer = $this->Buffer->Get( ); $Buffer = $this->Buffer->Get( );
$Trimmed = Trim( $Buffer ); $Trimmed = Trim( $Buffer );
if($Trimmed === 'Bad rcon_password.') if( $Trimmed === 'Bad rcon_password.' )
{ {
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD); throw new AuthenticationException( $Trimmed, AuthenticationException::BAD_PASSWORD );
} }
else if($Trimmed === 'You have been banned from this server.') else if( $Trimmed === 'You have been banned from this server.' )
{ {
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED); throw new AuthenticationException( $Trimmed, AuthenticationException::BANNED );
} }
$ReadMore = false; $ReadMore = false;

@ -1,14 +1,14 @@
<?php <?php
use xPaw\SourceQuery\Exception\InvalidPacketException; /**
use xPaw\SourceQuery\Exception\SocketException;
/**
* Class written by xPaw * Class written by xPaw
* *
* Website: http://xpaw.me * Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class * GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/ */
use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;
class SourceQuerySocket class SourceQuerySocket
{ {
public $Socket; public $Socket;
@ -51,7 +51,7 @@ use xPaw\SourceQuery\Exception\SocketException;
if( $ErrNo || $this->Socket === false ) if( $ErrNo || $this->Socket === false )
{ {
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET); throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
} }
Stream_Set_Timeout( $this->Socket, $Timeout ); Stream_Set_Timeout( $this->Socket, $Timeout );
@ -146,7 +146,7 @@ use xPaw\SourceQuery\Exception\SocketException;
if( CRC32( $Buffer ) !== $PacketChecksum ) if( CRC32( $Buffer ) !== $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 );
} }
} }
@ -154,7 +154,7 @@ use xPaw\SourceQuery\Exception\SocketException;
} }
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 );
} }
} }

@ -143,12 +143,12 @@
if( !is_int( $Timeout ) || $Timeout < 0 ) if( !is_int( $Timeout ) || $Timeout < 0 )
{ {
throw new InvalidArgumentException("Timeout must be an integer.", InvalidArgumentException::TIMEOUT_NOT_INTEGER); throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
} }
if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) ) if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) )
{ {
throw new TimeoutException("Could not connect to server.", TimeoutException::TIMEOUT_CONNECT); throw new TimeoutException( 'Could not connect to server.', TimeoutException::TIMEOUT_CONNECT );
} }
$this->Connected = true; $this->Connected = true;
@ -279,7 +279,7 @@
if( $Type !== self :: S2A_INFO ) if( $Type !== self :: S2A_INFO )
{ {
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' ] = $this->Buffer->GetByte( );
@ -344,8 +344,8 @@
if( $this->Buffer->Remaining( ) > 0 ) if( $this->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? ' . $this->Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.',
InvalidPacketException::BUFFER_NOT_EMPTY); InvalidPacketException::BUFFER_NOT_EMPTY );
} }
} }
@ -386,7 +386,7 @@
} }
else if( $Type !== self :: S2A_PLAYER ) else if( $Type !== self :: S2A_PLAYER )
{ {
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
break; break;
@ -443,7 +443,7 @@
} }
else if( $Type !== self :: S2A_RULES ) else if( $Type !== self :: S2A_RULES )
{ {
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
break; break;
@ -512,7 +512,7 @@
} }
default: default:
{ {
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
} }
} }
} }

@ -1,14 +1,15 @@
<?php <?php
use xPaw\SourceQuery\Exception\AuthenticationException; /**
use xPaw\SourceQuery\Exception\TimeoutException;
/**
* Class written by xPaw * Class written by xPaw
* *
* Website: http://xpaw.me * Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class * GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/ */
use xPaw\SourceQuery\Exception\AuthenticationException;
use xPaw\SourceQuery\Exception\TimeoutException;
use xPaw\SourceQuery\Exception\InvalidPacketException;
class SourceQuerySourceRcon class SourceQuerySourceRcon
{ {
/** /**
@ -54,7 +55,7 @@ use xPaw\SourceQuery\Exception\TimeoutException;
if( $ErrNo || !$this->RconSocket ) if( $ErrNo || !$this->RconSocket )
{ {
throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT); throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT );
} }
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout ); Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
@ -78,6 +79,11 @@ use xPaw\SourceQuery\Exception\TimeoutException;
{ {
$this->Buffer->Set( FRead( $this->RconSocket, $Length ) ); $this->Buffer->Set( FRead( $this->RconSocket, $Length ) );
if( $this->Buffer->Remaining( ) < 4 )
{
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
}
$PacketSize = $this->Buffer->GetLong( ); $PacketSize = $this->Buffer->GetLong( );
$Buffer = $this->Buffer->Get( ); $Buffer = $this->Buffer->Get( );
@ -102,11 +108,12 @@ use xPaw\SourceQuery\Exception\TimeoutException;
$this->Read( ); $this->Read( );
$this->Buffer->GetLong( ); // RequestID $this->Buffer->GetLong( ); // RequestID
$Type = $this->Buffer->GetLong( );
$Type = $this->Buffer->GetLong( );
if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE ) if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE )
{ {
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD); throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
} }
else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE ) else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
{ {
@ -163,7 +170,7 @@ use xPaw\SourceQuery\Exception\TimeoutException;
if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE ) if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE )
{ {
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD); throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
} }
return true; return true;

Loading…
Cancel
Save