-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
368 lines (333 loc) · 7.83 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
\ION\PHPLibEventFallback\PHPLibEventFallback::setUp();
/**
* Create and initialize new event base
*
* @return resource
*/
function event_base_new()
{
static $base;
if ($base) {
trigger_error("Only one event base may be initialized", E_USER_WARNING);
} else {
$base = fopen("ion.plf.base://event_base", "r+");
}
return $base;
}
/**
* Handle events
*
* @param resource $event_base
* @param int $flags
* @return int
*/
function event_base_loop($event_base, int $flags = 0)
{
try {
$result = ION::dispatch($flags);
if ($result) {
return 0;
} else {
return 1;
}
} catch (\Throwable $e) {
trigger_error($e, E_USER_ERROR);
return -1;
}
}
/**
* Destroy event base
*
* @param resource $event_base
*/
function event_base_free($event_base)
{
// do nothing
}
/**
* Abort event loop
*
* @param resource $event_base
* @return bool
*/
function event_base_loopbreak($event_base): bool
{
ION::stop();
return true;
}
/**
* Exit loop after a time
*
* @param resource $event_base
* @param int $timeout
* @return bool
*/
function event_base_loopexit($event_base, int $timeout = -1): bool
{
if ($timeout >= 0) {
ION::stop($timeout/1e6);
} else {
ION::stop(0.0);
}
return true;
}
/**
* Set the number of event priority levels
*
* @param resource $event_base
* @param int $npriorities
* @return bool
*/
function event_base_priority_init($event_base, int $npriorities): bool
{
return true;
}
/**
* Reinitialize the event base after a fork
*
* @param resource $event_base
* @return bool
*/
function event_base_reinit($event_base): bool
{
ION::reinit();
return true;
}
/*
* Event buffer
*/
/**
* Create new buffered event
*
* @param resource $stream
* @param callable $readcb
* @param callable $writecb
* @param callable $errorcb
* @param mixed $arg
* @return bool|resource
*/
function event_buffer_new($stream, callable $readcb = null, callable $writecb = null, callable $errorcb = null, $arg = null)
{
$ion_stream = \ION\Stream::resource($stream);
$context = stream_context_create([
"ion.plf.buffer" => [
"readcb" => $readcb,
"writecb" => $writecb,
"errorcb" => $errorcb,
"arg" => $arg,
"ion_stream" => $ion_stream
]
]);
return fopen("ion.plf.buffer://stream#".intval($stream), "r+", null, $context);
}
/**
* @param resource $bevent
* @return \ION\Stream
*/
function event_buffer_get_ion_stream($bevent): \ION\Stream
{
return stream_context_get_options($bevent)["ion_stream"];
}
/**
* Associate buffered event with an event base
*
* @param resource $event
* @param resource $event_base
* @return bool
*/
function event_buffer_base_set($event, $event_base)
{
return true;
}
/**
* Read data from a buffered event
*
* @param resource $bevent
* @param int $data_size
* @return string
*/
function event_buffer_read($bevent, int $data_size): string
{
return fread($bevent, $data_size);
}
/**
* Write data to a buffered event
*
* @param resource $bevent
* @param string $data
* @return int
*/
function event_buffer_write($bevent, string $data): int
{
return fwrite($bevent, $data);
}
/**
* Set or reset callbacks for a buffered event
*
* @param resource $bevent
* @param callable $readcb
* @param callable $writecb
* @param callable $errorcb
* @param mixed $arg
* @return bool
*/
function event_buffer_set_callback($bevent, callable $readcb = null, callable $writecb = null, callable $errorcb = null, $arg = null): bool
{
stream_context_set_option($bevent, "ion.plf.buffer", "readcb", $readcb);
stream_context_set_option($bevent, "ion.plf.buffer", "writecb", $writecb);
stream_context_set_option($bevent, "ion.plf.buffer", "errorcb", $errorcb);
stream_context_set_option($bevent, "ion.plf.buffer", "arg", $arg);
return true;
}
/**
* Destroy buffered event
*
* @param resource $bevent
*/
function event_buffer_free($bevent)
{
fclose($bevent);
}
/**
* Change a buffered event file descriptor
*
* @param resource $bevent
* @param resource $fd
*/
function event_buffer_fd_set($bevent, $fd)
{
$ion_stream = \ION\Stream::resource($fd);
stream_context_set_option($bevent, "ion.plf.buffer", "ion_stream", $ion_stream);
fseek($bevent, \ION\PHPLibEventFallback\Wrapper\EventBufferWrapper::CMD_UPDATE_STREAM);
}
/**
* Enable a buffered event
*
* @param resource $bevent
* @param int $events
* @return bool
* @todo add $events
*/
function event_buffer_enable($bevent, int $events): bool
{
return fseek($bevent, \ION\PHPLibEventFallback\Wrapper\EventBufferWrapper::CMD_ENABLE);
}
/**
* Disable a buffered event
*
* @param resource $bevent
* @param int $events
* @return bool
* @todo add $events
*/
function event_buffer_disable($bevent, int $events): bool
{
return fseek($bevent, \ION\PHPLibEventFallback\Wrapper\EventBufferWrapper::CMD_DISABLE);
}
/**
* Assign a priority to a buffered event
*
* @param resource $bevent
* @param int $priority
* @return bool
*/
function event_buffer_priority_set($bevent, int $priority): bool
{
return true;
}
/**
* Set read and write timeouts for a buffered event
*
* @param resource $bevent
* @param int $read_timeout
* @param int $write_timeout
* @return bool
* @todo
*/
function event_buffer_timeout_set($bevent, int $read_timeout, int $write_timeout): bool
{
}
/**
* Set the watermarks for read and write events
*
* @param resource $bevent
* @param int $events
* @param int $lowmark
* @param int $highmark
* @todo
*/
function event_buffer_watermark_set($bevent, int $events, int $lowmark, int $highmark)
{
}
/**
* Read line from buffer by EOL
* @param resource $bevent buffer event
* @param int $eol mask of constants BEV_EOL_ANY, BEV_EOL_CRLF, BEV_EOL_CRLF_STRICT, BEV_EOL_LF
* @return string
**/
function event_buffer_readln($bevent, $eol = BEV_EOL_ANY): string
{
$ion_stream = event_buffer_get_ion_stream($bevent);
$matches = [];
if ($eol & BEV_EOL_CRLF) {
$matches[] = $ion_stream->search("\r\n");
$matches[] = $ion_stream->search("\n\r");
}
if ($eol & BEV_EOL_CRLF_STRICT) {
$matches[] = $ion_stream->search("\r\n");
}
if ($eol & BEV_EOL_LF) {
$matches[] = $ion_stream->search("\n");
}
$matches = array_filter($matches, function ($v) {
return $v !== false;
});
if ($matches) {
return min($matches);
} else {
return "";
}
}
/**
* Read line from buffer by any string token
* @param resource $bevent buffer event
* @param string $token
* @param int $type one of constants BEV_WITHOUT_TOKEN, BEV_WITH_TOKEN, BEV_TRIM_TOKEN
* @param int $max_length
* @return string|false returns false if token not found
**/
function event_buffer_gets($bevent, $token, $type = BEV_TRIM_TOKEN, $max_length = 0): string
{
return event_buffer_get_ion_stream($bevent)->getLine($token, $type, $max_length);
}
/**
* Return size of bytes in the buffer
* @param resource $bevent buffer event
* @param int $type EV_READ or EV_WRITE
* @return int
**/
function event_buffer_get_length($bevent, $type = EV_READ): int
{
return event_buffer_get_ion_stream($bevent)->getSize($type);
}
/**
* Send a file
* @param resource $bevent buffer event
* @param resource $fd file descriptor
* @param int $length send specified count bytes if zero - send all file
* @param int $offset skip bytes
* @return bool
**/
function event_buffer_sendfile($bevent, $fd, $length = 0, $offset = 0): bool
{
$ion_stream = event_buffer_get_ion_stream($bevent);
try {
/* @var \ION\Stream $ion_stream */
$ion_stream->sendFile(stream_get_meta_data($fd)["uri"], $offset, $length == 0 ? -1 : $length);
return true;
} catch (\Throwable $e) {
trigger_error($e, E_USER_ERROR);
return false;
}
}