From f9844545d1435705efb57e00735eb72eca96a956 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Wed, 21 Oct 2015 13:10:12 +0300 Subject: [PATCH 1/3] add timeout period on stream support --- DnodeSyncClient.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/DnodeSyncClient.php b/DnodeSyncClient.php index 74ce49d..5209948 100644 --- a/DnodeSyncClient.php +++ b/DnodeSyncClient.php @@ -43,19 +43,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"); } From 44ab1445f807f8ed293e4293a8524f435f294451 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Wed, 21 Oct 2015 14:05:15 +0300 Subject: [PATCH 2/3] thrown connection timeout if it's needed --- DnodeSyncClient.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/DnodeSyncClient.php b/DnodeSyncClient.php index 5209948..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 * @@ -95,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) { From c1ff402d2a0536c7919bc95f6e129a6dc3902cc0 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Thu, 22 Oct 2015 14:22:00 +0300 Subject: [PATCH 3/3] add composer autoload --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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\\": "" + } + } }