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 }
0 commit comments