Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function start(Timer $timer)
 {
     if (!isset($this->timers[$timer])) {
         $handle = \uv_timer_init($this->loopHandle);
         $interval = $timer->getInterval() * self::MILLISEC_PER_SEC;
         \uv_timer_start($handle, $interval, $timer->isPeriodic() ? $interval : 0, $this->callback);
         $this->timers[$timer] = $handle;
         $this->handles[(int) $handle] = $timer;
     }
 }
Esempio n. 2
0
 public function setInterval($callback, $delay)
 {
     $args = func_get_args();
     array_shift($args);
     //skip $callback
     array_shift($args);
     //skip $delay
     $interval_id = uv_timer_init();
     uv_timer_start($interval_id, $delay, $delay, function ($stat) use($callback, $args) {
         call_user_func_array($callback, $args);
     });
     return $interval_id;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 protected function enableRepeatWatcher(TimerWatcher $watcher)
 {
     if ($watcher->event === null) {
         $watcher->event = \uv_timer_init($this->loop);
     }
     $this->timerWatchers[(int) $watcher->event] = $watcher;
     if (\uv_is_active($watcher->event)) {
         \uv_timer_stop($watcher->event);
     }
     $delay = (int) \ceil($watcher->delay * 1000);
     \uv_timer_start($watcher->event, $delay, $delay, $this->repeatCallback);
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function listen(Io $io, float $timeout = 0)
 {
     $resource = $io->getResource();
     $id = (int) $resource;
     if (!isset($this->sockets[$id]) || $io !== $this->sockets[$id]) {
         throw new FreedError();
     }
     // If no poll handle exists for the socket, create one now.
     if (!isset($this->polls[$id])) {
         $this->polls[$id] = \uv_poll_init_socket($this->loopHandle, $resource);
     }
     // Begin polling for events.
     \uv_poll_start($this->polls[$id], $this->type, $this->pollCallback);
     // If a timeout is given, set up a separate timer for cancelling the poll in the future.
     if ($timeout) {
         if (self::MIN_TIMEOUT > $timeout) {
             $timeout = self::MIN_TIMEOUT;
         }
         if (!isset($this->timers[$id])) {
             $timer = \uv_timer_init($this->loopHandle);
             $this->handles[(int) $timer] = $id;
             $this->timers[$id] = $timer;
         }
         $timeout *= self::MILLISEC_PER_SEC;
         \uv_timer_start($this->timers[$id], $timeout, $timeout, $this->timerCallback);
     } elseif (isset($this->timers[$id]) && \uv_is_active($this->timers[$id])) {
         \uv_timer_stop($this->timers[$id]);
     }
 }
Esempio n. 5
0
<?php

$loop = uv_default_loop();
$timer = uv_timer_init();
$i = 0;
uv_timer_start($timer, 1000, 1000, function ($stat) use(&$i, $timer, $loop) {
    echo "count: {$i}" . PHP_EOL;
    $i++;
    if ($i > 3) {
        uv_timer_stop($timer);
        uv_unref($timer);
    }
});
uv_run();
echo "finished";
Esempio n. 6
0
 private function registerTimer(callable $callback, $msDelay, $msInterval, array $options)
 {
     $isRepeating = $msInterval !== -1;
     $watcher = new \StdClass();
     $watcher->id = $watcherId = \spl_object_hash($watcher);
     $watcher->type = $isRepeating ? Watcher::TIMER_REPEAT : Watcher::TIMER_ONCE;
     $watcher->uvHandle = \uv_timer_init($this->loop);
     $watcher->callback = $this->wrapTimerCallback($watcher, $callback);
     $watcher->cbData = @$options["cb_data"];
     $watcher->isEnabled = isset($options["enable"]) ? (bool) $options["enable"] : true;
     $watcher->keepAlive = isset($options["keep_alive"]) ? (bool) $options["keep_alive"] : true;
     $this->keepAliveCount += $watcher->isEnabled && $watcher->keepAlive;
     if (empty($watcher->keepAlive)) {
         \uv_unref($watcher->uvHandle);
     }
     $watcher->msDelay = $msDelay;
     $watcher->msInterval = $isRepeating ? $msInterval : 0;
     $this->watchers[$watcherId] = $watcher;
     if ($watcher->isEnabled) {
         \uv_timer_start($watcher->uvHandle, $watcher->msDelay, $watcher->msInterval, $watcher->callback);
     }
     return $watcherId;
 }