From 50729990c636abb74c7610298ada8ca9d481dd71 Mon Sep 17 00:00:00 2001 From: xPaw Date: Tue, 17 Feb 2015 13:42:23 +0200 Subject: [PATCH] Minor cosmetic changes, throw an error when rcon read fails (#42) --- SourceQuery/Exceptions.class.php | 84 ++++++++++++++-------------- SourceQuery/GoldSourceRcon.class.php | 8 +-- SourceQuery/Socket.class.php | 14 ++--- SourceQuery/SourceQuery.class.php | 16 +++--- SourceQuery/SourceRcon.class.php | 23 +++++--- 5 files changed, 76 insertions(+), 69 deletions(-) diff --git a/SourceQuery/Exceptions.class.php b/SourceQuery/Exceptions.class.php index 87c1628..f9b3c29 100644 --- a/SourceQuery/Exceptions.class.php +++ b/SourceQuery/Exceptions.class.php @@ -1,43 +1,43 @@ 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; diff --git a/SourceQuery/Socket.class.php b/SourceQuery/Socket.class.php index e8f46f6..3e3113d 100644 --- a/SourceQuery/Socket.class.php +++ b/SourceQuery/Socket.class.php @@ -1,14 +1,14 @@ 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 ); @@ -146,7 +146,7 @@ use xPaw\SourceQuery\Exception\SocketException; 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 { - 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 ); } } diff --git a/SourceQuery/SourceQuery.class.php b/SourceQuery/SourceQuery.class.php index fb359b4..f44e2be 100644 --- a/SourceQuery/SourceQuery.class.php +++ b/SourceQuery/SourceQuery.class.php @@ -143,12 +143,12 @@ 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; @@ -279,7 +279,7 @@ 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( ); @@ -344,8 +344,8 @@ 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 ); } } @@ -386,7 +386,7 @@ } 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; @@ -443,7 +443,7 @@ } 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; @@ -512,7 +512,7 @@ } 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 ); } } } diff --git a/SourceQuery/SourceRcon.class.php b/SourceQuery/SourceRcon.class.php index fd35fbf..d6c9b41 100644 --- a/SourceQuery/SourceRcon.class.php +++ b/SourceQuery/SourceRcon.class.php @@ -1,14 +1,15 @@ 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 ); @@ -78,6 +79,11 @@ use xPaw\SourceQuery\Exception\TimeoutException; { $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( ); @@ -102,11 +108,12 @@ use xPaw\SourceQuery\Exception\TimeoutException; $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 ) { @@ -163,7 +170,7 @@ use xPaw\SourceQuery\Exception\TimeoutException; 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;