-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathUvDriver.php
More file actions
262 lines (211 loc) · 8.33 KB
/
UvDriver.php
File metadata and controls
262 lines (211 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types=1);
namespace Revolt\EventLoop\Driver;
use Revolt\EventLoop\FiberFactory;
use Revolt\EventLoop\Internal\AbstractDriver;
use Revolt\EventLoop\Internal\DriverCallback;
use Revolt\EventLoop\Internal\SignalCallback;
use Revolt\EventLoop\Internal\StreamCallback;
use Revolt\EventLoop\Internal\StreamReadableCallback;
use Revolt\EventLoop\Internal\StreamWritableCallback;
use Revolt\EventLoop\Internal\TimerCallback;
final class UvDriver extends AbstractDriver
{
public static function isSupported(): bool
{
return \extension_loaded("uv");
}
/** @var resource|\UVLoop A uv_loop resource created with uv_loop_new() */
private $handle;
/** @var array<string, resource> */
private array $events = [];
/** @var array<int, array<array-key, DriverCallback>> */
private array $uvCallbacks = [];
/** @var array<int, resource> */
private array $streams = [];
private readonly \Closure $ioCallback;
private readonly \Closure $timerCallback;
private readonly \Closure $signalCallback;
public function __construct(?FiberFactory $fiberFactory = null)
{
parent::__construct($fiberFactory);
$this->handle = \uv_loop_new();
$this->ioCallback = function ($event, $status, $events, $resource): void {
$callbacks = $this->uvCallbacks[(int) $event];
// Invoke the callback on errors, as this matches behavior with other loop back-ends.
// Re-enable callback as libuv disables the callback on non-zero status.
if ($status !== 0) {
$flags = 0;
foreach ($callbacks as $callback) {
\assert($callback instanceof StreamCallback);
$flags |= $callback->invokable ? $this->getStreamCallbackFlags($callback) : 0;
}
\uv_poll_start($event, $flags, $this->ioCallback);
}
foreach ($callbacks as $callback) {
\assert($callback instanceof StreamCallback);
// $events is ORed with 4 to trigger callback if no events are indicated (0) or on UV_DISCONNECT (4).
// http://docs.libuv.org/en/v1.x/poll.html
if (!($this->getStreamCallbackFlags($callback) & $events || ($events | 4) === 4)) {
continue;
}
$this->enqueueCallback($callback);
}
};
$this->timerCallback = function ($event): void {
$callback = $this->uvCallbacks[(int) $event][0];
\assert($callback instanceof TimerCallback);
$this->enqueueCallback($callback);
};
$this->signalCallback = function ($event): void {
$callback = $this->uvCallbacks[(int) $event][0];
$this->enqueueCallback($callback);
};
}
/**
* {@inheritdoc}
*/
public function cancel(string $callbackId): void
{
parent::cancel($callbackId);
if (!isset($this->events[$callbackId])) {
return;
}
$event = $this->events[$callbackId];
$eventId = (int) $event;
if (isset($this->uvCallbacks[$eventId][0])) { // All except IO callbacks.
unset($this->uvCallbacks[$eventId]);
} elseif (isset($this->uvCallbacks[$eventId][$callbackId])) {
$callback = $this->uvCallbacks[$eventId][$callbackId];
unset($this->uvCallbacks[$eventId][$callbackId]);
\assert($callback instanceof StreamCallback);
if (empty($this->uvCallbacks[$eventId])) {
unset($this->uvCallbacks[$eventId], $this->streams[(int) $callback->stream]);
}
}
unset($this->events[$callbackId]);
}
/**
* @return \UVLoop|resource
*/
public function getHandle(): mixed
{
return $this->handle;
}
protected function now(): float
{
\uv_update_time($this->handle);
/** @psalm-suppress TooManyArguments */
return \uv_now($this->handle) / 1000;
}
/**
* {@inheritdoc}
*/
protected function dispatch(bool $blocking): void
{
/** @psalm-suppress TooManyArguments */
\uv_run($this->handle, $blocking ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
}
/**
* {@inheritdoc}
*/
protected function activate(array $callbacks): void
{
$now = $this->now();
foreach ($callbacks as $callback) {
$id = $callback->id;
if ($callback instanceof StreamCallback) {
\assert(\is_resource($callback->stream));
$streamId = (int) $callback->stream;
if (isset($this->streams[$streamId])) {
$event = $this->streams[$streamId];
} elseif (isset($this->events[$id])) {
$event = $this->streams[$streamId] = $this->events[$id];
} else {
/** @psalm-suppress TooManyArguments */
$event = $this->streams[$streamId] = \uv_poll_init_socket($this->handle, $callback->stream);
}
$eventId = (int) $event;
$this->events[$id] = $event;
$this->uvCallbacks[$eventId][$id] = $callback;
$flags = 0;
foreach ($this->uvCallbacks[$eventId] as $w) {
\assert($w instanceof StreamCallback);
$flags |= $w->enabled ? ($this->getStreamCallbackFlags($w)) : 0;
}
\uv_poll_start($event, $flags, $this->ioCallback);
} elseif ($callback instanceof TimerCallback) {
if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
$event = $this->events[$id] = \uv_timer_init($this->handle);
}
$this->uvCallbacks[(int) $event] = [$callback];
\uv_timer_start(
$event,
(int) \min(\max(0, \ceil(($callback->expiration - $now) * 1000)), \PHP_INT_MAX),
$callback->repeat ? (int) \min(\max(0, \ceil($callback->interval * 1000)), \PHP_INT_MAX) : 0,
$this->timerCallback
);
} elseif ($callback instanceof SignalCallback) {
if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
/** @psalm-suppress TooManyArguments */
$event = $this->events[$id] = \uv_signal_init($this->handle);
}
$this->uvCallbacks[(int) $event] = [$callback];
/** @psalm-suppress TooManyArguments */
\uv_signal_start($event, $this->signalCallback, $callback->signal);
} else {
// @codeCoverageIgnoreStart
throw new \Error("Unknown callback type");
// @codeCoverageIgnoreEnd
}
}
}
/**
* {@inheritdoc}
*/
protected function deactivate(DriverCallback $callback): void
{
$id = $callback->id;
if (!isset($this->events[$id])) {
return;
}
$event = $this->events[$id];
if (!\uv_is_active($event)) {
return;
}
if ($callback instanceof StreamCallback) {
$flags = 0;
foreach ($this->uvCallbacks[(int) $event] as $w) {
\assert($w instanceof StreamCallback);
$flags |= $w->invokable ? ($this->getStreamCallbackFlags($w)) : 0;
}
if ($flags) {
\uv_poll_start($event, $flags, $this->ioCallback);
} else {
\uv_poll_stop($event);
}
} elseif ($callback instanceof TimerCallback) {
\uv_timer_stop($event);
} elseif ($callback instanceof SignalCallback) {
\uv_signal_stop($event);
} else {
// @codeCoverageIgnoreStart
throw new \Error("Unknown callback type");
// @codeCoverageIgnoreEnd
}
}
private function getStreamCallbackFlags(StreamCallback $callback): int
{
if ($callback instanceof StreamWritableCallback) {
return \UV::WRITABLE;
}
if ($callback instanceof StreamReadableCallback) {
return \UV::READABLE;
}
throw new \Error('Invalid callback type');
}
}