Skip to content
This repository has been archived by the owner on Feb 12, 2020. It is now read-only.

fix the Connection read might cause incomplete data extracting #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/nsqphp/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,32 @@ public function isReadable()
/**
* Read from the socket exactly $len bytes
*
* @param integer $len How many bytes to read
* @param integer $desiredLen How many bytes to read
*
* @return string Binary data
*/
public function read($len)
public function read($desiredLen)
{
$null = NULL;
$read = array($socket = $this->getSocket());
$surplusLen = $desiredLen;
$buffer = $data = '';
while (strlen($data) < $len) {
while (strlen($data) < $desiredLen) {
$readable = stream_select($read, $null, $null, $this->readWriteTimeoutSec, $this->readWriteTimeoutUsec);
if ($readable > 0) {
$buffer = @stream_socket_recvfrom($socket, $len);
$buffer = @stream_socket_recvfrom($socket, $surplusLen);
if ($buffer === FALSE) {
throw new SocketException("Could not read {$len} bytes from {$this->hostname}:{$this->port}");
throw new SocketException("Could not read {$surplusLen} bytes from {$this->hostname}:{$this->port}");
} else if ($buffer == '') {
throw new SocketException("Read 0 bytes from {$this->hostname}:{$this->port}");
}
} else if ($readable === 0) {
throw new SocketException("Timed out reading {$len} bytes from {$this->hostname}:{$this->port} after {$this->readWriteTimeoutSec} seconds and {$this->readWriteTimeoutUsec} microseconds");
throw new SocketException("Timed out reading {$surplusLen} bytes from {$this->hostname}:{$this->port} after {$this->readWriteTimeoutSec} seconds and {$this->readWriteTimeoutUsec} microseconds");
} else {
throw new SocketException("Could not read {$len} bytes from {$this->hostname}:{$this->port}");
throw new SocketException("Could not read {$surplusLen} bytes from {$this->hostname}:{$this->port}");
}
$data .= $buffer;
$len -= strlen($buffer);
$surplusLen -= strlen($buffer);
}
return $data;
}
Expand Down