diff --git a/DnodeSyncClient.php b/DnodeSyncClient.php index 74ce49d..219d5e9 100644 --- a/DnodeSyncClient.php +++ b/DnodeSyncClient.php @@ -31,6 +31,11 @@ class MethodNotExistsException extends Exception {} */ class ConnectionClosedException extends Exception {} +/** + * Thrown when calling method on closed connection + */ +class ConnectionTimeoutException extends Exception {} + /** * Main Dnode client class * @@ -43,19 +48,20 @@ class Dnode { * * @param string $host * @param string $port - * @param float $connectTimeout Number of seconds until `connect()` should timeout. + * @param bool|float $connectTimeout Number of seconds until `connect()` should timeout. * Default: `ini_get("default_socket_timeout")` * - * @return \DnodeSyncClient\Connection - * - * @throws \DnodeSyncClient\IOException - * @throws \DnodeSyncClient\ProtocolException + * @param bool $timeout + * @return Connection */ - public function connect($host, $port, $connectTimeout = false) { + public function connect($host, $port, $connectTimeout = false, $timeout = false) { $address = "tcp://$host:$port"; $stream = $connectTimeout ? @\stream_socket_client($address, $error, $errorMessage, $connectTimeout) : @\stream_socket_client($address, $error, $errorMessage); + if ($timeout) { + stream_set_timeout($stream, $timeout); + } if (!$stream) { throw new IOException("Can't create socket to $address. Error: $error $errorMessage"); } @@ -94,6 +100,10 @@ public function __construct($stream) { // write our (empty) methods description fputs($this->stream, json_encode(array("method" => "methods")) ."\n"); + $streamMeta = stream_get_meta_data($stream); + if ($streamMeta['timed_out'] === true) { + throw new ConnectionTimeoutException("Connection timed out"); + } // read remote methods $line = fgets($this->stream); if ($line === false) { diff --git a/composer.json b/composer.json index 2cf2df5..7a74294 100644 --- a/composer.json +++ b/composer.json @@ -14,5 +14,9 @@ "require": { "php": ">=5.3.0" }, - "autoload": {} + "autoload": { + "psr-4": { + "DnodeSyncClient\\": "" + } + } }