| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- /**
- * This file is part of workerman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Workerman\Events;
- use Throwable;
- use Workerman\Worker;
- /**
- * select eventloop
- */
- class Select implements EventInterface
- {
- /**
- * All listeners for read/write event.
- *
- * @var array
- */
- protected $readEvents = [];
- /**
- * All listeners for read/write event.
- *
- * @var array
- */
- protected $writeEvents = [];
- /**
- * @var array
- */
- protected $exceptEvents = [];
- /**
- * Event listeners of signal.
- *
- * @var array
- */
- protected $signalEvents = [];
- /**
- * Fds waiting for read event.
- *
- * @var array
- */
- protected $readFds = [];
- /**
- * Fds waiting for write event.
- *
- * @var array
- */
- protected $writeFds = [];
- /**
- * Fds waiting for except event.
- *
- * @var array
- */
- protected $exceptFds = [];
- /**
- * Timer scheduler.
- * {['data':timer_id, 'priority':run_timestamp], ..}
- *
- * @var \SplPriorityQueue
- */
- protected $scheduler = null;
- /**
- * All timer event listeners.
- * [[func, args, flag, timer_interval], ..]
- *
- * @var array
- */
- protected $eventTimer = [];
- /**
- * Timer id.
- *
- * @var int
- */
- protected $timerId = 1;
- /**
- * Select timeout.
- *
- * @var int
- */
- protected $selectTimeout = 100000000;
- /**
- * Construct.
- */
- public function __construct()
- {
- // Init SplPriorityQueue.
- $this->scheduler = new \SplPriorityQueue();
- $this->scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
- }
- /**
- * {@inheritdoc}
- */
- public function delay(float $delay, $func, $args)
- {
- $timerId = $this->timerId++;
- $runTime = \microtime(true) + $delay;
- $this->scheduler->insert($timerId, -$runTime);
- $this->eventTimer[$timerId] = [$func, (array)$args];
- $selectTimeout = ($runTime - \microtime(true)) * 1000000;
- $selectTimeout = $selectTimeout <= 0 ? 1 : (int)$selectTimeout;
- if ($this->selectTimeout > $selectTimeout) {
- $this->selectTimeout = $selectTimeout;
- }
- return $timerId;
- }
- /**
- * {@inheritdoc}
- */
- public function repeat(float $delay, $func, $args)
- {
- $timerId = $this->timerId++;
- $runTime = \microtime(true) + $delay;
- $this->scheduler->insert($timerId, -$runTime);
- $this->eventTimer[$timerId] = [$func, (array)$args, $delay];
- $selectTimeout = ($runTime - \microtime(true)) * 1000000;
- $selectTimeout = $selectTimeout <= 0 ? 1 : (int)$selectTimeout;
- if ($this->selectTimeout > $selectTimeout) {
- $this->selectTimeout = $selectTimeout;
- }
- return $timerId;
- }
- /**
- * {@inheritdoc}
- */
- public function offDelay($timerId)
- {
- if (isset($this->eventTimer[$timerId])) {
- unset($this->eventTimer[$timerId]);
- return true;
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function offRepeat($timerId)
- {
- return $this->offDelay($timerId);
- }
- /**
- * {@inheritdoc}
- */
- public function onReadable($stream, $func)
- {
- $count = \count($this->readFds);
- if ($count >= 1024) {
- echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
- } else if (\DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
- echo "Warning: system call select exceeded the maximum number of connections 256.\n";
- }
- $fdKey = (int)$stream;
- $this->readEvents[$fdKey] = $func;
- $this->readFds[$fdKey] = $stream;
- }
- /**
- * {@inheritdoc}
- */
- public function offReadable($stream)
- {
- $fdKey = (int)$stream;
- unset($this->readEvents[$fdKey], $this->readFds[$fdKey]);
- }
- /**
- * {@inheritdoc}
- */
- public function onWritable($stream, $func)
- {
- $count = \count($this->writeFds);
- if ($count >= 1024) {
- echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
- } else if (\DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
- echo "Warning: system call select exceeded the maximum number of connections 256.\n";
- }
- $fdKey = (int)$stream;
- $this->writeEvents[$fdKey] = $func;
- $this->writeFds[$fdKey] = $stream;
- }
- /**
- * {@inheritdoc}
- */
- public function offWritable($stream)
- {
- $fdKey = (int)$stream;
- unset($this->writeEvents[$fdKey], $this->writeFds[$fdKey]);
- }
- /**
- * {@inheritdoc}
- */
- public function onExcept($stream, $func)
- {
- $fdKey = (int)$stream;
- $this->exceptEvents[$fdKey] = $func;
- $this->exceptFds[$fdKey] = $stream;
- }
- /**
- * {@inheritdoc}
- */
- public function offExcept($stream)
- {
- $fdKey = (int)$stream;
- unset($this->exceptEvents[$fdKey], $this->exceptFds[$fdKey]);
- }
- /**
- * {@inheritdoc}
- */
- public function onSignal($signal, $func)
- {
- if (\DIRECTORY_SEPARATOR !== '/') {
- return null;
- }
- $this->signalEvents[$signal] = $func;
- \pcntl_signal($signal, [$this, 'signalHandler']);
- }
- /**
- * {@inheritdoc}
- */
- public function offsignal($signal)
- {
- unset($this->signalEvents[$signal]);
- \pcntl_signal($signal, SIG_IGN);
- }
- /**
- * Signal handler.
- *
- * @param int $signal
- */
- public function signalHandler($signal)
- {
- $this->signalEvents[$signal]($signal);
- }
- /**
- * Tick for timer.
- *
- * @return void
- */
- protected function tick()
- {
- $tasksToInsert = [];
- while (!$this->scheduler->isEmpty()) {
- $schedulerData = $this->scheduler->top();
- $timerId = $schedulerData['data'];
- $nextRunTime = -$schedulerData['priority'];
- $timeNow = \microtime(true);
- $this->selectTimeout = (int)(($nextRunTime - $timeNow) * 1000000);
- if ($this->selectTimeout <= 0) {
- $this->scheduler->extract();
- if (!isset($this->eventTimer[$timerId])) {
- continue;
- }
- // [func, args, timer_interval]
- $taskData = $this->eventTimer[$timerId];
- if (isset($taskData[2])) {
- $nextRunTime = $timeNow + $taskData[2];
- $tasksToInsert[] = [$timerId, -$nextRunTime];
- } else {
- unset($this->eventTimer[$timerId]);
- }
- try {
- $taskData[0](...$taskData[1]);
- } catch (Throwable $e) {
- Worker::stopAll(250, $e);
- }
- } else {
- break;
- }
- }
- foreach ($tasksToInsert as $item) {
- $this->scheduler->insert($item[0], $item[1]);
- }
- if (!$this->scheduler->isEmpty()) {
- $schedulerData = $this->scheduler->top();
- $nextRunTime = -$schedulerData['priority'];
- $timeNow = \microtime(true);
- $this->selectTimeout = \max((int)(($nextRunTime - $timeNow) * 1000000), 0);
- return;
- }
- $this->selectTimeout = 100000000;
- }
- /**
- * {@inheritdoc}
- */
- public function deleteAllTimer()
- {
- $this->scheduler = new \SplPriorityQueue();
- $this->scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
- $this->eventTimer = [];
- }
- /**
- * {@inheritdoc}
- */
- public function run()
- {
- while (1) {
- if (\DIRECTORY_SEPARATOR === '/') {
- // Calls signal handlers for pending signals
- \pcntl_signal_dispatch();
- }
- $read = $this->readFds;
- $write = $this->writeFds;
- $except = $this->exceptFds;
- if ($read || $write || $except) {
- // Waiting read/write/signal/timeout events.
- try {
- @stream_select($read, $write, $except, 0, $this->selectTimeout);
- } catch (Throwable $e) {
- }
- } else {
- $this->selectTimeout >= 1 && usleep($this->selectTimeout);
- }
- if (!$this->scheduler->isEmpty()) {
- $this->tick();
- }
- foreach ($read as $fd) {
- $fdKey = (int)$fd;
- if (isset($this->readEvents[$fdKey])) {
- $this->readEvents[$fdKey]($fd);
- }
- }
- foreach ($write as $fd) {
- $fdKey = (int)$fd;
- if (isset($this->writeEvents[$fdKey])) {
- $this->writeEvents[$fdKey]($fd);
- }
- }
- foreach ($except as $fd) {
- $fdKey = (int)$fd;
- if (isset($this->exceptEvents[$fdKey])) {
- $this->exceptEvents[$fdKey]($fd);
- }
- }
- }
- }
- /**
- * {@inheritdoc}
- */
- public function stop()
- {
- $this->deleteAllTimer();
- foreach ($this->signalEvents as $signal => $item) {
- $this->offsignal($signal);
- }
- $this->readFds = $this->writeFds = $this->exceptFds = $this->readEvents
- = $this->writeEvents = $this->exceptEvents = $this->signalEvents = [];
- }
- /**
- * {@inheritdoc}
- */
- public function getTimerCount()
- {
- return \count($this->eventTimer);
- }
- }
|