Author: Zorin Vasily (maintainer@daemon.io)
Inheritance: implements ArrayAccess, use trait PHPDaemon\Traits\ClassWatchdog
Example #1
0
 /**
  * Bind given socket
  * @param  string  $uri Address to bind
  * @return boolean      Success
  */
 public function bindSocket($uri)
 {
     $u = \PHPDaemon\Config\Object::parseCfgUri($uri);
     $scheme = $u['scheme'];
     if ($scheme === 'unix') {
         $socket = new \PHPDaemon\BoundSocket\UNIX($u);
     } elseif ($scheme === 'udp') {
         $socket = new \PHPDaemon\BoundSocket\UDP($u);
         if (isset($this->config->port->value)) {
             $socket->setDefaultPort($this->config->port->value);
         }
     } elseif ($scheme === 'tcp') {
         $socket = new \PHPDaemon\BoundSocket\TCP($u);
         if (isset($this->config->port->value)) {
             $socket->setDefaultPort($this->config->port->value);
         }
     } else {
         Daemon::log(get_class($this) . ': enable to bind \'' . $uri . '\': scheme \'' . $scheme . '\' is not supported');
         return false;
     }
     $socket->attachTo($this);
     if ($socket->bindSocket()) {
         if ($this->enabled) {
             $socket->enable();
         }
         return true;
     }
     return false;
 }
 /**
  * Load config-file
  * @param string $paths Path
  * @return boolean - Success.
  */
 public static function loadConfig($paths)
 {
     $apaths = explode(';', $paths);
     foreach ($apaths as $path) {
         if (is_file($p = realpath($path))) {
             $ext = strtolower(pathinfo($p, PATHINFO_EXTENSION));
             if ($ext === 'conf') {
                 return Daemon::$config->loadFile($p);
             } else {
                 Daemon::log('Config file \'' . $p . '\' has unsupported file extension.');
                 return false;
             }
         }
     }
     Daemon::log('Config file not found in \'' . $paths . '\'., dir: ' . __DIR__);
     return false;
 }
Example #3
0
 /**
  * Constructor
  * @param string URI
  * @return object
  */
 public function __construct($uri)
 {
     $this->uri = is_array($uri) ? $uri : \PHPDaemon\Config\Object::parseCfgUri($uri);
     if (!$this->uri) {
         return;
     }
     $this->importParams();
     if ($this->ssl || $this->tls) {
         $this->initSecureContext();
     }
 }
Example #4
0
 /**
  * Connects to URL
  * @param  string   $url URL
  * @param  callable $cb  Callback
  * @return boolean       Success
  */
 public function connect($url, $cb = null)
 {
     $this->uri = Config\Object::parseCfgUri($url);
     $u =& $this->uri;
     if (!$u) {
         return false;
     }
     $this->importParams();
     if (!isset($u['port'])) {
         if ($this->ssl) {
             if (isset($this->pool->config->sslport->value)) {
                 $u['port'] = $this->pool->config->sslport->value;
             }
         } else {
             if (isset($this->pool->config->port->value)) {
                 $u['port'] = $this->pool->config->port->value;
             }
         }
     }
     if (isset($u['user'])) {
         $this->user = $u['user'];
     }
     if ($this->ssl) {
         $this->setContext($this->initSSLContext(), \EventBufferEvent::SSL_CONNECTING);
     }
     $this->url = $url;
     $this->scheme = strtolower($u['scheme']);
     $this->host = isset($u['host']) ? $u['host'] : null;
     $this->port = isset($u['port']) ? $u['port'] : 0;
     if (isset($u['pass'])) {
         $this->password = $u['pass'];
     }
     if (isset($u['path'])) {
         $this->path = ltrim($u['path'], '/');
     }
     if ($cb !== null) {
         $this->onConnected($cb);
     }
     if ($this->scheme === 'unix') {
         return $this->connectUnix($u['path']);
     }
     if ($this->scheme === 'raw') {
         return $this->connectRaw($u['host']);
     }
     if ($this->scheme === 'udp') {
         return $this->connectUdp($this->host, $this->port);
     }
     if ($this->scheme === 'tcp') {
         return $this->connectTcp($this->host, $this->port);
     }
     Daemon::log(get_class($this) . ': connect(): unrecoginized scheme \'' . $this->scheme . '\' (not unix/raw/udp/tcp) in URL: ' . $url);
     return false;
 }