$line = '';\r
while (!feof($this->socket)) {\r
if (null !== $localTimeout) {\r
- stream_set_timeout($this->socket, $localTimeout);\r
+ $timeout = $localTimeout;\r
} elseif ($this->deadline) {\r
- stream_set_timeout($this->socket, max($this->deadline - time(), 1));\r
+ $timeout = max($this->deadline - time(), 1);\r
+ } else {\r
+ // "If tv_sec is NULL stream_select() can block\r
+ // indefinitely, returning only when an event on one of\r
+ // the watched streams occurs (or if a signal interrupts\r
+ // the system call)." - http://php.net/stream_select\r
+ $timeout = null;\r
}\r
\r
- $line .= @fgets($this->socket, $bufferSize);\r
+ $info = stream_get_meta_data($this->socket);\r
+ $old_blocking = (bool)$info['blocked'];\r
+ stream_set_blocking($this->socket, false);\r
+ $r = array($this->socket);\r
+ $w = array();\r
+ $e = array();\r
+ if (stream_select($r, $w, $e, $timeout)) {\r
+ $line .= @fgets($this->socket, $bufferSize);\r
+ }\r
+ stream_set_blocking($this->socket, $old_blocking);\r
\r
if (null === $localTimeout) {\r
$this->checkTimeout();\r