-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathSwow.php
303 lines (278 loc) · 8.14 KB
/
Swow.php
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
declare(strict_types=1);
namespace Workerman\Events;
use RuntimeException;
use Workerman\Coroutine\Coroutine\Swow as Coroutine;
use Swow\Signal;
use Swow\SignalException;
use function Swow\Sync\waitAll;
final class Swow implements EventInterface
{
/**
* All listeners for read timer.
*
* @var array<int, int>
*/
private array $eventTimer = [];
/**
* All listeners for read event.
*
* @var array<int, Coroutine>
*/
private array $readEvents = [];
/**
* All listeners for write event.
*
* @var array<int, Coroutine>
*/
private array $writeEvents = [];
/**
* All listeners for signal.
*
* @var array<int, Coroutine>
*/
private array $signalListener = [];
/**
* @var ?callable
*/
private $errorHandler = null;
/**
* Get timer count.
*
* @return integer
*/
public function getTimerCount(): int
{
return count($this->eventTimer);
}
/**
* {@inheritdoc}
*/
public function delay(float $delay, callable $func, array $args = []): int
{
$t = (int)($delay * 1000);
$t = max($t, 1);
$coroutine = Coroutine::run(function () use ($t, $func, $args): void {
msleep($t);
unset($this->eventTimer[Coroutine::getCurrent()->getId()]);
$this->safeCall($func, $args);
});
$timerId = $coroutine->getId();
$this->eventTimer[$timerId] = $timerId;
return $timerId;
}
/**
* {@inheritdoc}
*/
public function repeat(float $interval, callable $func, array $args = []): int
{
$t = (int)($interval * 1000);
$t = max($t, 1);
$coroutine = Coroutine::run(function () use ($t, $func, $args): void {
// @phpstan-ignore-next-line While loop condition is always true.
while (true) {
msleep($t);
$this->safeCall($func, $args);
}
});
$timerId = $coroutine->getId();
$this->eventTimer[$timerId] = $timerId;
return $timerId;
}
/**
* {@inheritdoc}
*/
public function offDelay(int $timerId): bool
{
if (isset($this->eventTimer[$timerId])) {
try {
(Coroutine::getAll()[$timerId])->kill();
return true;
} finally {
unset($this->eventTimer[$timerId]);
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function offRepeat(int $timerId): bool
{
return $this->offDelay($timerId);
}
/**
* {@inheritdoc}
*/
public function deleteAllTimer(): void
{
foreach ($this->eventTimer as $timerId) {
$this->offDelay($timerId);
}
}
/**
* {@inheritdoc}
*/
public function onReadable($stream, callable $func): void
{
$fd = (int)$stream;
if (isset($this->readEvents[$fd])) {
$this->offReadable($stream);
}
Coroutine::run(function () use ($stream, $func, $fd): void {
try {
$this->readEvents[$fd] = Coroutine::getCurrent();
while (true) {
if (!is_resource($stream)) {
$this->offReadable($stream);
break;
}
// Under Windows, setting a timeout is necessary; otherwise, the accept cannot be listened to.
// Setting it to 1000ms will result in a 1-second delay for the first accept under Windows.
$rEvent = stream_poll_one($stream, STREAM_POLLIN | STREAM_POLLHUP, 1000);
if (!isset($this->readEvents[$fd]) || $this->readEvents[$fd] !== Coroutine::getCurrent()) {
break;
}
if ($rEvent !== STREAM_POLLNONE) {
$this->safeCall($func, [$stream]);
}
if ($rEvent !== STREAM_POLLIN && $rEvent !== STREAM_POLLNONE) {
$this->offReadable($stream);
break;
}
}
} catch (RuntimeException) {
$this->offReadable($stream);
}
});
}
/**
* {@inheritdoc}
*/
public function offReadable($stream): bool
{
// 在当前协程执行 $coroutine->kill() 会导致不可预知问题,所以没有使用$coroutine->kill()
$fd = (int)$stream;
if (isset($this->readEvents[$fd])) {
unset($this->readEvents[$fd]);
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
public function onWritable($stream, callable $func): void
{
$fd = (int)$stream;
if (isset($this->writeEvents[$fd])) {
$this->offWritable($stream);
}
Coroutine::run(function () use ($stream, $func, $fd): void {
try {
$this->writeEvents[$fd] = Coroutine::getCurrent();
while (true) {
$rEvent = stream_poll_one($stream, STREAM_POLLOUT | STREAM_POLLHUP);
if (!isset($this->writeEvents[$fd]) || $this->writeEvents[$fd] !== Coroutine::getCurrent()) {
break;
}
if ($rEvent !== STREAM_POLLNONE) {
$this->safeCall($func, [$stream]);
}
if ($rEvent !== STREAM_POLLOUT) {
$this->offWritable($stream);
break;
}
}
} catch (RuntimeException) {
$this->offWritable($stream);
}
});
}
/**
* {@inheritdoc}
*/
public function offWritable($stream): bool
{
$fd = (int)$stream;
if (isset($this->writeEvents[$fd])) {
unset($this->writeEvents[$fd]);
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
public function onSignal(int $signal, callable $func): void
{
Coroutine::run(function () use ($signal, $func): void {
$this->signalListener[$signal] = Coroutine::getCurrent();
while (1) {
try {
Signal::wait($signal);
if (!isset($this->signalListener[$signal]) ||
$this->signalListener[$signal] !== Coroutine::getCurrent()) {
break;
}
$this->safeCall($func, [$signal]);
} catch (SignalException) {
// do nothing
}
}
});
}
/**
* {@inheritdoc}
*/
public function offSignal(int $signal): bool
{
if (!isset($this->signalListener[$signal])) {
return false;
}
unset($this->signalListener[$signal]);
return true;
}
/**
* {@inheritdoc}
*/
public function run(): void
{
waitAll();
}
/**
* Destroy loop.
*
* @return void
*/
public function stop(): void
{
Coroutine::killAll();
}
/**
* {@inheritdoc}
*/
public function setErrorHandler(callable $errorHandler): void
{
$this->errorHandler = $errorHandler;
}
/**
* @param callable $func
* @param array $args
* @return void
*/
private function safeCall(callable $func, array $args = []): void
{
Coroutine::run(function () use ($func, $args): void {
try {
$func(...$args);
} catch (\Throwable $e) {
if ($this->errorHandler === null) {
echo $e;
} else {
($this->errorHandler)($e);
}
}
});
}
}