Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit b0eac48

Browse files
committed
Fix corrupt reads.
Now continuously reads from stream in chunks until whole message is received
1 parent fab933e commit b0eac48

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/Connection.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class Connection
1313
*/
1414
private $pings = 0;
1515

16+
/**
17+
* Chunk size in bytes to use when reading with fread.
18+
* @var int
19+
*/
20+
private $chunkSize = 8192;
21+
1622
/**
1723
* Return the number of pings.
1824
*
@@ -159,7 +165,18 @@ private function receive($len = null)
159165
{
160166

161167
if ($len) {
162-
$line = fread($this->streamSocket, $len);
168+
$chunkSize = $this->chunkSize;
169+
$line = null;
170+
$receivedBytes = 0;
171+
while ($receivedBytes < $len) {
172+
$bytesLeft = $len - $receivedBytes;
173+
if ( $bytesLeft < 1500 ) {
174+
$chunkSize = $bytesLeft;
175+
}
176+
177+
$line .= fread($this->streamSocket, $chunkSize);
178+
$receivedBytes += $chunkSize;
179+
}
163180
} else {
164181
$line = fgets($this->streamSocket);
165182
}
@@ -425,6 +442,13 @@ public function reconnect()
425442
$this->connect();
426443
}
427444

445+
/**
446+
* @param integer $chunkSize Set byte chunk len to read when reading from wire
447+
*/
448+
public function setChunkSize($chunkSize){
449+
$this->chunkSize = $chunkSize;
450+
}
451+
428452
/**
429453
* Close will close the connection to the server.
430454
*

0 commit comments

Comments
 (0)