Skip to content

Commit

Permalink
Merge pull request #79 from dfeyer/bugfix-timeout
Browse files Browse the repository at this point in the history
BUGFIX: Connection::setStreamTimeout must support milliseconds
  • Loading branch information
repejota committed Jul 19, 2016
2 parents 883311c + 8a3986a commit 14078bc
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/Nats/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,12 @@ private function receive($len = null)
* @return resource
* @throws \Exception Exception raised if connection fails.
*/
private function getStream($address, $timeout = null)
private function getStream($address)
{
if (is_null($timeout)) {
$timeout = intval(ini_get('default_socket_timeout'));
}
$errno = null;
$errstr = null;

$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
$timeout = number_format($timeout, 3);
$seconds = floor($timeout);
$microseconds = ($timeout - $seconds) * 1000;
stream_set_timeout($fp, $seconds, $microseconds);
$fp = stream_socket_client($address, $errno, $errstr, null, STREAM_CLIENT_CONNECT);

if (!$fp) {
throw new \Exception($errstr, $errno);
Expand Down Expand Up @@ -231,9 +224,13 @@ public function isConnected()
*/
public function connect($timeout = null)
{
if ($timeout === null) {
$timeout = intval(ini_get('default_socket_timeout'));
}

$this->timeout = $timeout;
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
$this->streamSocket = $this->getStream($this->options->getAddress());
$this->setStreamTimeout($timeout);

$msg = 'CONNECT '.$this->options;
$this->send($msg);
Expand Down Expand Up @@ -447,9 +444,12 @@ public function wait($quantity = 0)
public function setStreamTimeout($seconds)
{
if ($this->isConnected()) {
if (is_int($seconds)) {
if (is_numeric($seconds)) {
try {
return stream_set_timeout($this->streamSocket, $seconds);
$timeout = number_format($seconds, 3);
$seconds = floor($timeout);
$microseconds = ($timeout - $seconds) * 1000;
return stream_set_timeout($this->streamSocket, $seconds, $microseconds);
} catch (\Exception $e) {
return false;
}
Expand Down

0 comments on commit 14078bc

Please sign in to comment.