From db78ce290afb4918cf60bfc9204332cac7632a3c Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 21 Aug 2018 13:56:00 +0300 Subject: [PATCH] read loop simplification --- src/Client.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/Client.php b/src/Client.php index 52a0c0d..8e00d5e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -174,29 +174,17 @@ class Client implements Interfaces\ClientInterface // Read length of line $length = $this->getLength($byte); - // By default line is empty - $line = ''; - - // If we have got more characters to read, read them in. - if ($length > 0) { - $line = ''; - $retLen = 0; - while ($retLen < $length) { - $toRead = $length - $retLen; - $line .= fread($this->_socket, $toRead); - $retLen = \strlen($line); - } - $response[] = $line; - } + // Save output line to response array + $response[] = stream_get_contents($this->_socket, $length); - // If we get a !done, change state of done variable - $done = ($line === '!done'); + // If we get a !done line in response, change state of $isDone variable + $isDone = ('!done' === end($response)); // Get status about latest operation $status = stream_get_meta_data($this->_socket); // If we do not have unread bytes from socket or <-same and if done, then exit from loop - if ((!$status['unread_bytes']) || (!$status['unread_bytes'] && $done)) { + if ((!$status['unread_bytes']) || (!$status['unread_bytes'] && $isDone)) { break; } }