Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 3793581

Browse files
committed
allow for a configurable timeout at getStream method
1 parent 88ff28a commit 3793581

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/Connection.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,18 @@ private function receive($len = null)
148148
*
149149
* @param string $address Server url string.
150150
*
151+
* @param null $timeout
151152
* @return resource
153+
* @throws \Exception
152154
*/
153-
private function getStream($address)
155+
private function getStream($address, $timeout = null)
154156
{
155-
$fp = stream_socket_client($address, $errno, $errstr, STREAM_CLIENT_CONNECT);
157+
if (is_null($timeout)) {
158+
$timeout = intval(ini_get('default_socket_timeout'));
159+
}
160+
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
156161
if (!$fp) {
157-
echo '!!!!!!! '.$errstr.' - '.$errno;
162+
throw new \Exception($errstr, $errno);
158163
}
159164
//stream_set_blocking($fp, 0);
160165
return $fp;
@@ -173,11 +178,12 @@ public function isConnected()
173178
/**
174179
* Connect to server.
175180
*
176-
* @return void
181+
* @param null $timeout
182+
* @throws \Exception
177183
*/
178-
public function connect()
184+
public function connect($timeout = null)
179185
{
180-
$this->streamSocket = $this->getStream($this->options->getAddress());
186+
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
181187
$msg = 'CONNECT '.$this->options;
182188
$this->send($msg);
183189
}
@@ -268,7 +274,7 @@ private function handleMSG($line)
268274
} else {
269275
return new \Exception('not callable');
270276
}
271-
277+
272278
return;
273279
}
274280

0 commit comments

Comments
 (0)