Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions DnodeSyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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");
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"require": {
"php": ">=5.3.0"
},
"autoload": {}
"autoload": {
"psr-4": {
"DnodeSyncClient\\": ""
}
}
}