|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\EventLoop; |
| 4 | + |
| 5 | +use React\EventLoop\Tick\FutureTickQueue; |
| 6 | +use React\EventLoop\Timer\Timer; |
| 7 | +use React\EventLoop\Timer\Timers; |
| 8 | +use SplObjectStorage; |
| 9 | + |
| 10 | +final class IoPollLoop implements LoopInterface |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @internal |
| 14 | + * This is about 22 years, and the max \Time\Duration` takes for seconds |
| 15 | + */ |
| 16 | + const MAX_DURATION_SECONDS = 9_223_372_035; |
| 17 | + |
| 18 | + private $running = false; |
| 19 | + private $context; |
| 20 | + private $futureTickQueue; |
| 21 | + private $timers; |
| 22 | + private $pcntl = false; |
| 23 | + private $pcntlPoll = false; |
| 24 | + private $signals; |
| 25 | + private $watchers = []; |
| 26 | + private $readListeners = []; |
| 27 | + private $writeListeners = []; |
| 28 | + |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + $this->context = new \Io\Poll\Context(); |
| 32 | + $this->futureTickQueue = new FutureTickQueue(); |
| 33 | + $this->timers = new Timers(); |
| 34 | + $this->pcntl = \function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch'); |
| 35 | + $this->pcntlPoll = $this->pcntl && !\function_exists('pcntl_async_signals'); |
| 36 | + $this->signals = new SignalsHandler(); |
| 37 | + |
| 38 | + // prefer async signals if available (PHP 7.1+) or fall back to dispatching on each tick |
| 39 | + if ($this->pcntl && !$this->pcntlPoll) { |
| 40 | + \pcntl_async_signals(true); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public function addReadStream($stream, $listener) |
| 45 | + { |
| 46 | + $key = (int) $stream; |
| 47 | + if (!isset($this->readListeners[$key])) { |
| 48 | + $this->readListeners[$key] = $listener; |
| 49 | + } |
| 50 | + $this->manageStream($key, $stream, \Io\Poll\Event::Read, true); |
| 51 | + } |
| 52 | + |
| 53 | + public function addWriteStream($stream, $listener) |
| 54 | + { |
| 55 | + $key = (int) $stream; |
| 56 | + if (!isset($this->writeListeners[$key])) { |
| 57 | + $this->writeListeners[$key] = $listener; |
| 58 | + } |
| 59 | + $this->manageStream($key, $stream, \Io\Poll\Event::Write, true); |
| 60 | + } |
| 61 | + |
| 62 | + public function removeReadStream($stream) |
| 63 | + { |
| 64 | + $key = (int) $stream; |
| 65 | + unset($this->readListeners[$key]); |
| 66 | + $this->manageStream($key, $stream, \Io\Poll\Event::Read, false); |
| 67 | + } |
| 68 | + |
| 69 | + public function removeWriteStream($stream) |
| 70 | + { |
| 71 | + $key = (int) $stream; |
| 72 | + unset($this->writeListeners[$key]); |
| 73 | + $this->manageStream($key, $stream, \Io\Poll\Event::Write, false); |
| 74 | + } |
| 75 | + |
| 76 | + public function addTimer($interval, $callback) |
| 77 | + { |
| 78 | + $timer = new Timer($interval, $callback, false); |
| 79 | + |
| 80 | + $this->timers->add($timer); |
| 81 | + |
| 82 | + return $timer; |
| 83 | + } |
| 84 | + |
| 85 | + public function addPeriodicTimer($interval, $callback) |
| 86 | + { |
| 87 | + $timer = new Timer($interval, $callback, true); |
| 88 | + |
| 89 | + $this->timers->add($timer); |
| 90 | + |
| 91 | + return $timer; |
| 92 | + } |
| 93 | + |
| 94 | + public function cancelTimer(TimerInterface $timer) |
| 95 | + { |
| 96 | + $this->timers->cancel($timer); |
| 97 | + } |
| 98 | + |
| 99 | + public function futureTick($listener) |
| 100 | + { |
| 101 | + $this->futureTickQueue->add($listener); |
| 102 | + } |
| 103 | + |
| 104 | + public function addSignal($signal, $listener) |
| 105 | + { |
| 106 | + if ($this->pcntl === false) { |
| 107 | + throw new \BadMethodCallException('Event loop feature "signals" isn\'t supported by the "StreamSelectLoop"'); |
| 108 | + } |
| 109 | + |
| 110 | + $first = $this->signals->count($signal) === 0; |
| 111 | + $this->signals->add($signal, $listener); |
| 112 | + |
| 113 | + if ($first) { |
| 114 | + \pcntl_signal($signal, [$this->signals, 'call']); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public function removeSignal($signal, $listener) |
| 119 | + { |
| 120 | + if (!$this->signals->count($signal)) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + $this->signals->remove($signal, $listener); |
| 125 | + |
| 126 | + if ($this->signals->count($signal) === 0) { |
| 127 | + \pcntl_signal($signal, \SIG_DFL); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function run() |
| 132 | + { |
| 133 | + $this->running = true; |
| 134 | + |
| 135 | + while ($this->running) { |
| 136 | + $this->futureTickQueue->tick(); |
| 137 | + |
| 138 | + $this->timers->tick(); |
| 139 | + |
| 140 | + // Future-tick queue has pending callbacks ... |
| 141 | + if (!$this->futureTickQueue->isEmpty()) { |
| 142 | + $timeout = 0; |
| 143 | + |
| 144 | + // There is a pending timer, only block until it is due ... |
| 145 | + } elseif ($scheduledAt = $this->timers->getFirst()) { |
| 146 | + $timeout = ($scheduledAt - $this->timers->getTime()) / 1_000_000_000; |
| 147 | + if ($timeout < 0) { |
| 148 | + $timeout = 0; |
| 149 | + } else { |
| 150 | + $timeout = $timeout > self::MAX_DURATION_SECONDS ? self::MAX_DURATION_SECONDS : $timeout; |
| 151 | + } |
| 152 | + |
| 153 | + // The only possible event is stream or signal activity, so wait forever ... |
| 154 | + } elseif ($this->readListeners || $this->writeListeners || !$this->signals->isEmpty()) { |
| 155 | + $timeout = self::MAX_DURATION_SECONDS; |
| 156 | + |
| 157 | + // There's nothing left to do ... |
| 158 | + } else { |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + $seconds = (int)$timeout; |
| 163 | + $nanoseconds = (int)(($timeout - $seconds) * 1_000_000_000); |
| 164 | + $waitTimeout = \Time\Duration::fromSeconds($seconds, $nanoseconds); |
| 165 | + |
| 166 | + foreach ($this->context->wait($waitTimeout) as $watcher) { |
| 167 | + $stream = $watcher->getHandle()->getStream(); |
| 168 | + $key = $watcher->getData(); |
| 169 | + $triggeredEvents = $watcher->getTriggeredEvents(); |
| 170 | + |
| 171 | + if (array_key_exists($key, $this->readListeners) && in_array(\Io\Poll\Event::Read, $triggeredEvents)) { |
| 172 | + \call_user_func($this->readListeners[$key], $stream); |
| 173 | + } |
| 174 | + |
| 175 | + if (array_key_exists($key, $this->writeListeners) && in_array(\Io\Poll\Event::Write, $triggeredEvents)) { |
| 176 | + \call_user_func($this->writeListeners[$key], $stream); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public function stop() |
| 183 | + { |
| 184 | + $this->running = false; |
| 185 | + } |
| 186 | + |
| 187 | + private function manageStream($key, $stream, \Io\Poll\Event $event, bool $add) |
| 188 | + { |
| 189 | + if (!array_key_exists($key, $this->watchers)) { |
| 190 | + if (!$add) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + $handle = new \StreamPollHandle($stream); |
| 195 | + $this->watchers[$key] = $this->context->add($handle, [$event], $key); |
| 196 | + |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + if (!$this->watchers[$key]->isActive()) { |
| 201 | + unset($this->watchers[$key]); |
| 202 | + |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + $events = $this->watchers[$key]->getWatchedEvents(); |
| 207 | + $events = array_filter($events, static fn (\Io\Poll\Event $watchedEvent)=> $watchedEvent === $event); |
| 208 | + if ($add) { |
| 209 | + $events[] = $event; |
| 210 | + } |
| 211 | + |
| 212 | + if (count($events) > 0) { |
| 213 | + $this->watchers[$key]->modifyEvents($events); |
| 214 | + |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + $this->watchers[$key]->remove(); |
| 219 | + unset($this->watchers[$key]); |
| 220 | + } |
| 221 | +} |
0 commit comments