Skip to content

Commit

Permalink
Make the max fread length a const
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Sep 24, 2024
1 parent fd187f0 commit 4b900d3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions SourceQuery/BaseSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function __destruct( )
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( int $Length = 1400 ) : Buffer;
abstract public function Read( ) : Buffer;

protected function ReadInternal( Buffer $Buffer, int $Length, callable $SherlockFunction ) : Buffer
protected function ReadInternal( Buffer $Buffer, callable $SherlockFunction ) : Buffer
{
if( $Buffer->Remaining( ) === 0 )
{
Expand Down Expand Up @@ -106,7 +106,7 @@ protected function ReadInternal( Buffer $Buffer, int $Length, callable $Sherlock

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

$Data = implode( $Packets );

Expand Down
12 changes: 7 additions & 5 deletions SourceQuery/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function Write( int $Header, string $String = '' ) : bool

return $Length === fwrite( $this->Socket, $Command, $Length );
}

private const MaxPacketLength = 1 << 16;

/**
* Reads from socket and returns Buffer.
Expand All @@ -69,19 +71,19 @@ public function Write( int $Header, string $String = '' ) : bool
*
* @return Buffer Buffer
*/
public function Read( int $Length = 1400 ) : Buffer
public function Read( ) : Buffer
{
$Buffer = new Buffer( );
$Buffer->Set( fread( $this->Socket, $Length ) );
$Buffer->Set( fread( $this->Socket, self::MaxPacketLength ) );

$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
$this->ReadInternal( $Buffer, [ $this, 'Sherlock' ] );

return $Buffer;
}

public function Sherlock( Buffer $Buffer, int $Length ) : bool
public function Sherlock( Buffer $Buffer ) : bool
{
$Data = fread( $this->Socket, $Length );
$Data = fread( $this->Socket, self::MaxPacketLength );

if( strlen( $Data ) < 4 )
{
Expand Down
5 changes: 2 additions & 3 deletions SourceQuery/SourceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ public function GetPlayers( ) : array
$this->GetChallenge( self::A2S_PLAYER, self::S2A_PLAYER );

$this->Socket->Write( self::A2S_PLAYER, $this->Challenge );
$Buffer = $this->Socket->Read( 14000 ); // Arma 3 developers do not split their packets, so we have to read more data
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
$Buffer = $this->Socket->Read( );

$Type = $Buffer->GetByte( );

Expand Down Expand Up @@ -429,7 +428,7 @@ public function GetRules( ) : array
$this->GetChallenge( self::A2S_RULES, self::S2A_RULES );

$this->Socket->Write( self::A2S_RULES, $this->Challenge );
$Buffer = $this->Socket->Read( 14000 ); // fix Rust long desc
$Buffer = $this->Socket->Read( );

$Type = $Buffer->GetByte( );

Expand Down
6 changes: 3 additions & 3 deletions Tests/Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public function Write( int $Header, string $String = '' ) : bool
return true;
}

public function Read( int $Length = 1400 ) : Buffer
public function Read( ) : Buffer
{
$Buffer = new Buffer( );
$Buffer->Set( $this->PacketQueue->shift() );

$this->ReadInternal( $Buffer, $Length, [ $this, 'Sherlock' ] );
$this->ReadInternal( $Buffer, [ $this, 'Sherlock' ] );

return $Buffer;
}

public function Sherlock( Buffer $Buffer, int $Length ) : bool
public function Sherlock( Buffer $Buffer ) : bool
{
if( $this->PacketQueue->isEmpty() )
{
Expand Down

0 comments on commit 4b900d3

Please sign in to comment.