Example #1
0
 /**
  *
  */
 public function close()
 {
     $this->loop->removeReadStream($this->socket);
     if (is_resource($this->socket)) {
         fclose($this->socket);
     }
     $this->notifyCompleted();
 }
Example #2
0
 /**
  * Remove event listener from event loop.
  *
  * @param mixed $fd
  * @param int   $flag
  * @return bool
  */
 public function del($fd, $flag)
 {
     switch ($flag) {
         case EventInterface::EV_READ:
             return $this->_loop->removeReadStream($fd);
         case EventInterface::EV_WRITE:
             return $this->_loop->removeWriteStream($fd);
         case EventInterface::EV_SIGNAL:
             return $this->_loop->removeSignal($fd);
         case EventInterface::EV_TIMER:
         case EventInterface::EV_TIMER_ONCE:
             return $this->_loop->cancelTimer($fd);
     }
     return false;
 }
Example #3
0
 /**
  * Disconnects client from server.
  *
  * Calling disconnect() multiple times or if client is not connected will result in error.
  *
  * @param int $replyCode
  * @param string $replyText
  * @return Promise\PromiseInterface
  */
 public function disconnect($replyCode = 0, $replyText = "")
 {
     if ($this->state !== ClientStateEnum::CONNECTED) {
         return Promise\reject(new ClientException("Client is not connected."));
     }
     $this->state = ClientStateEnum::DISCONNECTING;
     $promises = [];
     if ($replyCode === 0) {
         foreach ($this->channels as $channel) {
             $promises[] = $channel->close();
         }
     }
     if ($this->heartbeatTimer) {
         $this->heartbeatTimer->cancel();
         $this->heartbeatTimer = null;
     }
     return Promise\all($promises)->then(function () use($replyCode, $replyText) {
         return $this->connectionClose($replyCode, $replyText, 0, 0);
     })->then(function () {
         $this->eventLoop->removeReadStream($this->getStream());
         $this->closeStream();
         $this->init();
         return $this;
     });
 }
Example #4
0
 protected function unregister()
 {
     if (!$this->active) {
         return;
     }
     $this->active = false;
     $this->loop->removeReadStream($this->fd, [$this, 'handleEvent']);
 }
Example #5
0
 public function testRemoveInvalid()
 {
     $stream = $this->createStream();
     // remove a valid stream from the event loop that was never added in the first place
     $this->loop->removeReadStream($stream);
     $this->loop->removeWriteStream($stream);
     $this->loop->removeStream($stream);
 }
 /**
  * close the inotifyHandler and clear all pending events (if any)
  */
 public function close()
 {
     if ($this->inotifyHandler !== false) {
         $this->loop->removeReadStream($this->inotifyHandler);
         fclose($this->inotifyHandler);
         $this->inotifyHandler = false;
         $this->watchDescriptors = array();
     }
 }
Example #7
0
 /**
  * Cancel an existing timer/stream watcher
  *
  * @param int $watcherId
  */
 public function cancel($watcherId)
 {
     if (isset($this->watchers[$watcherId])) {
         list($type, $data) = $this->watchers[$watcherId];
         switch ($type) {
             case self::WATCHER_TYPE_READ:
                 $this->reactor->removeReadStream($data);
                 break;
             case self::WATCHER_TYPE_WRITE:
                 $this->reactor->removeWriteStream($data);
                 break;
             case self::WATCHER_TYPE_TIMER:
                 $this->reactor->cancelTimer($data);
                 break;
         }
     }
     unset($this->watchers[$watcherId], $this->disabledWatchers[$watcherId]);
 }
Example #8
0
 public function process($stream, $loop)
 {
     $response = fgets($stream);
     if ($response === false) {
         $this->state = self::STATE_DISCONNECTED;
         $this->onFailure->__invoke($this->record, "Error while reading data");
         echo $this->errno . ': ' . $this->errstr . PHP_EOL;
         return;
     }
     $code = intval(substr($response, 0, 3));
     $sub = intval($response[4] . $response[6] . $response[8]);
     if (($code === 220 || $code === 250) && $sub === 0) {
         return;
     }
     if ($code === 250 && $sub === 210) {
         $this->state = self::STATE_IDLE;
         return;
     }
     if ($this->record === null) {
         throw new LogicException("Email not set.");
     }
     if ($code === 250 && $sub === 215) {
         $this->onSuccess->__invoke($this->record);
         $this->state = self::STATE_IDLE;
     } else {
         if ($code === 452 && $sub === 453) {
             $this->loop->removeReadStream($this->stream);
             fclose($this->stream);
             $this->onFailure->__invoke($this->record, "limit reached");
             $this->state = self::STATE_DISCONNECTED;
         } else {
             $this->onFailure->__invoke($this->record, false);
             while (substr($response, -7, 5) !== 'gsmtp') {
                 $response = fgets($stream);
             }
             $this->state = self::STATE_IDLE;
         }
     }
 }
 /**
  * Remove the read event listener for the given stream.
  *
  * @param stream $stream The PHP stream resource.
  */
 public function removeReadStream($stream)
 {
     $this->emit('removeReadStream', [$stream]);
     $this->loop->removeReadStream($stream);
 }