Example #1
0
 private function establish()
 {
     $unix = in_array("unix", \stream_get_transports(), true);
     if ($unix) {
         $promise = \Amp\Socket\connect("unix://{$this->path}.sock");
     } else {
         $promise = \Amp\pipe(\Amp\file\get($this->path), 'Amp\\Socket\\connect');
     }
     $promise->when(function ($e, $sock) {
         if ($e) {
             $this->failAll();
             return;
         }
         $this->sock = $sock;
         $this->writeWatcher = \Amp\onWritable($sock, $this->writer);
     });
 }
Example #2
0
 private function initializeNewConnection(Deferred $promisor, $uri, $options)
 {
     $this->pendingSockets[$uri] = isset($this->pendingSockets[$uri]) ? $this->pendingSockets[$uri] + 1 : 1;
     $futureSocket = \Amp\Socket\connect($uri, $options);
     $futureSocket->when(function ($error, $socket) use($promisor, $uri, $options) {
         if ($error) {
             $promisor->fail($error);
         } else {
             $this->finalizeNewConnection($promisor, $uri, $socket, $options);
         }
     });
 }
Example #3
0
 /**
  * @return Promise
  */
 private function connect()
 {
     if ($this->promisor) {
         return $this->promisor->promise();
     }
     // Already connected
     if (is_resource($this->socket)) {
         return new Success();
     }
     $this->promisor = new \Amp\Deferred();
     /** @var $socketPromise Promise */
     $socketPromise = \Amp\Socket\connect($this->uri, ['timeout' => 1000]);
     $socketPromise->when(function ($error, $socket) {
         $promisor = $this->promisor;
         $this->promisor = null;
         $this->socket = $socket;
         $this->reader = \Amp\onReadable($this->socket, [$this, "onRead"]);
         $this->writer = \Amp\onWritable($this->socket, [$this, "onWrite"]);
         $promisor->succeed();
     });
     return $this->promisor->promise();
 }