/
functions.php
329 lines (294 loc) · 8.45 KB
/
functions.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
declare(strict_types=1);
/**
* This file is part of OpenSwoole.
* @link https://openswoole.com
* @contact hello@openswoole.com
*/
namespace OpenSwoole\Core\Coroutine;
use OpenSwoole\Coroutine;
use OpenSwoole\Coroutine\Socket;
use RuntimeException;
// std
function exec(string $command, &$output = null, &$returnVar = null)
{
$result = Coroutine::exec($command);
if ($result) {
$outputList = explode(PHP_EOL, $result['output']);
foreach ($outputList as &$value) {
$value = rtrim($value);
}
if (($endLine = end($outputList)) === '') {
array_pop($outputList);
$endLine = end($outputList);
}
if ($output) {
$output = array_merge($output, $outputList);
} else {
$output = $outputList;
}
$returnVar = $result['code'];
return $endLine;
}
return false;
}
function shell_exec(string $cmd)
{
$result = Coroutine::exec($cmd);
if ($result && $result['output'] !== '') {
return $result['output'];
}
return null;
}
// socket
function socket_create(int $domain, int $type, int $protocol)
{
return new Socket($domain, $type, $protocol);
}
function socket_connect(Socket $socket, string $address, int $port = 0)
{
return $socket->connect($address, $port);
}
function socket_read(Socket $socket, int $length, int $type = PHP_BINARY_READ)
{
if ($type != PHP_BINARY_READ) {
return $socket->recvLine($length);
}
return $socket->recv($length);
}
function socket_write(Socket $socket, string $buffer, int $length = 0): int
{
if ($length > 0 and $length < strlen($buffer)) {
$buffer = substr($buffer, 0, $length);
}
return $socket->send($buffer);
}
function socket_send(Socket $socket, string $buffer, int $length, int $flags): int
{
if ($flags != 0) {
throw new RuntimeException("\$flags[{$flags}] is not supported");
}
return socket_write($socket, $buffer, $length);
}
function socket_recv(Socket $socket, &$buffer, int $length, int $flags)
{
if ($flags & MSG_OOB) {
throw new RuntimeException('$flags[MSG_OOB] is not supported');
}
if ($flags & MSG_PEEK) {
$buffer = $socket->peek($length);
}
$timeout = $flags & MSG_DONTWAIT ? 0.001 : 0;
if ($flags & MSG_WAITALL) {
$buffer = $socket->recvAll($length, $timeout);
} else {
$buffer = $socket->recv($length, $timeout);
}
if ($buffer === false) {
return false;
}
return strlen($buffer);
}
function socket_sendto(Socket $socket, string $buffer, int $length, int $flags, string $addr, int $port = 0)
{
if ($flags != 0) {
throw new RuntimeException("\$flags[{$flags}] is not supported");
}
if ($socket->type != SOCK_DGRAM) {
throw new RuntimeException('only supports dgram type socket');
}
if ($length > 0 and $length < strlen($buffer)) {
$buffer = substr($buffer, 0, $length);
}
return $socket->sendto($addr, $port, $buffer);
}
function socket_recvfrom(Socket $socket, &$buffer, int $length, int $flags, &$name, &$port)
{
if ($flags != 0) {
throw new RuntimeException("\$flags[{$flags}] is not supported");
}
if ($socket->type != SOCK_DGRAM) {
throw new RuntimeException('only supports dgram type socket');
}
$data = $socket->recvfrom($peer);
if ($data === false) {
return false;
}
$name = $peer['address'];
if (func_num_args() == 6) {
$port = $peer['port'];
}
if ($length < strlen($data)) {
$buffer = substr($data, 0, $length);
} else {
$buffer = $data;
}
return 100;
}
function socket_bind(Socket $socket, string $address, int $port = 0): bool
{
return $socket->bind($address, $port);
}
function socket_listen(Socket $socket, int $backlog = 0): bool
{
return $socket->listen($backlog);
}
function socket_create_listen(int $port, int $backlog = 128)
{
$socket = new Socket(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket->bind('0.0.0.0', $port)) {
return false;
}
if (!$socket->listen($backlog)) {
return false;
}
return $socket;
}
function socket_accept(Socket $socket)
{
return $socket->accept();
}
function socket_getpeername(Socket $socket, &$address, &$port = null)
{
$info = $socket->getpeername();
if (!$info) {
return false;
}
$address = $info['address'];
if (func_num_args() == 3) {
$port = $info['port'];
}
return true;
}
function socket_getsockname(Socket $socket, &$address, &$port = null)
{
$info = $socket->getsockname();
if (!$info) {
return false;
}
$address = $info['address'];
if (func_num_args() == 3) {
$port = $info['port'];
}
return true;
}
function socket_set_option(Socket $socket, int $level, int $optname, $optval): bool
{
return $socket->setOption($level, $optname, $optval);
}
function socket_setopt(Socket $socket, int $level, int $optname, $optval): bool
{
return $socket->setOption($level, $optname, $optval);
}
function socket_get_option(Socket $socket, int $level, int $optname)
{
return $socket->getOption($level, $optname);
}
function socket_getopt(Socket $socket, int $level, int $optname)
{
return $socket->getOption($level, $optname);
}
function socket_shutdown(Socket $socket, int $how = 2)
{
$socket->shutdown($how);
}
function socket_close(Socket $socket)
{
$socket->close();
}
function socket_clear_error(Socket $socket = null)
{
if ($socket) {
$socket->errCode = 0;
}
clear_error();
}
function socket_last_error(Socket $socket = null): int
{
if ($socket) {
return $socket->errCode;
}
return last_error();
}
function socket_set_block(Socket $socket)
{
if (isset($socket->__ext_sockets_nonblock) and $socket->__ext_sockets_nonblock) {
$socket->setOption(SOL_SOCKET, SO_RCVTIMEO, $socket->__ext_sockets_timeout);
}
$socket->__ext_sockets_nonblock = false;
return true;
}
function socket_set_nonblock(Socket $socket)
{
if (isset($socket->__ext_sockets_nonblock) and $socket->__ext_sockets_nonblock) {
return true;
}
$socket->__ext_sockets_nonblock = true;
$socket->__ext_sockets_timeout = $socket->getOption(SOL_SOCKET, SO_RCVTIMEO);
$socket->setOption(SOL_SOCKET, SO_RCVTIMEO, ['sec' => 0, 'usec' => 1000]);
return true;
}
// co
function batch(array $tasks, float $timeout = -1): array
{
$wg = new WaitGroup(count($tasks));
foreach ($tasks as $id => $task) {
Coroutine::create(function () use ($wg, &$tasks, $id, $task) {
$tasks[$id] = null;
$tasks[$id] = $task();
$wg->done();
});
}
$wg->wait($timeout);
return $tasks;
}
function parallel(int $n, callable $fn): void
{
$count = $n;
$wg = new WaitGroup($n);
while ($count--) {
Coroutine::create(function () use ($fn, $wg) {
$fn();
$wg->done();
});
}
$wg->wait();
}
function map(array $list, callable $fn, float $timeout = -1): array
{
$wg = new WaitGroup(count($list));
foreach ($list as $id => $elem) {
Coroutine::create(function () use ($wg, &$list, $id, $elem, $fn): void {
$list[$id] = null;
$list[$id] = $fn($elem);
$wg->done();
});
}
$wg->wait($timeout);
return $list;
}
function deadlock_check()
{
$all_coroutines = Coroutine::list();
$count = Coroutine::stats()['coroutine_num'];
echo "\n===================================================================",
"\n [FATAL ERROR]: all coroutines (count: {$count}) are asleep - deadlock!",
"\n===================================================================\n";
$options = Coroutine::getOptions();
if (empty($options['deadlock_check_disable_trace'])) {
$index = 0;
$limit = empty($options['deadlock_check_limit']) ? 32 : intval($options['deadlock_check_limit']);
$depth = empty($options['deadlock_check_depth']) ? 32 : intval($options['deadlock_check_depth']);
foreach ($all_coroutines as $cid) {
echo "\n [Coroutine-{$cid}]";
echo "\n--------------------------------------------------------------------\n";
echo Coroutine::printBackTrace($cid, DEBUG_BACKTRACE_IGNORE_ARGS, $depth);
echo "\n";
$index++;
// limit the number of maximum outputs
if ($index >= $limit) {
break;
}
}
}
}