Skip to content

Commit 5072999

Browse files
committed
Minor cosmetic changes, throw an error when rcon read fails (#42)
1 parent 3bae84c commit 5072999

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

SourceQuery/Exceptions.class.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
<?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-
}
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+
// Base exception class
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_EMPTY = 2;
30+
const BUFFER_NOT_EMPTY = 3;
31+
const CHECKSUM_MISMATCH = 4;
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public function Read( $Length = 1400 )
7171
$Buffer = $this->Buffer->Get( );
7272
$Trimmed = Trim( $Buffer );
7373

74-
if($Trimmed === 'Bad rcon_password.')
74+
if( $Trimmed === 'Bad rcon_password.' )
7575
{
76-
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
76+
throw new AuthenticationException( $Trimmed, AuthenticationException::BAD_PASSWORD );
7777
}
78-
else if($Trimmed === 'You have been banned from this server.')
78+
else if( $Trimmed === 'You have been banned from this server.' )
7979
{
80-
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
80+
throw new AuthenticationException( $Trimmed, AuthenticationException::BANNED );
8181
}
8282

8383
$ReadMore = false;

SourceQuery/Socket.class.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
use xPaw\SourceQuery\Exception\InvalidPacketException;
3-
use xPaw\SourceQuery\Exception\SocketException;
4-
5-
/**
2+
/**
63
* Class written by xPaw
74
*
85
* Website: http://xpaw.me
96
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
107
*/
118

9+
use xPaw\SourceQuery\Exception\InvalidPacketException;
10+
use xPaw\SourceQuery\Exception\SocketException;
11+
1212
class SourceQuerySocket
1313
{
1414
public $Socket;
@@ -51,7 +51,7 @@ public function Open( $Ip, $Port, $Timeout, $Engine )
5151

5252
if( $ErrNo || $this->Socket === false )
5353
{
54-
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET);
54+
throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
5555
}
5656

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

147147
if( CRC32( $Buffer ) !== $PacketChecksum )
148148
{
149-
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
149+
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
150150
}
151151
}
152152

153153
$this->Buffer->Set( SubStr( $Buffer, 4 ) );
154154
}
155155
else
156156
{
157-
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
157+
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
158158
}
159159
}
160160

SourceQuery/SourceQuery.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ public function Connect( $Ip, $Port, $Timeout = 3, $Engine = self :: SOURCE )
143143

144144
if( !is_int( $Timeout ) || $Timeout < 0 )
145145
{
146-
throw new InvalidArgumentException("Timeout must be an integer.", InvalidArgumentException::TIMEOUT_NOT_INTEGER);
146+
throw new InvalidArgumentException( 'Timeout must be an integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER );
147147
}
148148

149149
if( !$this->Socket->Open( $Ip, (int)$Port, $Timeout, (int)$Engine ) )
150150
{
151-
throw new TimeoutException("Could not connect to server.", TimeoutException::TIMEOUT_CONNECT);
151+
throw new TimeoutException( 'Could not connect to server.', TimeoutException::TIMEOUT_CONNECT );
152152
}
153153

154154
$this->Connected = true;
@@ -279,7 +279,7 @@ public function GetInfo( )
279279

280280
if( $Type !== self :: S2A_INFO )
281281
{
282-
throw new InvalidPacketException("GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')", InvalidPacketException::PACKET_HEADER_MISMATCH);
282+
throw new InvalidPacketException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
283283
}
284284

285285
$Server[ 'Protocol' ] = $this->Buffer->GetByte( );
@@ -344,8 +344,8 @@ public function GetInfo( )
344344

345345
if( $this->Buffer->Remaining( ) > 0 )
346346
{
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);
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 );
349349
}
350350
}
351351

@@ -386,7 +386,7 @@ public function GetPlayers( )
386386
}
387387
else if( $Type !== self :: S2A_PLAYER )
388388
{
389-
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
389+
throw new InvalidPacketException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
390390
}
391391

392392
break;
@@ -443,7 +443,7 @@ public function GetRules( )
443443
}
444444
else if( $Type !== self :: S2A_RULES )
445445
{
446-
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
446+
throw new InvalidPacketException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
447447
}
448448

449449
break;
@@ -512,7 +512,7 @@ private function GetChallenge( $Header, $ExpectedResult )
512512
}
513513
default:
514514
{
515-
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
515+
throw new InvalidPacketException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
516516
}
517517
}
518518
}

SourceQuery/SourceRcon.class.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
2-
use xPaw\SourceQuery\Exception\AuthenticationException;
3-
use xPaw\SourceQuery\Exception\TimeoutException;
4-
5-
/**
2+
/**
63
* Class written by xPaw
74
*
85
* Website: http://xpaw.me
96
* GitHub: https://github.com/xPaw/PHP-Source-Query-Class
107
*/
118

9+
use xPaw\SourceQuery\Exception\AuthenticationException;
10+
use xPaw\SourceQuery\Exception\TimeoutException;
11+
use xPaw\SourceQuery\Exception\InvalidPacketException;
12+
1213
class SourceQuerySourceRcon
1314
{
1415
/**
@@ -54,7 +55,7 @@ public function Open( )
5455

5556
if( $ErrNo || !$this->RconSocket )
5657
{
57-
throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT);
58+
throw new TimeoutException( 'Can\'t connect to RCON server: ' . $ErrStr, TimeoutException::TIMEOUT_CONNECT );
5859
}
5960

6061
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
@@ -78,6 +79,11 @@ public function Read( $Length = 1400 )
7879
{
7980
$this->Buffer->Set( FRead( $this->RconSocket, $Length ) );
8081

82+
if( $this->Buffer->Remaining( ) < 4 )
83+
{
84+
throw new InvalidPacketException( 'Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
85+
}
86+
8187
$PacketSize = $this->Buffer->GetLong( );
8288

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

104110
$this->Buffer->GetLong( ); // RequestID
105-
$Type = $this->Buffer->GetLong( );
111+
112+
$Type = $this->Buffer->GetLong( );
106113

107114
if( $Type === SourceQuery :: SERVERDATA_AUTH_RESPONSE )
108115
{
109-
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD);
116+
throw new AuthenticationException( 'Bad rcon_password.', AuthenticationException::BAD_PASSWORD );
110117
}
111118
else if( $Type !== SourceQuery :: SERVERDATA_RESPONSE_VALUE )
112119
{
@@ -163,7 +170,7 @@ public function Authorize( $Password )
163170

164171
if( $RequestID === -1 || $Type !== SourceQuery :: SERVERDATA_AUTH_RESPONSE )
165172
{
166-
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
173+
throw new AuthenticationException( 'RCON authorization failed.', AuthenticationException::BAD_PASSWORD );
167174
}
168175

169176
return true;

0 commit comments

Comments
 (0)