|
1 | 1 | <?php |
2 | 2 | declare(strict_types=1); |
3 | 3 |
|
4 | | - /** |
5 | | - * @author Pavel Djundik |
6 | | - * |
7 | | - * @link https://xpaw.me |
8 | | - * @link https://github.com/xPaw/PHP-Source-Query |
9 | | - * |
10 | | - * @license GNU Lesser General Public License, version 2.1 |
11 | | - * |
12 | | - * @internal |
13 | | - */ |
14 | | - |
15 | | - namespace xPaw\SourceQuery; |
16 | | - |
17 | | - use xPaw\SourceQuery\Exception\InvalidPacketException; |
18 | | - use xPaw\SourceQuery\Exception\SocketException; |
19 | | - |
20 | | - /** |
21 | | - * Base socket interface |
22 | | - * |
23 | | - * @package xPaw\SourceQuery |
24 | | - * |
25 | | - * @uses xPaw\SourceQuery\Exception\InvalidPacketException |
26 | | - * @uses xPaw\SourceQuery\Exception\SocketException |
27 | | - */ |
28 | | - abstract class BaseSocket |
| 4 | +/** |
| 5 | + * @author Pavel Djundik |
| 6 | + * |
| 7 | + * @link https://xpaw.me |
| 8 | + * @link https://github.com/xPaw/PHP-Source-Query |
| 9 | + * |
| 10 | + * @license GNU Lesser General Public License, version 2.1 |
| 11 | + * |
| 12 | + * @internal |
| 13 | + */ |
| 14 | + |
| 15 | +namespace xPaw\SourceQuery; |
| 16 | + |
| 17 | +use xPaw\SourceQuery\Exception\InvalidPacketException; |
| 18 | +use xPaw\SourceQuery\Exception\SocketException; |
| 19 | + |
| 20 | +/** |
| 21 | + * Base socket interface |
| 22 | + * |
| 23 | + * @package xPaw\SourceQuery |
| 24 | + * |
| 25 | + * @uses xPaw\SourceQuery\Exception\InvalidPacketException |
| 26 | + * @uses xPaw\SourceQuery\Exception\SocketException |
| 27 | + */ |
| 28 | +abstract class BaseSocket |
| 29 | +{ |
| 30 | + /** @var ?resource */ |
| 31 | + public $Socket; |
| 32 | + public int $Engine; |
| 33 | + |
| 34 | + public string $Address; |
| 35 | + public int $Port; |
| 36 | + public int $Timeout; |
| 37 | + |
| 38 | + public function __destruct( ) |
29 | 39 | { |
30 | | - /** @var ?resource */ |
31 | | - public $Socket; |
32 | | - public int $Engine; |
| 40 | + $this->Close( ); |
| 41 | + } |
33 | 42 |
|
34 | | - public string $Address; |
35 | | - public int $Port; |
36 | | - public int $Timeout; |
| 43 | + abstract public function Close( ) : void; |
| 44 | + abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void; |
| 45 | + abstract public function Write( int $Header, string $String = '' ) : bool; |
| 46 | + abstract public function Read( ) : Buffer; |
37 | 47 |
|
38 | | - public function __destruct( ) |
| 48 | + protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer |
| 49 | + { |
| 50 | + if( $Buffer->Remaining( ) === 0 ) |
39 | 51 | { |
40 | | - $this->Close( ); |
| 52 | + throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY ); |
41 | 53 | } |
42 | 54 |
|
43 | | - abstract public function Close( ) : void; |
44 | | - abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void; |
45 | | - abstract public function Write( int $Header, string $String = '' ) : bool; |
46 | | - abstract public function Read( ) : Buffer; |
| 55 | + $Header = $Buffer->ReadInt32( ); |
47 | 56 |
|
48 | | - protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer |
| 57 | + if( $Header === -1 ) // Single packet |
49 | 58 | { |
50 | | - if( $Buffer->Remaining( ) === 0 ) |
51 | | - { |
52 | | - throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY ); |
53 | | - } |
54 | | - |
55 | | - $Header = $Buffer->ReadInt32( ); |
| 59 | + // We don't have to do anything |
| 60 | + } |
| 61 | + else if( $Header === -2 ) // Split packet |
| 62 | + { |
| 63 | + $Packets = []; |
| 64 | + $IsCompressed = false; |
| 65 | + $ReadMore = false; |
| 66 | + $PacketChecksum = null; |
56 | 67 |
|
57 | | - if( $Header === -1 ) // Single packet |
58 | | - { |
59 | | - // We don't have to do anything |
60 | | - } |
61 | | - else if( $Header === -2 ) // Split packet |
| 68 | + do |
62 | 69 | { |
63 | | - $Packets = []; |
64 | | - $IsCompressed = false; |
65 | | - $ReadMore = false; |
66 | | - $PacketChecksum = null; |
| 70 | + $RequestID = $Buffer->ReadInt32( ); |
67 | 71 |
|
68 | | - do |
| 72 | + switch( $this->Engine ) |
69 | 73 | { |
70 | | - $RequestID = $Buffer->ReadInt32( ); |
| 74 | + case SourceQuery::GOLDSOURCE: |
| 75 | + { |
| 76 | + $PacketCountAndNumber = $Buffer->ReadByte( ); |
| 77 | + $PacketCount = $PacketCountAndNumber & 0xF; |
| 78 | + $PacketNumber = $PacketCountAndNumber >> 4; |
71 | 79 |
|
72 | | - switch( $this->Engine ) |
| 80 | + break; |
| 81 | + } |
| 82 | + case SourceQuery::SOURCE: |
73 | 83 | { |
74 | | - case SourceQuery::GOLDSOURCE: |
75 | | - { |
76 | | - $PacketCountAndNumber = $Buffer->ReadByte( ); |
77 | | - $PacketCount = $PacketCountAndNumber & 0xF; |
78 | | - $PacketNumber = $PacketCountAndNumber >> 4; |
| 84 | + $IsCompressed = ( $RequestID & 0x80000000 ) !== 0; |
| 85 | + $PacketCount = $Buffer->ReadByte( ); |
| 86 | + $PacketNumber = $Buffer->ReadByte( ) + 1; |
79 | 87 |
|
80 | | - break; |
81 | | - } |
82 | | - case SourceQuery::SOURCE: |
| 88 | + if( $IsCompressed ) |
83 | 89 | { |
84 | | - $IsCompressed = ( $RequestID & 0x80000000 ) !== 0; |
85 | | - $PacketCount = $Buffer->ReadByte( ); |
86 | | - $PacketNumber = $Buffer->ReadByte( ) + 1; |
87 | | - |
88 | | - if( $IsCompressed ) |
89 | | - { |
90 | | - $Buffer->ReadInt32( ); // Split size |
91 | | - |
92 | | - $PacketChecksum = $Buffer->ReadUInt32( ); |
93 | | - } |
94 | | - else |
95 | | - { |
96 | | - $Buffer->ReadInt16( ); // Split size |
97 | | - } |
98 | | - |
99 | | - break; |
| 90 | + $Buffer->ReadInt32( ); // Split size |
| 91 | + |
| 92 | + $PacketChecksum = $Buffer->ReadUInt32( ); |
100 | 93 | } |
101 | | - default: |
| 94 | + else |
102 | 95 | { |
103 | | - throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE ); |
| 96 | + $Buffer->ReadInt16( ); // Split size |
104 | 97 | } |
| 98 | + |
| 99 | + break; |
| 100 | + } |
| 101 | + default: |
| 102 | + { |
| 103 | + throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE ); |
105 | 104 | } |
| 105 | + } |
106 | 106 |
|
107 | | - $Packets[ $PacketNumber ] = $Buffer->Read( ); |
| 107 | + $Packets[ $PacketNumber ] = $Buffer->Read( ); |
108 | 108 |
|
109 | | - $ReadMore = $PacketCount > sizeof( $Packets ); |
110 | | - } |
111 | | - while( $ReadMore && $SherlockFunction( $Buffer ) ); |
| 109 | + $ReadMore = $PacketCount > sizeof( $Packets ); |
| 110 | + } |
| 111 | + while( $ReadMore && $SherlockFunction( $Buffer ) ); |
112 | 112 |
|
113 | | - $Data = implode( $Packets ); |
| 113 | + $Data = implode( $Packets ); |
114 | 114 |
|
115 | | - // TODO: Test this |
116 | | - if( $IsCompressed ) |
| 115 | + // TODO: Test this |
| 116 | + if( $IsCompressed ) |
| 117 | + { |
| 118 | + // Let's make sure this function exists, it's not included in PHP by default |
| 119 | + if( !function_exists( 'bzdecompress' ) ) |
117 | 120 | { |
118 | | - // Let's make sure this function exists, it's not included in PHP by default |
119 | | - if( !function_exists( 'bzdecompress' ) ) |
120 | | - { |
121 | | - throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); |
122 | | - } |
| 121 | + throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' ); |
| 122 | + } |
123 | 123 |
|
124 | | - $Data = bzdecompress( $Data ); |
| 124 | + $Data = bzdecompress( $Data ); |
125 | 125 |
|
126 | | - if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum ) |
127 | | - { |
128 | | - throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); |
129 | | - } |
| 126 | + if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum ) |
| 127 | + { |
| 128 | + throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH ); |
130 | 129 | } |
131 | | - |
132 | | - $Buffer->Set( substr( $Data, 4 ) ); |
133 | | - } |
134 | | - else |
135 | | - { |
136 | | - throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); |
137 | 130 | } |
138 | 131 |
|
139 | | - return $Buffer; |
| 132 | + $Buffer->Set( substr( $Data, 4 ) ); |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH ); |
140 | 137 | } |
| 138 | + |
| 139 | + return $Buffer; |
141 | 140 | } |
| 141 | +} |
0 commit comments