X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/blobdiff_plain/706ce1501a9ab1a33e4106774665c631a3ad3749..60bd8065af4019befdd74bfe7805ec4c74c8a8ec:/src/callnotifier/Source/Remote.php diff --git a/src/callnotifier/Source/Remote.php b/src/callnotifier/Source/Remote.php index 36e468d..be7604f 100644 --- a/src/callnotifier/Source/Remote.php +++ b/src/callnotifier/Source/Remote.php @@ -13,28 +13,42 @@ class Source_Remote public function run() { - $this->connect($this->config->host, $this->config->port); - $this->init(); - $this->loop(); + do { + try { + $tryAgain = false; + $this->connect($this->config->host, $this->config->port); + $this->init(); + $this->loop(); + } catch (Exception_ConnectionReset $e) { + $tryAgain = true; + //connection is refused directly after a connection reset + //hopefully waiting a bit will help + sleep(10); + } + } while ($tryAgain); $this->disconnect(); } public function connect($ip, $port) { + if ($ip == '') { + throw new \Exception('No remote IP or hostname given.'); + } + $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { - echo "socket_create() failed: reason: " - . socket_strerror(socket_last_error()) . "\n"; - } else { - echo "OK.\n"; + throw new \Exception( + 'socket_create() failed: reason: ' + . socket_strerror(socket_last_error()) + ); } - echo "Attempting to connect to '$ip' on port '$port'..."; + //echo "Attempting to connect to '$ip' on port '$port'..."; $result = socket_connect($socket, $ip, $port); if ($result === false) { - echo "socket_connect() failed.\nReason: ($result) " - . socket_strerror(socket_last_error($socket)) . "\n"; - } else { - echo "OK.\n"; + throw new \Exception( + "socket_connect() failed. Reason: " + . socket_strerror(socket_last_error($socket)) + ); } $this->socket = $socket; @@ -60,6 +74,11 @@ class Source_Remote function read_response() { $res = socket_read($this->socket, 2048, PHP_NORMAL_READ); + if ($res === false) { + //handle "Connection reset by peer" that appears nightly since + // version 4.0N + throw new Exception_ConnectionReset(); + } return substr($res, 2, -1); }