저자: Vasily Zorin (maintainer@daemon.io)
상속: extends PHPDaemon\Network\Client
 /**
  * Constructor.
  * @return void
  */
 public function init()
 {
     $req = $this;
     $job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use($req) {
         // called when job is done
         $req->wakeup();
         // wake up the request immediately
     });
     $job('query', function ($name, $job) {
         // registering job named 'showvar'
         \PHPDaemon\Clients\DNS\Pool::getInstance()->get('phpdaemon.net', function ($response) use($name, $job) {
             $job->setResult($name, $response);
         });
     });
     $job('resolve', function ($name, $job) {
         // registering job named 'showvar'
         \PHPDaemon\Clients\DNS\Pool::getInstance()->resolve('phpdaemon.net', function ($ip) use($name, $job) {
             $job->setResult($name, ['phpdaemon.net resolved to' => $ip]);
         });
     });
     $job();
     // let the fun begin
     $this->sleep(5, true);
     // setting timeout
 }
예제 #2
0
파일: Connection.php 프로젝트: zenus/phpinx
 /**
  * Establish TCP connection
  * @param  string  $host Hostname
  * @param  integer $port Port
  * @return boolean       Success
  */
 public function connectTcp($host, $port)
 {
     $this->type = 'tcp';
     $pton = @inet_pton($host);
     $fd = null;
     if ($pton === false) {
         // dirty check
         \PHPDaemon\Clients\DNS\Pool::getInstance()->resolve($this->host, function ($result) use($host, $port) {
             if (!$result) {
                 Daemon::log(get_class($this) . '->connectTcp : unable to resolve hostname: ' . $host);
                 $this->onStateEv($this->bev, \EventBufferEvent::ERROR);
                 return;
             }
             // @todo stack of addrs
             if (is_array($result)) {
                 if (!sizeof($result)) {
                     return;
                 }
                 srand(Daemon::$process->getPid());
                 $real = $result[rand(0, sizeof($result) - 1)];
                 srand();
             } else {
                 $real = $result;
             }
             $this->connectTcp($real, $port);
         });
         return true;
     }
     $this->hostReal = $host;
     if ($this->host === null) {
         $this->host = $this->hostReal;
     }
     // TCP
     $l = strlen($pton);
     if ($l === 4) {
         $this->addr = $host . ':' . $port;
         if (!$this->bevConnectEnabled) {
             $fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
         }
     } elseif ($l === 16) {
         $this->addr = '[' . $host . ']:' . $port;
         if (!$this->bevConnectEnabled) {
             $fd = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
         }
     } else {
         return false;
     }
     if (!$this->bevConnectEnabled && !$fd) {
         return false;
     }
     if (!$this->bevConnectEnabled) {
         socket_set_nonblock($fd);
     }
     if (!$this->bevConnectEnabled) {
         $this->fd = $fd;
         $this->setTimeouts($this->timeoutRead !== null ? $this->timeoutRead : $this->timeout, $this->timeoutWrite !== null ? $this->timeoutWrite : $this->timeout);
         socket_connect($fd, $host, $port);
         socket_getsockname($fd, $this->locAddr, $this->locPort);
     } else {
         $this->bevConnect = true;
     }
     $this->setFd($fd);
     if (!$this->bev) {
         return false;
     }
     return true;
 }