Skip to content

Commit c12ae4d

Browse files
authored
Merge pull request repejota#61 from byrnedo/develop
Fix corrupt reads.
2 parents cbdff6b + 851d881 commit c12ae4d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/Connection.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Connection
1616
*/
1717
private $pings = 0;
1818

19+
/**
20+
* Chunk size in bytes to use when reading with fread.
21+
* @var int
22+
*/
23+
private $chunkSize = 8192;
24+
1925
/**
2026
* Return the number of pings.
2127
*
@@ -176,7 +182,21 @@ private function receive($len = null)
176182
{
177183

178184
if ($len) {
179-
$line = fread($this->streamSocket, $len);
185+
$chunkSize = $this->chunkSize;
186+
$line = null;
187+
$receivedBytes = 0;
188+
while ($receivedBytes < $len) {
189+
$bytesLeft = $len - $receivedBytes;
190+
if ( $bytesLeft < 1500 ) {
191+
$chunkSize = $bytesLeft;
192+
}
193+
194+
$line .= fread($this->streamSocket, $chunkSize);
195+
$receivedBytes += $chunkSize;
196+
}
197+
if (strlen($line) > 2) {
198+
$line = substr($line, 0, -2);
199+
}
180200
} else {
181201
$line = fgets($this->streamSocket);
182202
}
@@ -467,6 +487,13 @@ public function reconnect()
467487
$this->connect($this->timeout);
468488
}
469489

490+
/**
491+
* @param integer $chunkSize Set byte chunk len to read when reading from wire
492+
*/
493+
public function setChunkSize($chunkSize){
494+
$this->chunkSize = $chunkSize;
495+
}
496+
470497
/**
471498
* Close will close the connection to the server.
472499
*

0 commit comments

Comments
 (0)