Skip to content

Commit

Permalink
Trim trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Sep 24, 2024
1 parent 8b16be3 commit 951e2c3
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 305 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ root = true
charset = utf-8
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.yaml]
indent_style = space
Expand Down
12 changes: 6 additions & 6 deletions Examples/Example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php
declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use xPaw\SourceQuery\SourceQuery;

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

// Edit this ->
define( 'SQ_SERVER_ADDR', 'localhost' );
define( 'SQ_SERVER_PORT', 27015 );
define( 'SQ_TIMEOUT', 1 );
define( 'SQ_ENGINE', SourceQuery::SOURCE );
// Edit this <-

$Query = new SourceQuery( );

try
{
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

print_r( $Query->GetInfo( ) );
print_r( $Query->GetPlayers( ) );
print_r( $Query->GetRules( ) );
Expand Down
14 changes: 7 additions & 7 deletions Examples/RconExample.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?php
declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use xPaw\SourceQuery\SourceQuery;

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

// Edit this ->
define( 'SQ_SERVER_ADDR', 'localhost' );
define( 'SQ_SERVER_PORT', 27015 );
define( 'SQ_TIMEOUT', 1 );
define( 'SQ_ENGINE', SourceQuery::SOURCE );
// Edit this <-

$Query = new SourceQuery( );

try
{
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

$Query->SetRconPassword( 'my_awesome_password' );

var_dump( $Query->Rcon( 'say hello' ) );
}
catch( Exception $e )
Expand Down
30 changes: 15 additions & 15 deletions Examples/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
require __DIR__ . '/../vendor/autoload.php';

use xPaw\SourceQuery\SourceQuery;

// Edit this ->
define( 'SQ_SERVER_ADDR', 'localhost' );
define( 'SQ_SERVER_PORT', 27015 );
define( 'SQ_TIMEOUT', 3 );
define( 'SQ_ENGINE', SourceQuery::SOURCE );
// Edit this <-

$Timer = microtime( true );

$Query = new SourceQuery( );

$Info = [];
$Rules = [];
$Players = [];
$Exception = null;

try
{
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
//$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound

$Info = $Query->GetInfo( );
$Players = $Query->GetPlayers( );
$Rules = $Query->GetRules( );
Expand All @@ -38,36 +38,36 @@
{
$Query->Disconnect( );
}

$Timer = number_format( microtime( true ) - $Timer, 4, '.', '' );
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Source Query PHP Library</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style type="text/css">
.table {
table-layout: fixed;
border-top-color: #428BCA;
}

.table td {
overflow-x: auto;
}

.table thead th {
background-color: #428BCA;
border-color: #428BCA !important;
color: #FFF;
}

.info-column {
width: 120px;
}

.frags-column {
width: 80px;
}
Expand All @@ -78,17 +78,17 @@
<div class="jumbotron">
<div class="container">
<h1>Source Query PHP Library</h1>

<p class="lead">This library was created to query game server which use the Source (Steamworks) query protocol.</p>

<p>
<a class="btn btn-large btn-primary" href="https://xpaw.me">Made by xPaw</a>
<a class="btn btn-large btn-primary" href="https://github.com/xPaw/PHP-Source-Query">View on GitHub</a>
<a class="btn btn-large btn-danger" href="https://github.com/xPaw/PHP-Source-Query/blob/master/LICENSE">LGPL v2.1</a>
</p>
</div>
</div>

<div class="container">
<?php if( $Exception !== null ): ?>
<div class="panel panel-error">
Expand Down
44 changes: 22 additions & 22 deletions SourceQuery/BaseSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*
* @internal
*/

namespace xPaw\SourceQuery;

use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;

Expand All @@ -30,30 +30,30 @@ abstract class BaseSocket
/** @var ?resource */
public $Socket;
public int $Engine;

public string $Address;
public int $Port;
public int $Timeout;

public function __destruct( )
{
$this->Close( );
}

abstract public function Close( ) : void;
abstract public function Open( string $Address, int $Port, int $Timeout, int $Engine ) : void;
abstract public function Write( int $Header, string $String = '' ) : bool;
abstract public function Read( ) : Buffer;

protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer
{
if( $Buffer->Remaining( ) === 0 )
{
throw new InvalidPacketException( 'Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY );
}

$Header = $Buffer->GetLong( );

if( $Header === -1 ) // Single packet
{
// We don't have to do anything
Expand All @@ -64,54 +64,54 @@ protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) :
$IsCompressed = false;
$ReadMore = false;
$PacketChecksum = null;

do
{
$RequestID = $Buffer->GetLong( );

switch( $this->Engine )
{
case SourceQuery::GOLDSOURCE:
{
$PacketCountAndNumber = $Buffer->GetByte( );
$PacketCount = $PacketCountAndNumber & 0xF;
$PacketNumber = $PacketCountAndNumber >> 4;

break;
}
case SourceQuery::SOURCE:
{
$IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
$PacketCount = $Buffer->GetByte( );
$PacketNumber = $Buffer->GetByte( ) + 1;

if( $IsCompressed )
{
$Buffer->GetLong( ); // Split size

$PacketChecksum = $Buffer->GetUnsignedLong( );
}
else
{
$Buffer->GetShort( ); // Split size
}

break;
}
default:
{
throw new SocketException( 'Unknown engine.', SocketException::INVALID_ENGINE );
}
}

$Packets[ $PacketNumber ] = $Buffer->Get( );

$ReadMore = $PacketCount > sizeof( $Packets );
}
while( $ReadMore && $SherlockFunction( $Buffer ) );

$Data = implode( $Packets );

// TODO: Test this
if( $IsCompressed )
{
Expand All @@ -120,22 +120,22 @@ protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) :
{
throw new \RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
}

$Data = bzdecompress( $Data );

if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
{
throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
}
}

$Buffer->Set( substr( $Data, 4 ) );
}
else
{
throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . dechex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
}

return $Buffer;
}
}
Loading

0 comments on commit 951e2c3

Please sign in to comment.