diff --git a/src/Connection.php b/src/Connection.php index 219e0dd..0994896 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -13,6 +13,12 @@ class Connection */ private $pings = 0; + /** + * Chunk size in bytes to use when reading with fread. + * @var int + */ + private $chunkSize = 8192; + /** * Return the number of pings. * @@ -159,7 +165,21 @@ private function receive($len = null) { if ($len) { - $line = fread($this->streamSocket, $len); + $chunkSize = $this->chunkSize; + $line = null; + $receivedBytes = 0; + while ($receivedBytes < $len) { + $bytesLeft = $len - $receivedBytes; + if ( $bytesLeft < 1500 ) { + $chunkSize = $bytesLeft; + } + + $line .= fread($this->streamSocket, $chunkSize); + $receivedBytes += $chunkSize; + } + if (strlen($line) > 2) { + $line = substr($line, 0, -2); + } } else { $line = fgets($this->streamSocket); } @@ -425,6 +445,13 @@ public function reconnect() $this->connect(); } + /** + * @param integer $chunkSize Set byte chunk len to read when reading from wire + */ + public function setChunkSize($chunkSize){ + $this->chunkSize = $chunkSize; + } + /** * Close will close the connection to the server. *