Skip to content

Commit cfcd431

Browse files
committed
Proposal for better Exceptions
1 parent 6d3355f commit cfcd431

File tree

6 files changed

+94
-40
lines changed

6 files changed

+94
-40
lines changed

SourceQuery/Exception.class.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

SourceQuery/Exceptions.class.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Class written by xPaw
4+
*
5+
* Website: http://xpaw.me
6+
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
7+
*/
8+
9+
namespace xPaw\SourceQuery\Exception;
10+
11+
abstract class SourceQueryException extends \Exception
12+
{
13+
14+
}
15+
16+
class InvalidArgumentException extends SourceQueryException
17+
{
18+
const TIMEOUT_NOT_INTEGER = 1;
19+
}
20+
21+
class TimeoutException extends SourceQueryException
22+
{
23+
const TIMEOUT_CONNECT = 1;
24+
}
25+
26+
class InvalidPacketException extends SourceQueryException
27+
{
28+
const PACKET_HEADER_MISMATCH = 1;
29+
const BUFFER_NOT_EMPTY = 2;
30+
31+
const CHECKSUM_MISMATCH = 3;
32+
}
33+
34+
class AuthenticationException extends SourceQueryException
35+
{
36+
const BAD_PASSWORD = 1;
37+
const BANNED = 2;
38+
}
39+
40+
class SocketException extends SourceQueryException
41+
{
42+
const COULD_NOT_CREATE_SOCKET = 1;
43+
}

SourceQuery/GoldSourceRcon.class.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Website: http://xpaw.me
66
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
77
*/
8+
9+
use xPaw\SourceQuery\Exception\AuthenticationException;
810

911
class SourceQueryGoldSourceRcon
1012
{
@@ -51,7 +53,11 @@ public function Write( $Header, $String = '' )
5153

5254
return $Length === FWrite( $this->Socket->Socket, $Command, $Length );
5355
}
54-
56+
57+
/**
58+
* @param int $Length
59+
* @throws AuthenticationException
60+
*/
5561
public function Read( $Length = 1400 )
5662
{
5763
// GoldSource RCON has same structure as Query
@@ -65,10 +71,13 @@ public function Read( $Length = 1400 )
6571
$Buffer = $this->Buffer->Get( );
6672
$Trimmed = Trim( $Buffer );
6773

68-
if( $Trimmed === 'Bad rcon_password.'
69-
|| $Trimmed === 'You have been banned from this server.' )
74+
if($Trimmed === 'Bad rcon_password.')
75+
{
76+
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
77+
}
78+
else if($Trimmed === 'You have been banned from this server.')
7079
{
71-
throw new SourceQueryException( $Trimmed );
80+
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
7281
}
7382

7483
$ReadMore = false;

SourceQuery/Socket.class.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2-
/**
2+
use xPaw\SourceQuery\Exception\InvalidPacketException;
3+
use xPaw\SourceQuery\Exception\SocketException;
4+
5+
/**
36
* Class written by xPaw
47
*
58
* Website: http://xpaw.me
@@ -48,7 +51,7 @@ public function Open( $Ip, $Port, $Timeout, $Engine )
4851

4952
if( $ErrNo || $this->Socket === false )
5053
{
51-
throw new Exception( 'Could not create socket: ' . $ErrStr );
54+
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET);
5255
}
5356

5457
Stream_Set_Timeout( $this->Socket, $Timeout );
@@ -143,15 +146,15 @@ public function Read( $Length = 1400 )
143146

144147
if( CRC32( $Buffer ) !== $PacketChecksum )
145148
{
146-
throw new SourceQueryException( 'CRC32 checksum mismatch of uncompressed packet data.' );
149+
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
147150
}
148151
}
149152

150153
$this->Buffer->Set( SubStr( $Buffer, 4 ) );
151154
}
152155
else
153156
{
154-
throw new SourceQueryException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')' );
157+
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
155158
}
156159
}
157160

SourceQuery/SourceQuery.class.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
define( '__DIR__', dirname( __FILE__ ) );
1515
}
1616

17-
require __DIR__ . '/Exception.class.php';
17+
require __DIR__ . '/Exceptions.class.php';
1818
require __DIR__ . '/Buffer.class.php';
1919
require __DIR__ . '/Socket.class.php';
2020
require __DIR__ . '/SourceRcon.class.php';
2121
require __DIR__ . '/GoldSourceRcon.class.php';
22-
22+
23+
use xPaw\SourceQuery\Exception\InvalidArgumentException;
24+
use xPaw\SourceQuery\Exception\TimeoutException;
25+
use xPaw\SourceQuery\Exception\InvalidPacketException;
26+
2327
class SourceQuery
2428
{
2529
/**
@@ -121,7 +125,7 @@ public function __destruct( )
121125
{
122126
$this->Disconnect( );
123127
}
124-
128+
125129
/**
126130
* Opens connection to server
127131
*
@@ -130,21 +134,21 @@ public function __destruct( )
130134
* @param int $Timeout Timeout period
131135
* @param int $Engine Engine the server runs on (goldsource, source)
132136
*
133-
* @throws SourceQueryException
134-
* @throws InvalidArgumentException If timeout is not an integer
137+
* @throws InvalidArgumentException
138+
* @throws TimeoutException
135139
*/
136140
public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE )
137141
{
138142
$this->Disconnect( );
139143

140144
if( !is_int( $Timeout ) || $Timeout < 0 )
141145
{
142-
throw new InvalidArgumentException( 'Timeout must be an integer.' );
146+
throw new InvalidArgumentException("Timeout must be an integer.", InvalidArgumentException::TIMEOUT_NOT_INTEGER);
143147
}
144148

145149
if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) )
146150
{
147-
throw new SourceQueryException( 'Can\'t connect to the server.' );
151+
throw new TimeoutException("Could not connect to server.", TimeoutException::TIMEOUT_CONNECT);
148152
}
149153

150154
$this->Connected = true;
@@ -208,7 +212,7 @@ public function Ping( )
208212
/**
209213
* Get server information
210214
*
211-
* @throws SourceQueryException
215+
* @throws InvalidPacketException
212216
*
213217
* @return bool|array Returns array with information on success, false on failure
214218
*/
@@ -275,7 +279,7 @@ public function GetInfo( )
275279

276280
if( $Type !== self :: S2A_INFO )
277281
{
278-
throw new SourceQueryException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
282+
throw new InvalidPacketException("GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')", InvalidPacketException::PACKET_HEADER_MISMATCH);
279283
}
280284

281285
$Server[ 'Protocol' ] = $this->Buffer->GetByte( );
@@ -340,7 +344,8 @@ public function GetInfo( )
340344

341345
if( $this->Buffer->Remaining( ) > 0 )
342346
{
343-
throw new SourceQueryException( 'GetInfo: unread data? ' . $this->Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.' );
347+
throw new InvalidPacketException("GetInfo: unread data? " . $this->Buffer->Remaining( ) . " bytes remaining in the buffer. Please report it to the library developer.",
348+
InvalidPacketException::BUFFER_NOT_EMPTY);
344349
}
345350
}
346351

@@ -350,7 +355,7 @@ public function GetInfo( )
350355
/**
351356
* Get players on the server
352357
*
353-
* @throws SourceQueryException
358+
* @throws InvalidPacketException
354359
*
355360
* @return bool|array Returns array with players on success, false on failure
356361
*/
@@ -381,7 +386,7 @@ public function GetPlayers( )
381386
}
382387
else if( $Type !== self :: S2A_PLAYER )
383388
{
384-
throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
389+
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
385390
}
386391

387392
break;
@@ -408,7 +413,7 @@ public function GetPlayers( )
408413
/**
409414
* Get rules (cvars) from the server
410415
*
411-
* @throws SourceQueryException
416+
* @throws InvalidPacketException
412417
*
413418
* @return bool|array Returns array with rules on success, false on failure
414419
*/
@@ -438,7 +443,7 @@ public function GetRules( )
438443
}
439444
else if( $Type !== self :: S2A_RULES )
440445
{
441-
throw new SourceQueryException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
446+
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
442447
}
443448

444449
break;
@@ -461,10 +466,13 @@ public function GetRules( )
461466

462467
return $Rules;
463468
}
464-
469+
465470
/**
466471
* Get challenge (used for players/rules packets)
467472
*
473+
* @param $Header
474+
* @param $ExpectedResult
475+
* @throws InvalidPacketException
468476
* @return bool True if all went well, false if server uses old GoldSource protocol, and it already contains answer
469477
*/
470478
private function GetChallenge( $Header, $ExpectedResult )
@@ -504,7 +512,7 @@ private function GetChallenge( $Header, $ExpectedResult )
504512
}
505513
default:
506514
{
507-
throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
515+
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
508516
}
509517
}
510518
}

SourceQuery/SourceRcon.class.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2-
/**
2+
use xPaw\SourceQuery\Exception\AuthenticationException;
3+
use xPaw\SourceQuery\Exception\TimeoutException;
4+
5+
/**
36
* Class written by xPaw
47
*
58
* Website: http://xpaw.me
@@ -51,7 +54,7 @@ public function Open( )
5154

5255
if( $ErrNo || !$this->RconSocket )
5356
{
54-
throw new SourceQueryException( 'Can\'t connect to RCON server: ' . $ErrStr );
57+
throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT);
5558
}
5659

5760
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
@@ -103,7 +106,7 @@ public function Command( $Command )
103106

104107
if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE )
105108
{
106-
throw new SourceQueryException( 'Bad rcon_password.' );
109+
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD);
107110
}
108111
else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
109112
{
@@ -160,7 +163,7 @@ public function Authorize( $Password )
160163

161164
if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE )
162165
{
163-
throw new SourceQueryException( 'RCON authorization failed.' );
166+
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
164167
}
165168

166169
return true;

0 commit comments

Comments
 (0)