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