1111 *
1212 * @internal
1313 */
14-
14+
1515 namespace xPaw \SourceQuery ;
16-
16+
1717 use xPaw \SourceQuery \Exception \InvalidPacketException ;
1818 use xPaw \SourceQuery \Exception \SocketException ;
1919
@@ -30,30 +30,30 @@ abstract class BaseSocket
3030 /** @var ?resource */
3131 public $ Socket ;
3232 public int $ Engine ;
33-
33+
3434 public string $ Address ;
3535 public int $ Port ;
3636 public int $ Timeout ;
37-
37+
3838 public function __destruct ( )
3939 {
4040 $ this ->Close ( );
4141 }
42-
42+
4343 abstract public function Close ( ) : void ;
4444 abstract public function Open ( string $ Address , int $ Port , int $ Timeout , int $ Engine ) : void ;
4545 abstract public function Write ( int $ Header , string $ String = '' ) : bool ;
4646 abstract public function Read ( ) : Buffer ;
47-
47+
4848 protected function ReadInternal ( Buffer $ Buffer , callable $ SherlockFunction ) : Buffer
4949 {
5050 if ( $ Buffer ->Remaining ( ) === 0 )
5151 {
5252 throw new InvalidPacketException ( 'Failed to read any data from socket ' , InvalidPacketException::BUFFER_EMPTY );
5353 }
54-
54+
5555 $ Header = $ Buffer ->GetLong ( );
56-
56+
5757 if ( $ Header === -1 ) // Single packet
5858 {
5959 // We don't have to do anything
@@ -64,54 +64,54 @@ protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) :
6464 $ IsCompressed = false ;
6565 $ ReadMore = false ;
6666 $ PacketChecksum = null ;
67-
67+
6868 do
6969 {
7070 $ RequestID = $ Buffer ->GetLong ( );
71-
71+
7272 switch ( $ this ->Engine )
7373 {
7474 case SourceQuery::GOLDSOURCE :
7575 {
7676 $ PacketCountAndNumber = $ Buffer ->GetByte ( );
7777 $ PacketCount = $ PacketCountAndNumber & 0xF ;
7878 $ PacketNumber = $ PacketCountAndNumber >> 4 ;
79-
79+
8080 break ;
8181 }
8282 case SourceQuery::SOURCE :
8383 {
8484 $ IsCompressed = ( $ RequestID & 0x80000000 ) !== 0 ;
8585 $ PacketCount = $ Buffer ->GetByte ( );
8686 $ PacketNumber = $ Buffer ->GetByte ( ) + 1 ;
87-
87+
8888 if ( $ IsCompressed )
8989 {
9090 $ Buffer ->GetLong ( ); // Split size
91-
91+
9292 $ PacketChecksum = $ Buffer ->GetUnsignedLong ( );
9393 }
9494 else
9595 {
9696 $ Buffer ->GetShort ( ); // Split size
9797 }
98-
98+
9999 break ;
100100 }
101101 default :
102102 {
103103 throw new SocketException ( 'Unknown engine. ' , SocketException::INVALID_ENGINE );
104104 }
105105 }
106-
106+
107107 $ Packets [ $ PacketNumber ] = $ Buffer ->Get ( );
108-
108+
109109 $ ReadMore = $ PacketCount > sizeof ( $ Packets );
110110 }
111111 while ( $ ReadMore && $ SherlockFunction ( $ Buffer ) );
112-
112+
113113 $ Data = implode ( $ Packets );
114-
114+
115115 // TODO: Test this
116116 if ( $ IsCompressed )
117117 {
@@ -120,22 +120,22 @@ protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) :
120120 {
121121 throw new \RuntimeException ( 'Received compressed packet, PHP doesn \'t have Bzip2 library installed, can \'t decompress. ' );
122122 }
123-
123+
124124 $ Data = bzdecompress ( $ Data );
125-
125+
126126 if ( !is_string ( $ Data ) || crc32 ( $ Data ) !== $ PacketChecksum )
127127 {
128128 throw new InvalidPacketException ( 'CRC32 checksum mismatch of uncompressed packet data. ' , InvalidPacketException::CHECKSUM_MISMATCH );
129129 }
130130 }
131-
131+
132132 $ Buffer ->Set ( substr ( $ Data , 4 ) );
133133 }
134134 else
135135 {
136136 throw new InvalidPacketException ( 'Socket read: Raw packet header mismatch. (0x ' . dechex ( $ Header ) . ') ' , InvalidPacketException::PACKET_HEADER_MISMATCH );
137137 }
138-
138+
139139 return $ Buffer ;
140140 }
141141 }
0 commit comments