예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function start(Timer $timer)
 {
     if (!isset($this->timers[$timer])) {
         $interval = $timer->getInterval();
         $event = $this->loop->timer($interval, $timer->isPeriodic() ? $interval : 0, $this->callback, $timer);
         $this->timers[$timer] = $event;
     }
 }
예제 #2
0
파일: EvLoop.php 프로젝트: amphp/loop
 /**
  * {@inheritdoc}
  */
 protected function activate(array $watchers)
 {
     foreach ($watchers as $watcher) {
         if (!isset($this->events[$id = $watcher->id])) {
             switch ($watcher->type) {
                 case Watcher::READABLE:
                     $this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
                     break;
                 case Watcher::WRITABLE:
                     $this->events[$id] = $this->handle->io($watcher->value, \Ev::WRITE, $this->ioCallback, $watcher);
                     break;
                 case Watcher::DELAY:
                 case Watcher::REPEAT:
                     $interval = $watcher->value / self::MILLISEC_PER_SEC;
                     $this->events[$id] = $this->handle->timer($interval, $watcher->type & Watcher::REPEAT ? $interval : 0, $this->timerCallback, $watcher);
                     break;
                 case Watcher::SIGNAL:
                     $this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
                     break;
                 default:
                     throw new \DomainException("Unknown watcher type");
             }
         } else {
             $this->events[$id]->start();
         }
     }
 }
예제 #3
0
파일: EvLoop.php 프로젝트: koolkode/async
 /**
  * {@inheritdoc}
  */
 protected function enableRepeatWatcher(TimerWatcher $watcher)
 {
     if ($watcher->event === null) {
         $watcher->event = $this->loop->timer($watcher->delay, $watcher->delay, $this->repeatCallback, $watcher);
     } else {
         $watcher->event->again();
         $watcher->event->start();
     }
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function addTimer($interval, callable $callback)
 {
     $timer = new Timer($this, $interval, $callback, false);
     $callback = function () use($timer) {
         call_user_func($timer->getCallback(), $timer);
         if ($this->isTimerActive($timer)) {
             $this->cancelTimer($timer);
         }
     };
     $event = $this->loop->timer($timer->getInterval(), 0.0, $callback);
     $this->timerEvents->attach($timer, $event);
     return $timer;
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function listen(SocketEventInterface $socket, float $timeout = 0)
 {
     $id = (int) $socket->getResource();
     if (!isset($this->events[$id]) || $socket !== $this->events[$id]->data) {
         throw new FreedError('Socket event has been freed.');
     }
     $this->events[$id]->start();
     if (0 !== $timeout) {
         if (self::MIN_TIMEOUT > $timeout) {
             $timeout = self::MIN_TIMEOUT;
         }
         if (isset($this->timers[$id])) {
             $this->timers[$id]->set($timeout, 0);
             $this->timers[$id]->start();
         } else {
             $this->timers[$id] = $this->loop->timer($timeout, 0, $this->timerCallback, $socket);
         }
     }
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function listen(Io $io, float $timeout = 0)
 {
     $id = (int) $io->getResource();
     if (!isset($this->events[$id]) || $io !== $this->events[$id]->data) {
         throw new FreedError();
     }
     $this->events[$id]->start();
     if ($timeout) {
         if (self::MIN_TIMEOUT > $timeout) {
             $timeout = self::MIN_TIMEOUT;
         }
         if (isset($this->timers[$id])) {
             $this->timers[$id]->set($timeout, 0);
             $this->timers[$id]->start();
         } else {
             $this->timers[$id] = $this->loop->timer($timeout, 0, $this->timerCallback, $io);
         }
     }
 }