Skip to content

Commit

Permalink
Minor cosmetic changes, throw an error when rcon read fails (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Feb 17, 2015
1 parent 3bae84c commit 5072999
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
84 changes: 42 additions & 42 deletions SourceQuery/Exceptions.class.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
<?php
/**
* Class written by xPaw
*
* Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/

namespace xPaw\SourceQuery\Exception;

abstract class SourceQueryException extends \Exception
{

}

class InvalidArgumentException extends SourceQueryException
{
const TIMEOUT_NOT_INTEGER = 1;
}

class TimeoutException extends SourceQueryException
{
const TIMEOUT_CONNECT = 1;
}

class InvalidPacketException extends SourceQueryException
{
const PACKET_HEADER_MISMATCH = 1;
const BUFFER_NOT_EMPTY = 2;

const CHECKSUM_MISMATCH = 3;
}

class AuthenticationException extends SourceQueryException
{
const BAD_PASSWORD = 1;
const BANNED = 2;
}

class SocketException extends SourceQueryException
{
const COULD_NOT_CREATE_SOCKET = 1;
}
/**
* Class written by xPaw
*
* Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/
namespace xPaw\SourceQuery\Exception;
abstract class SourceQueryException extends \Exception
{
// Base exception class
}
class InvalidArgumentException extends SourceQueryException
{
const TIMEOUT_NOT_INTEGER = 1;
}
class TimeoutException extends SourceQueryException
{
const TIMEOUT_CONNECT = 1;
}
class InvalidPacketException extends SourceQueryException
{
const PACKET_HEADER_MISMATCH = 1;
const BUFFER_EMPTY = 2;
const BUFFER_NOT_EMPTY = 3;
const CHECKSUM_MISMATCH = 4;
}
class AuthenticationException extends SourceQueryException
{
const BAD_PASSWORD = 1;
const BANNED = 2;
}
class SocketException extends SourceQueryException
{
const COULD_NOT_CREATE_SOCKET = 1;
}
8 changes: 4 additions & 4 deletions SourceQuery/GoldSourceRcon.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public function Read( $Length = 1400 )
$Buffer = $this->Buffer->Get( );
$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;
Expand Down
14 changes: 7 additions & 7 deletions SourceQuery/Socket.class.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;

/**
/**
* Class written by xPaw
*
* Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
*/

use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;

class SourceQuerySocket
{
public $Socket;
Expand Down Expand Up @@ -51,7 +51,7 @@ public function Open( $Ip, $Port, $Timeout, $Engine )

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 );
Expand Down Expand Up @@ -146,15 +146,15 @@ public function Read( $Length = 1400 )

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 );
}
}

$this->Buffer->Set( SubStr( $Buffer, 4 ) );
}
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 );
}
}

Expand Down
16 changes: 8 additions & 8 deletions SourceQuery/SourceQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE )

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 ) )
{
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;
Expand Down Expand Up @@ -279,7 +279,7 @@ public function GetInfo( )

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( );
Expand Down Expand Up @@ -344,8 +344,8 @@ public function GetInfo( )

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.",
InvalidPacketException::BUFFER_NOT_EMPTY);
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 );
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ public function GetPlayers( )
}
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;
Expand Down Expand Up @@ -443,7 +443,7 @@ public function GetRules( )
}
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;
Expand Down Expand Up @@ -512,7 +512,7 @@ private function GetChallenge( $Header, $ExpectedResult )
}
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 );
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions SourceQuery/SourceRcon.class.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php
use xPaw\SourceQuery\Exception\AuthenticationException;
use xPaw\SourceQuery\Exception\TimeoutException;

/**
/**
* Class written by xPaw
*
* Website: http://xpaw.me
* 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
{
/**
Expand Down Expand Up @@ -54,7 +55,7 @@ public function Open( )

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 );
Expand All @@ -78,6 +79,11 @@ public function Read( $Length = 1400 )
{
$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( );

$Buffer = $this->Buffer->Get( );
Expand All @@ -102,11 +108,12 @@ public function Command( $Command )
$this->Read( );

$this->Buffer->GetLong( ); // RequestID
$Type = $this->Buffer->GetLong( );

$Type = $this->Buffer->GetLong( );

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 )
{
Expand Down Expand Up @@ -163,7 +170,7 @@ public function Authorize( $Password )

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;
Expand Down

0 comments on commit 5072999

Please sign in to comment.