protected function _read_reply()
{
$server_reply = fgets($this->connection);
if ($server_reply === false) {
if (!$this->connect($this->host, $this->port)) {
return false;
} else {
$server_reply = fgets($this->connection);
if (empty($server_reply)) {
$this->repeat_reconnected = true;
return false;
}
}
}
$reply = trim($server_reply);
$response = null;
/**
* Thanks to Justin Poliey for original code of parsing the answer
* https://github.com/jdp
* Error was fixed there: https://github.com/jamm/redisent
*/
switch ($reply[0]) {
/* Error reply */
case '-':
$this->reportError('error: ' . $reply);
return false;
/* Inline reply */
/* Inline reply */
case '+':
return substr($reply, 1);
/* Bulk reply */
/* Bulk reply */
case '$':
if ($reply == '$-1') {
return null;
}
$response = null;
$size = intval(substr($reply, 1));
if ($size > 0) {
$response = stream_get_contents($this->connection, $size);
}
fread($this->connection, 2);
/* discard crlf */
break;
/* Multi-bulk reply */
/* Multi-bulk reply */
case '*':
$count = substr($reply, 1);
if ($count == '-1') {
return null;
}
$response = array();
for ($i = 0; $i < $count; $i++) {
$response[] = $this->_read_reply();
}
break;
/* Integer reply */
/* Integer reply */
case ':':
return intval(substr($reply, 1));
break;
default:
$this->reportError('Non-protocol answer: ' . print_r($server_reply, 1));
return false;
}
return $response;
}