3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Simple static helpers for network operations
20 class CRM_Utils_Network
{
23 * Try connecting to a TCP service; if it fails, retry. Repeat until serverStartupTimeOut elapses.
27 * @param int $serverStartupTimeOut
29 * @param float $interval
30 * Seconds to wait in between pollings.
33 * TRUE if service is online
35 public static function waitForServiceStartup($host, $port, $serverStartupTimeOut, $interval = 0.333) {
37 $end = $start +
$serverStartupTimeOut;
39 $interval_usec = (int) 1000000 * $interval;
41 while (!$found && $end >= time()) {
42 $found = self
::checkService($host, $port, $end - time());
46 usleep($interval_usec);
52 * Check whether a TCP service is available on $host and $port.
56 * @param string $serverConnectionTimeOut
60 public static function checkService($host, $port, $serverConnectionTimeOut) {
61 $old_error_reporting = error_reporting();
62 error_reporting($old_error_reporting & ~E_WARNING
);
64 $fh = fsockopen($host, $port, $errno, $errstr, $serverConnectionTimeOut);
67 error_reporting($old_error_reporting);
71 catch (Exception
$e) {
73 error_reporting($old_error_reporting);