Skip to content

Commit 825e6ae

Browse files
committed
Reduce indent
1 parent dc5c130 commit 825e6ae

13 files changed

+1186
-1186
lines changed

Examples/Example.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types=1);
33

4-
require __DIR__ . '/../vendor/autoload.php';
4+
require __DIR__ . '/../vendor/autoload.php';
55

6-
use xPaw\SourceQuery\SourceQuery;
6+
use xPaw\SourceQuery\SourceQuery;
77

8-
// For the sake of this example
9-
header( 'Content-Type: text/plain' );
10-
header( 'X-Content-Type-Options: nosniff' );
8+
// For the sake of this example
9+
header( 'Content-Type: text/plain' );
10+
header( 'X-Content-Type-Options: nosniff' );
1111

12-
// Edit this ->
13-
define( 'SQ_SERVER_ADDR', 'localhost' );
14-
define( 'SQ_SERVER_PORT', 27015 );
15-
define( 'SQ_TIMEOUT', 1 );
16-
define( 'SQ_ENGINE', SourceQuery::SOURCE );
17-
// Edit this <-
12+
// Edit this ->
13+
define( 'SQ_SERVER_ADDR', 'localhost' );
14+
define( 'SQ_SERVER_PORT', 27015 );
15+
define( 'SQ_TIMEOUT', 1 );
16+
define( 'SQ_ENGINE', SourceQuery::SOURCE );
17+
// Edit this <-
1818

19-
$Query = new SourceQuery( );
19+
$Query = new SourceQuery( );
2020

21-
try
22-
{
23-
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
21+
try
22+
{
23+
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
2424

25-
print_r( $Query->GetInfo( ) );
26-
print_r( $Query->GetPlayers( ) );
27-
print_r( $Query->GetRules( ) );
28-
}
29-
catch( Exception $e )
30-
{
31-
echo $e->getMessage( );
32-
}
33-
finally
34-
{
35-
$Query->Disconnect( );
36-
}
25+
print_r( $Query->GetInfo( ) );
26+
print_r( $Query->GetPlayers( ) );
27+
print_r( $Query->GetRules( ) );
28+
}
29+
catch( Exception $e )
30+
{
31+
echo $e->getMessage( );
32+
}
33+
finally
34+
{
35+
$Query->Disconnect( );
36+
}

Examples/RconExample.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<?php
2-
declare(strict_types=1);
2+
declare(strict_types=1);
33

4-
require __DIR__ . '/../vendor/autoload.php';
4+
require __DIR__ . '/../vendor/autoload.php';
55

6-
use xPaw\SourceQuery\SourceQuery;
6+
use xPaw\SourceQuery\SourceQuery;
77

8-
// For the sake of this example
9-
header( 'Content-Type: text/plain' );
10-
header( 'X-Content-Type-Options: nosniff' );
8+
// For the sake of this example
9+
header( 'Content-Type: text/plain' );
10+
header( 'X-Content-Type-Options: nosniff' );
1111

12-
// Edit this ->
13-
define( 'SQ_SERVER_ADDR', 'localhost' );
14-
define( 'SQ_SERVER_PORT', 27015 );
15-
define( 'SQ_TIMEOUT', 1 );
16-
define( 'SQ_ENGINE', SourceQuery::SOURCE );
17-
// Edit this <-
12+
// Edit this ->
13+
define( 'SQ_SERVER_ADDR', 'localhost' );
14+
define( 'SQ_SERVER_PORT', 27015 );
15+
define( 'SQ_TIMEOUT', 1 );
16+
define( 'SQ_ENGINE', SourceQuery::SOURCE );
17+
// Edit this <-
1818

19-
$Query = new SourceQuery( );
19+
$Query = new SourceQuery( );
2020

21-
try
22-
{
23-
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
21+
try
22+
{
23+
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
2424

25-
$Query->SetRconPassword( 'my_awesome_password' );
25+
$Query->SetRconPassword( 'my_awesome_password' );
2626

27-
var_dump( $Query->Rcon( 'say hello' ) );
28-
}
29-
catch( Exception $e )
30-
{
31-
echo $e->getMessage( );
32-
}
33-
finally
34-
{
35-
$Query->Disconnect( );
36-
}
27+
var_dump( $Query->Rcon( 'say hello' ) );
28+
}
29+
catch( Exception $e )
30+
{
31+
echo $e->getMessage( );
32+
}
33+
finally
34+
{
35+
$Query->Disconnect( );
36+
}

SourceQuery/BaseSocket.php

Lines changed: 106 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,141 @@
11
<?php
22
declare(strict_types=1);
33

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( )
2939
{
30-
/** @var ?resource */
31-
public $Socket;
32-
public int $Engine;
40+
$this->Close( );
41+
}
3342

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;
3747

38-
public function __destruct( )
48+
protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer
49+
{
50+
if( $Buffer->Remaining( ) === 0 )
3951
{
40-
$this->Close( );
52+
throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
4153
}
4254

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( );
4756

48-
protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer
57+
if( $Header === -1 ) // Single packet
4958
{
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;
5667

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
6269
{
63-
$Packets = [];
64-
$IsCompressed = false;
65-
$ReadMore = false;
66-
$PacketChecksum = null;
70+
$RequestID = $Buffer->ReadInt32( );
6771

68-
do
72+
switch( $this->Engine )
6973
{
70-
$RequestID = $Buffer->ReadInt32( );
74+
case SourceQuery::GOLDSOURCE:
75+
{
76+
$PacketCountAndNumber = $Buffer->ReadByte( );
77+
$PacketCount = $PacketCountAndNumber & 0xF;
78+
$PacketNumber = $PacketCountAndNumber >> 4;
7179

72-
switch( $this->Engine )
80+
break;
81+
}
82+
case SourceQuery::SOURCE:
7383
{
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;
7987

80-
break;
81-
}
82-
case SourceQuery::SOURCE:
88+
if( $IsCompressed )
8389
{
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( );
10093
}
101-
default:
94+
else
10295
{
103-
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
96+
$Buffer->ReadInt16( ); // Split size
10497
}
98+
99+
break;
100+
}
101+
default:
102+
{
103+
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
105104
}
105+
}
106106

107-
$Packets[ $PacketNumber ] = $Buffer->Read( );
107+
$Packets[ $PacketNumber ] = $Buffer->Read( );
108108

109-
$ReadMore = $PacketCount > sizeof( $Packets );
110-
}
111-
while( $ReadMore && $SherlockFunction( $Buffer ) );
109+
$ReadMore = $PacketCount > sizeof( $Packets );
110+
}
111+
while( $ReadMore && $SherlockFunction( $Buffer ) );
112112

113-
$Data = implode( $Packets );
113+
$Data = implode( $Packets );
114114

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' ) )
117120
{
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+
}
123123

124-
$Data = bzdecompress( $Data );
124+
$Data = bzdecompress( $Data );
125125

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 );
130129
}
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 );
137130
}
138131

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 );
140137
}
138+
139+
return $Buffer;
141140
}
141+
}

0 commit comments

Comments
 (0)