forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswoole_async.c
552 lines (491 loc) · 13.8 KB
/
swoole_async.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "php_streams.h"
#include "php_network.h"
#include "async.h"
#define PHP_SWOOLE_AIO_MAXEVENTS 128
typedef struct {
zval *callback;
zval *filename;
int fd;
off_t offset;
uint16_t type;
uint8_t once;
char *file_content;
uint32_t content_length;
} swoole_async_file_request;
typedef struct {
zval *callback;
zval *domain;
} swoole_async_dns_request;
static void php_swoole_check_aio();
static void php_swoole_aio_onComplete(swAio_event *event);
static char php_swoole_aio_init = 0;
static swHashMap php_swoole_open_files = NULL;
static void php_swoole_check_aio()
{
if (php_swoole_aio_init == 0)
{
php_swoole_check_reactor();
swoole_aio_init(SwooleG.main_reactor, PHP_SWOOLE_AIO_MAXEVENTS);
swoole_aio_set_callback(php_swoole_aio_onComplete);
php_swoole_try_run_reactor();
php_swoole_aio_init = 1;
}
}
static void php_swoole_aio_onComplete(swAio_event *event)
{
int isEOF = SW_FALSE;
int64_t ret;
zval *retval = NULL, *zcallback = NULL, *zwriten = NULL;
zval *zcontent = NULL;
zval **args[2];
swoole_async_file_request *file_req = NULL;
swoole_async_dns_request *dns_req = NULL;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (event->type == SW_AIO_DNS_LOOKUP)
{
dns_req = (swoole_async_dns_request *) event->req;
if (dns_req->callback == NULL)
{
zend_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[2]");
return;
}
zcallback = dns_req->callback;
}
else
{
if (zend_hash_find(&php_sw_aio_callback, (char *)&(event->fd), sizeof(event->fd), (void**) &file_req) != SUCCESS)
{
zend_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[1]");
return;
}
if (file_req->callback == NULL && file_req->type == SW_AIO_READ)
{
zend_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[2]");
return;
}
zcallback = file_req->callback;
}
ret = event->ret;
if (ret < 0)
{
zend_error(E_WARNING, "swoole_async: Aio Error: %s[%d]", strerror(event->error), event->error);
}
else if (file_req != NULL)
{
if (ret == 0)
{
bzero(event->buf, event->nbytes);
isEOF = SW_TRUE;
}
else if (file_req->once == 1 && ret < file_req->content_length)
{
zend_error(E_WARNING, "swoole_async: ret_length[%d] < req->length[%d].", (int) ret, file_req->content_length);
}
file_req->offset += event->ret;
}
if (event->type == SW_AIO_READ)
{
MAKE_STD_ZVAL(zcontent);
args[0] = &file_req->filename;
args[1] = &zcontent;
ZVAL_STRINGL(zcontent, event->buf, ret, 0);
}
else if(event->type == SW_AIO_WRITE)
{
MAKE_STD_ZVAL(zwriten);
args[0] = &file_req->filename;
args[1] = &zwriten;
ZVAL_LONG(zwriten, ret);
}
else if(event->type == SW_AIO_DNS_LOOKUP)
{
MAKE_STD_ZVAL(zcontent);
args[0] = &dns_req->domain;
ZVAL_STRING(zcontent, event->buf, 0);
args[1] = &zcontent;
}
else
{
zend_error(E_WARNING, "swoole_async: onAsyncComplete unknow event type");
return;
}
if (zcallback)
{
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
zend_error(E_WARNING, "swoole_async: onAsyncComplete handler error");
return;
}
}
//readfile/writefile
if (file_req != NULL)
{
//只操作一次,完成后释放缓存区并关闭文件
if (file_req->once == 1)
{
close_file:
zval_ptr_dtor(&file_req->callback);
zval_ptr_dtor(&file_req->filename);
#ifdef SW_AIO_LINUX_NATIVE
free(event->buf);
#else
efree(event->buf);
#endif
close(event->fd);
//remove from hashtable
zend_hash_del(&php_sw_aio_callback, (char *)&(event->fd), sizeof(event->fd));
}
else if(file_req->type == SW_AIO_WRITE)
{
if (retval != NULL && !Z_BVAL_P(retval))
{
swHashMap_del(&php_swoole_open_files, Z_STRVAL_P(file_req->filename), Z_STRLEN_P(file_req->filename));
}
}
else
{
if (!Z_BVAL_P(retval) || isEOF)
{
goto close_file;
}
else if (swoole_aio_read(event->fd, event->buf, event->nbytes, file_req->offset) < 0)
{
zend_error(E_WARNING, "swoole_async: continue to read failed. Error: %s[%d]", strerror(event->error),
event->error);
}
}
}
else if(dns_req != NULL)
{
zval_ptr_dtor(&dns_req->callback);
zval_ptr_dtor(&dns_req->domain);
efree(dns_req);
efree(event->buf);
}
if (zcontent != NULL)
{
efree(zcontent);
}
if (zwriten != NULL)
{
zval_ptr_dtor(&zwriten);
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
}
PHP_FUNCTION(swoole_async_read)
{
zval *cb;
zval *filename;
long trunk_len = 8192;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &filename, &cb, &trunk_len) == FAILURE)
{
return;
}
convert_to_string(filename);
#ifdef HAVE_LINUX_NATIVE_AIO
int open_flag = O_RDONLY | O_DIRECT;
#else
int open_flag = O_RDONLY;
#endif
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
zend_error(E_WARNING, "swoole_async_readfile: open file[%s] failed. Error: %s[%d]", Z_STRVAL_P(filename), strerror(errno), errno);
RETURN_FALSE;
}
void *fcnt;
#ifdef HAVE_LINUX_NATIVE_AIO
int buf_len = trunk_len + (sysconf(_SC_PAGESIZE) - (trunk_len % sysconf(_SC_PAGESIZE)));
if (posix_memalign((void **)&fcnt, sysconf(_SC_PAGESIZE), buf_len))
{
zend_error(E_WARNING, "posix_memalign failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#else
fcnt = emalloc(trunk_len);
if (fcnt == NULL)
{
zend_error(E_WARNING, "malloc failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#endif
//printf("buf_len=%d|addr=%p\n", buf_len, fcnt);
//printf("pagesize=%d|st_size=%d\n", sysconf(_SC_PAGESIZE), buf_len);
swoole_async_file_request req;
req.fd = fd;
req.filename = filename;
req.callback = cb;
req.file_content = fcnt;
req.once = 0;
req.type = SW_AIO_READ;
req.content_length = trunk_len;
req.offset = 0;
Z_ADDREF_PP(&cb);
Z_ADDREF_PP(&filename);
if (zend_hash_update(&php_sw_aio_callback, (char * )&fd, sizeof(fd), &req, sizeof(swoole_async_file_request),
NULL) == FAILURE)
{
zend_error(E_WARNING, "swoole_async_readfile add to hashtable[1] failed");
RETURN_FALSE;
}
php_swoole_check_aio();
SW_CHECK_RETURN(swoole_aio_read(fd, fcnt, trunk_len, 0));
RETURN_TRUE;
}
PHP_FUNCTION(swoole_async_write)
{
zval *cb = NULL;
zval *filename;
char *fcnt;
int fcnt_len;
int fd;
off_t offset;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zsl|z", &filename, &fcnt, &fcnt_len, &offset, &cb) == FAILURE)
{
return;
}
convert_to_string(filename);
char *wt_cnt;
#ifdef SW_AIO_LINUX_NATIVE
if (posix_memalign((void **)&wt_cnt, sysconf(_SC_PAGESIZE), fcnt_len))
{
zend_error(E_WARNING, "posix_memalign failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#else
wt_cnt = fcnt;
wt_cnt = emalloc(fcnt_len);
#endif
swoole_async_file_request *req = swHashMap_find(&php_swoole_open_files, Z_STRVAL_P(filename), Z_STRLEN_P(filename));
if (req == NULL)
{
#ifdef HAVE_LINUX_NATIVE_AIO
int open_flag = O_WRONLY | O_DIRECT | O_CREAT;
#else
int open_flag = O_WRONLY | O_CREAT;
#endif
fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
zend_error(E_WARNING, "swoole_async_write: open file failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
swoole_async_file_request new_req;
new_req.fd = fd;
new_req.filename = filename;
new_req.callback = cb;
new_req.file_content = wt_cnt;
new_req.once = 0;
new_req.type = SW_AIO_WRITE;
new_req.content_length = fcnt_len;
new_req.offset = 0;
if (cb != NULL)
{
Z_ADDREF_PP(&cb);
}
Z_ADDREF_PP(&filename);
if (zend_hash_update(&php_sw_aio_callback, (char *)&fd, sizeof(fd), (void **) &new_req, sizeof(new_req), (void **) &req) == FAILURE)
{
zend_error(E_WARNING, "swoole_async_write: add to hashtable[1] failed");
RETURN_FALSE;
}
swHashMap_add(&php_swoole_open_files, Z_STRVAL_P(filename), Z_STRLEN_P(filename), req);
}
else
{
fd = req->fd;
}
swTrace("buf_len=%d|addr=%p", buf_len, fcnt);
swTrace("pagesize=%d|st_size=%d", sysconf(_SC_PAGESIZE), buf_len);
memcpy(wt_cnt, fcnt, fcnt_len);
php_swoole_check_aio();
SW_CHECK_RETURN(swoole_aio_write(fd, wt_cnt, fcnt_len, offset));
RETURN_TRUE;
}
PHP_FUNCTION(swoole_async_readfile)
{
zval *cb;
zval *filename;
#ifdef HAVE_LINUX_NATIVE_AIO
int open_flag = O_RDONLY | O_DIRECT;
#else
int open_flag = O_RDONLY;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &filename, &cb) == FAILURE)
{
return;
}
convert_to_string(filename);
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
zend_error(E_WARNING, "swoole_async_readfile: open file[%s] failed. Error: %s[%d]", Z_STRVAL_P(filename), strerror(errno), errno);
RETURN_FALSE;
}
struct stat file_stat;
if (fstat(fd, &file_stat) < 0)
{
zend_error(E_WARNING, "swoole_async_readfile: fstat failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
if (file_stat.st_size <= 0)
{
zend_error(E_WARNING, "swoole_async_readfile: file is empty.");
RETURN_FALSE;
}
if (file_stat.st_size > SW_AIO_MAX_FILESIZE)
{
zend_error(E_WARNING,
"swoole_async_readfile: file_size[size=%ld|max_size=%d] is too big. Please use swoole_async_read.",
(long int) file_stat.st_size, SW_AIO_MAX_FILESIZE);
RETURN_FALSE;
}
void *fcnt;
#ifdef HAVE_LINUX_NATIVE_AIO
int buf_len = file_stat.st_size + (sysconf(_SC_PAGESIZE) - (file_stat.st_size % sysconf(_SC_PAGESIZE)));
if (posix_memalign((void **)&fcnt, sysconf(_SC_PAGESIZE), buf_len))
{
zend_error(E_WARNING, "posix_memalign failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#else
int buf_len = file_stat.st_size;
fcnt = emalloc(buf_len);
if (fcnt == NULL)
{
zend_error(E_WARNING, "malloc failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#endif
//printf("buf_len=%d|addr=%p\n", buf_len, fcnt);
//printf("pagesize=%d|st_size=%d\n", sysconf(_SC_PAGESIZE), buf_len);
swoole_async_file_request req;
req.fd = fd;
req.filename = filename;
req.callback = cb;
req.file_content = fcnt;
req.once = 1;
req.type = SW_AIO_READ;
req.content_length = file_stat.st_size;
req.offset = 0;
Z_ADDREF_PP(&cb);
Z_ADDREF_PP(&filename);
if(zend_hash_update(&php_sw_aio_callback, (char *)&fd, sizeof(fd), &req, sizeof(swoole_async_file_request), NULL) == FAILURE)
{
zend_error(E_WARNING, "swoole_async_readfile add to hashtable failed");
RETURN_FALSE;
}
php_swoole_check_aio();
SW_CHECK_RETURN(swoole_aio_read(fd, fcnt, buf_len, 0));
}
PHP_FUNCTION(swoole_async_writefile)
{
zval *cb = NULL;
zval *filename;
char *fcnt;
int fcnt_len;
#ifdef HAVE_LINUX_NATIVE_AIO
int open_flag = O_CREAT | O_WRONLY | O_DIRECT;
#else
int open_flag = O_CREAT | O_WRONLY;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|z", &filename, &fcnt, &fcnt_len, &cb) == FAILURE)
{
return;
}
if (fcnt_len <= 0)
{
zend_error(E_WARNING, "swoole_async_writefile: file is empty.");
RETURN_FALSE;
}
if (fcnt_len > SW_AIO_MAX_FILESIZE)
{
zend_error(E_WARNING,
"swoole_async_writefile: file_size[size=%d|max_size=%d] is too big. Please use swoole_async_read.",
fcnt_len, SW_AIO_MAX_FILESIZE);
RETURN_FALSE;
}
convert_to_string(filename);
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
zend_error(E_WARNING, "swoole_async_writefile: open file failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
char *wt_cnt;
#ifdef SW_AIO_LINUX_NATIVE
fcnt_len = fcnt_len + (sysconf(_SC_PAGESIZE) - (fcnt_len % sysconf(_SC_PAGESIZE)));
if (posix_memalign((void **)&wt_cnt, sysconf(_SC_PAGESIZE), fcnt_len))
{
zend_error(E_WARNING, "posix_memalign failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
#else
wt_cnt = emalloc(fcnt_len);
#endif
memcpy(wt_cnt, fcnt, fcnt_len);
swoole_async_file_request req;
req.fd = fd;
req.filename = filename;
req.callback = cb;
req.type = SW_AIO_WRITE;
req.file_content = wt_cnt;
req.once = 1;
req.content_length = fcnt_len;
req.offset = 0;
Z_ADDREF_PP(&filename);
if (req.callback != NULL)
{
Z_ADDREF_PP(&req.callback);
}
if (zend_hash_update(&php_sw_aio_callback, (char *)&fd, sizeof(fd), &req, sizeof(swoole_async_file_request), NULL) == FAILURE)
{
zend_error(E_WARNING, "swoole_async_writefile add to hashtable failed");
RETURN_FALSE;
}
memcpy(wt_cnt, fcnt, fcnt_len);
php_swoole_check_aio();
SW_CHECK_RETURN(swoole_aio_write(fd, wt_cnt, fcnt_len, 0));
}
PHP_FUNCTION(swoole_async_dns_lookup)
{
zval *domain;
zval *cb;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &domain, &cb) == FAILURE)
{
return;
}
if (Z_STRLEN_P(domain) == 0)
{
zend_error(E_WARNING, "swoole_async_dns_lookup: domain name empty.");
RETURN_FALSE;
}
swoole_async_dns_request *req = emalloc(sizeof(swoole_async_dns_request));
req->callback = cb;
req->domain = domain;
Z_ADDREF_PP(&req->callback);
Z_ADDREF_PP(&req->domain);
void *buf = emalloc(SW_IP_MAX_LENGTH);
memcpy(buf, Z_STRVAL_P(domain), Z_STRLEN_P(domain));
php_swoole_check_aio();
SW_CHECK_RETURN(swoole_aio_dns_lookup(req, buf, SW_IP_MAX_LENGTH));
}