diff --git a/SourceQuery/Exception.class.php b/SourceQuery/Exception.class.php deleted file mode 100644 index 2ef995d..0000000 --- a/SourceQuery/Exception.class.php +++ /dev/null @@ -1,12 +0,0 @@ -Socket->Socket, $Command, $Length ); } - + + /** + * @param int $Length + * @throws AuthenticationException + */ public function Read( $Length = 1400 ) { // GoldSource RCON has same structure as Query @@ -65,10 +71,13 @@ $Buffer = $this->Buffer->Get( ); $Trimmed = Trim( $Buffer ); - if( $Trimmed === 'Bad rcon_password.' - || $Trimmed === 'You have been banned from this server.' ) + if($Trimmed === 'Bad rcon_password.') + { + throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD); + } + else if($Trimmed === 'You have been banned from this server.') { - throw new SourceQueryException( $Trimmed ); + throw new AuthenticationException($Trimmed, AuthenticationException::BANNED); } $ReadMore = false; diff --git a/SourceQuery/Socket.class.php b/SourceQuery/Socket.class.php index 2518a47..e8f46f6 100644 --- a/SourceQuery/Socket.class.php +++ b/SourceQuery/Socket.class.php @@ -1,5 +1,8 @@ Socket === false ) { - throw new Exception( 'Could not create socket: ' . $ErrStr ); + throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET); } Stream_Set_Timeout( $this->Socket, $Timeout ); @@ -143,7 +146,7 @@ if( CRC32( $Buffer ) !== $PacketChecksum ) { - throw new SourceQueryException( 'CRC32 checksum mismatch of uncompressed packet data.' ); + throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH); } } @@ -151,7 +154,7 @@ } else { - throw new SourceQueryException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')' ); + 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 47bf8b2..fb359b4 100644 --- a/SourceQuery/SourceQuery.class.php +++ b/SourceQuery/SourceQuery.class.php @@ -14,12 +14,16 @@ define( '__DIR__', dirname( __FILE__ ) ); } - require __DIR__ . '/Exception.class.php'; + require __DIR__ . '/Exceptions.class.php'; require __DIR__ . '/Buffer.class.php'; require __DIR__ . '/Socket.class.php'; require __DIR__ . '/SourceRcon.class.php'; require __DIR__ . '/GoldSourceRcon.class.php'; - + + use xPaw\SourceQuery\Exception\InvalidArgumentException; + use xPaw\SourceQuery\Exception\TimeoutException; + use xPaw\SourceQuery\Exception\InvalidPacketException; + class SourceQuery { /** @@ -121,7 +125,7 @@ { $this->Disconnect( ); } - + /** * Opens connection to server * @@ -130,8 +134,8 @@ * @param int $Timeout Timeout period * @param int $Engine Engine the server runs on (goldsource, source) * - * @throws SourceQueryException - * @throws InvalidArgumentException If timeout is not an integer + * @throws InvalidArgumentException + * @throws TimeoutException */ public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE ) { @@ -139,12 +143,12 @@ if( !is_int( $Timeout ) || $Timeout < 0 ) { - throw new InvalidArgumentException( 'Timeout must be an 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 SourceQueryException( 'Can\'t connect to the server.' ); + throw new TimeoutException("Could not connect to server.", TimeoutException::TIMEOUT_CONNECT); } $this->Connected = true; @@ -208,7 +212,7 @@ /** * Get server information * - * @throws SourceQueryException + * @throws InvalidPacketException * * @return bool|array Returns array with information on success, false on failure */ @@ -275,7 +279,7 @@ if( $Type !== self :: S2A_INFO ) { - throw new SourceQueryException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')' ); + throw new InvalidPacketException("GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')", InvalidPacketException::PACKET_HEADER_MISMATCH); } $Server[ 'Protocol' ] = $this->Buffer->GetByte( ); @@ -340,7 +344,8 @@ if( $this->Buffer->Remaining( ) > 0 ) { - throw new SourceQueryException( '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); } } @@ -350,7 +355,7 @@ /** * Get players on the server * - * @throws SourceQueryException + * @throws InvalidPacketException * * @return bool|array Returns array with players on success, false on failure */ @@ -381,7 +386,7 @@ } else if( $Type !== self :: S2A_PLAYER ) { - throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')' ); + throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); } break; @@ -408,7 +413,7 @@ /** * Get rules (cvars) from the server * - * @throws SourceQueryException + * @throws InvalidPacketException * * @return bool|array Returns array with rules on success, false on failure */ @@ -438,7 +443,7 @@ } else if( $Type !== self :: S2A_RULES ) { - throw new SourceQueryException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')' ); + throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); } break; @@ -461,10 +466,13 @@ return $Rules; } - + /** * Get challenge (used for players/rules packets) * + * @param $Header + * @param $ExpectedResult + * @throws InvalidPacketException * @return bool True if all went well, false if server uses old GoldSource protocol, and it already contains answer */ private function GetChallenge( $Header, $ExpectedResult ) @@ -504,7 +512,7 @@ } default: { - throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')' ); + 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 ea6000a..fd35fbf 100644 --- a/SourceQuery/SourceRcon.class.php +++ b/SourceQuery/SourceRcon.class.php @@ -1,5 +1,8 @@ RconSocket ) { - throw new SourceQueryException( 'Can\'t connect to RCON server: ' . $ErrStr ); + throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT); } Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout ); @@ -103,7 +106,7 @@ if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE ) { - throw new SourceQueryException( 'Bad rcon_password.' ); + throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD); } else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE ) { @@ -160,7 +163,7 @@ if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE ) { - throw new SourceQueryException( 'RCON authorization failed.' ); + throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD); } return true;