Skip to content

Commit 00c5a9f

Browse files
committed
Partial fix for multi-packet source rcon
1 parent 5072999 commit 00c5a9f

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

SourceQuery/SourceRcon.class.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public function Write( $Header, $String = '' )
7575
return $Length === FWrite( $this->RconSocket, $Command, $Length );
7676
}
7777

78-
public function Read( $Length = 1400 )
78+
public function Read( )
7979
{
80-
$this->Buffer->Set( FRead( $this->RconSocket, $Length ) );
80+
$this->Buffer->Set( FRead( $this->RconSocket, 4 ) );
8181

8282
if( $this->Buffer->Remaining( ) < 4 )
8383
{
@@ -86,16 +86,27 @@ public function Read( $Length = 1400 )
8686

8787
$PacketSize = $this->Buffer->GetLong( );
8888

89+
$this->Buffer->Set( FRead( $this->RconSocket, $PacketSize ) );
90+
8991
$Buffer = $this->Buffer->Get( );
9092

9193
$Remaining = $PacketSize - StrLen( $Buffer );
9294

9395
while( $Remaining > 0 )
9496
{
95-
$Buffer2 = FRead( $this->RconSocket, $Length );
96-
$Buffer .= $Buffer2;
97+
$Buffer2 = FRead( $this->RconSocket, $Remaining );
98+
99+
$PacketSize = StrLen( $Buffer2 );
100+
101+
if( $PacketSize === 0 )
102+
{
103+
throw new InvalidPacketException( 'Read ' . strlen( $Buffer ) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY );
104+
105+
break;
106+
}
97107

98-
$Remaining -= StrLen( $Buffer2 );
108+
$Buffer .= $Buffer2;
109+
$Remaining -= $PacketSize;
99110
}
100111

101112
$this->Buffer->Set( $Buffer );
@@ -139,9 +150,16 @@ public function Command( $Command )
139150
break;
140151
}
141152

142-
$Buffer .= $this->Buffer->Get( );
153+
$Buffer2 = $this->Buffer->Get( );
154+
155+
if( $Buffer2 === "\x00\x01\x00\x00\x00\x00" )
156+
{
157+
break;
158+
}
159+
160+
$Buffer .= $Buffer2;
143161
}
144-
while( false ); // TODO: This is so broken that we don't even try to read multiple times, needs to be revised
162+
while( true );
145163
}
146164

147165
// TODO: It should use GetString, but there are no null bytes at the end, why?

0 commit comments

Comments
 (0)