Beispiel #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;
     }
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 protected function enableSignalWatcher(SignalWatcher $watcher)
 {
     if ($watcher->event === null) {
         $watcher->event = $this->loop->signal($watcher->signo, $this->signalCallback, $watcher);
     } else {
         $watcher->event->start();
     }
 }
Beispiel #3
0
 /**
  * @param bool $enableSignals True to enable signal handling, false to disable.
  *
  * @return \Icicle\Loop\Loop
  *
  * @codeCoverageIgnore
  */
 function create(bool $enableSignals = true) : Loop
 {
     if (UvLoop::enabled()) {
         return new UvLoop($enableSignals);
     }
     if (EvLoop::enabled()) {
         return new EvLoop($enableSignals);
     }
     return new SelectLoop($enableSignals);
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $this->running = true;
     while ($this->running) {
         $this->nextTickQueue->tick();
         $this->futureTickQueue->tick();
         $flags = \Ev::RUN_ONCE;
         if (!$this->running || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) {
             $flags |= \Ev::RUN_NOWAIT;
         } elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
             break;
         }
         $this->loop->run($flags);
     }
 }
 /**
  * {@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);
         }
     }
 }
Beispiel #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);
         }
     }
 }
Beispiel #7
0
 function start()
 {
     //$this->deamonlize();
     cy_i_drop(CY_TYPE_SYS);
     cy_i_init(CY_TYPE_SYS);
     cy_i_set('bps_srv_status', CY_TYPE_SYS, 0);
     $pid = posix_getpid();
     $param = [];
     $name = '';
     foreach ($_ENV['server'] as $type => $arr) {
         if (empty($pid)) {
             break;
         }
         if (empty($arr['enable'])) {
             continue;
         }
         $name = $type;
         $param = $arr;
         switch ($pid = pcntl_fork()) {
             case -1:
                 cy_log(CYE_ERROR, "pcntl_fork({$key}) error.");
                 exit(-1);
             case 0:
                 $this->type = $type;
                 break;
             default:
                 break;
         }
     }
     if ($pid || empty($name) || empty($param)) {
         exit(0);
     }
     $ptname = "CY_Srv_Protocol_" . $name;
     if (!class_exists($ptname)) {
         exit("unkown server implements " . $ptname . "\n");
     }
     $wkname = 'CY_Srv_' . $param['worker'];
     if (!class_exists($wkname)) {
         exit("unkown server implements " . $wkname . "\n");
     }
     $worker = new $wkname($param['listen'], [new $ptname(), 'run'], $name);
     $this->servers = ['stat' => ['number' => 0, 'status' => 0], 'list' => [], 'worker' => $worker];
     cy_i_set('bps_srv_' . $name . '_num', CY_TYPE_SYS, 0);
     cy_i_set('bps_srv_' . $name . '_req_num', CY_TYPE_SYS, 0);
     $this->flag = WKST_RUNNING;
     $this->loop = EvLoop::defaultLoop();
     $this->worker_start();
     // master process is here.
     //cy_title($this->cmd." [master]");
     // check dead lock. check every 10 second.
     $this->watcher['ta'] = $this->loop->timer(0, 2, [$this, 'master_timer']);
     // master loop here.
     $this->loop->run();
 }
Beispiel #8
0
 /**
  * Initialise the current process state and watchers.
  */
 public function init()
 {
     // Define current process environment.
     $this->pid = posix_getpid();
     $this->loop = \EvLoop::defaultLoop();
 }