forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_event.c
859 lines (755 loc) · 23.1 KB
/
swoole_event.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2015 The Swoole Group |
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
typedef struct
{
struct
{
zval cb_read;
zval cb_write;
zval socket;
} stack;
zval *cb_read;
zval *cb_write;
zval *socket;
} php_reactor_fd;
static int php_swoole_event_onRead(swReactor *reactor, swEvent *event);
static int php_swoole_event_onWrite(swReactor *reactor, swEvent *event);
static int php_swoole_event_onError(swReactor *reactor, swEvent *event);
static void php_swoole_event_onEndCallback(void *_cb);
static void free_event_callback(void* data)
{
php_reactor_fd *ev_set = (php_reactor_fd*) data;
if (ev_set->cb_read)
{
zval_ptr_dtor((ev_set->cb_read));
ev_set->cb_read = NULL;
}
if (ev_set->cb_write)
{
zval_ptr_dtor((ev_set->cb_write));
ev_set->cb_write = NULL;
}
if (ev_set->socket)
{
zval_ptr_dtor((ev_set->socket));
ev_set->socket = NULL;
}
efree(ev_set);
}
static void free_callback(void* data)
{
php_defer_callback *cb = (php_defer_callback *) data;
zval_ptr_dtor(cb->callback);
efree(cb);
}
static int php_swoole_event_onRead(swReactor *reactor, swEvent *event)
{
zval *retval = NULL;
zval args[1];
php_reactor_fd *fd = event->socket->object;
args[0] = *fd->socket;
if (sw_call_user_function_ex(EG(function_table), NULL, fd->cb_read, &retval, 1, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: onRead handler error.");
SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd);
return SW_ERR;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
return SW_OK;
}
static int php_swoole_event_onWrite(swReactor *reactor, swEvent *event)
{
zval *retval = NULL;
zval args[1];
php_reactor_fd *fd = event->socket->object;
if (!fd->cb_write)
{
return swReactor_onWrite(reactor, event);
}
args[0] = *fd->socket;
if (sw_call_user_function_ex(EG(function_table), NULL, fd->cb_write, &retval, 1, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: onWrite handler error");
SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd);
return SW_ERR;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
return SW_OK;
}
static int php_swoole_event_onError(swReactor *reactor, swEvent *event)
{
int error;
socklen_t len = sizeof(error);
if (getsockopt(event->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event->onError[1]: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno);
}
if (error != 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event->onError[1]: socket error. Error: %s [%d]", strerror(error), error);
}
efree(event->socket->object);
event->socket->active = 0;
SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd);
return SW_OK;
}
void php_swoole_event_onDefer(void *_cb)
{
php_defer_callback *defer = _cb;
zval _retval, *retval = &_retval;
if (sw_call_user_function_ex(EG(function_table), NULL, defer->callback, &retval, 0, NULL, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: defer handler error");
return;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
zval_ptr_dtor(defer->callback);
efree(defer);
}
static void php_swoole_event_onEndCallback(void *_cb)
{
php_defer_callback *defer = _cb;
zval *retval = NULL;
if (sw_call_user_function_ex(EG(function_table), NULL, defer->callback, &retval, 0, NULL, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: cycle callback handler error.");
return;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
}
void php_swoole_reactor_init()
{
if (!SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "async-io must be used in PHP CLI mode.");
return;
}
if (SwooleG.serv && swIsTaskWorker() && SwooleG.serv->task_async == 0)
{
swoole_php_fatal_error(E_ERROR, "Unable to use async-io in task processes, please set `task_async` to true.");
return;
}
if (SwooleG.main_reactor == NULL)
{
swTraceLog(SW_TRACE_PHP, "init reactor");
SwooleG.main_reactor = (swReactor *) sw_malloc(sizeof(swReactor));
if (SwooleG.main_reactor == NULL)
{
swoole_php_fatal_error(E_ERROR, "malloc failed.");
return;
}
if (swReactor_create(SwooleG.main_reactor, SW_REACTOR_MAXEVENTS) < 0)
{
swoole_php_fatal_error(E_ERROR, "failed to create reactor.");
return;
}
SwooleG.main_reactor->can_exit = php_coroutine_reactor_can_exit;
//client, swoole_event_exit will set swoole_running = 0
SwooleWG.in_client = 1;
SwooleWG.reactor_wait_onexit = 1;
SwooleWG.reactor_ready = 0;
//only client side
php_swoole_register_shutdown_function_prepend("swoole_event_wait");
}
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_READ, php_swoole_event_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_WRITE, php_swoole_event_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_ERROR, php_swoole_event_onError);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_WRITE, swReactor_onWrite);
SwooleWG.reactor_init = 1;
}
void php_swoole_event_wait()
{
if (SwooleWG.in_client == 1 && SwooleWG.reactor_ready == 0 && SwooleG.running)
{
if (PG(last_error_message))
{
switch (PG(last_error_type))
{
case E_ERROR:
case E_CORE_ERROR:
case E_USER_ERROR:
case E_COMPILE_ERROR:
return;
default:
break;
}
}
SwooleWG.reactor_ready = 1;
#ifdef HAVE_SIGNALFD
if (SwooleG.main_reactor->check_signalfd)
{
swSignalfd_setup(SwooleG.main_reactor);
}
#endif
if (!swReactor_empty(SwooleG.main_reactor))
{
SW_DECLARE_EG_SCOPE(scope);
SW_SAVE_EG_SCOPE(scope);
int ret = SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL);
if (ret < 0)
{
swoole_php_fatal_error(E_ERROR, "reactor wait failed. Error: %s [%d]", strerror(errno), errno);
}
SW_SET_EG_SCOPE(scope);
}
php_swoole_clear_all_timer();
SwooleWG.reactor_exit = 1;
SwooleG.running = 0;
SwooleG.main_reactor->running = 0;
}
}
void php_swoole_event_exit()
{
if (SwooleWG.in_client == 1)
{
if (SwooleG.main_reactor)
{
SwooleG.main_reactor->running = 0;
}
SwooleG.running = 0;
}
}
int swoole_convert_to_fd(zval *zfd)
{
php_stream *stream;
int socket_fd;
#ifdef SWOOLE_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
if (Z_TYPE_P(zfd) == IS_RESOURCE)
{
if (SW_ZEND_FETCH_RESOURCE_NO_RETURN(stream, php_stream *, &zfd, -1, NULL, php_file_le_stream()))
{
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void* )&socket_fd, 1) != SUCCESS || socket_fd < 0)
{
return SW_ERR;
}
}
else
{
#ifdef SWOOLE_SOCKETS_SUPPORT
if (SW_ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, &zfd, -1, NULL, php_sockets_le_socket()))
{
socket_fd = php_sock->bsd_socket;
}
else
{
swoole_php_fatal_error(E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource");
return SW_ERR;
}
#else
swoole_php_fatal_error(E_WARNING, "fd argument must be valid PHP stream resource");
return SW_ERR;
#endif
}
}
else if (Z_TYPE_P(zfd) == IS_LONG)
{
socket_fd = Z_LVAL_P(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "invalid file descriptor passed");
return SW_ERR;
}
}
else if (Z_TYPE_P(zfd) == IS_OBJECT)
{
zval *zsock = NULL;
if (instanceof_function(Z_OBJCE_P(zfd), swoole_client_ce_ptr))
{
zsock = sw_zend_read_property(Z_OBJCE_P(zfd), zfd, ZEND_STRL("sock"), 0);
}
else if (instanceof_function(Z_OBJCE_P(zfd), swoole_process_ce_ptr))
{
zsock = sw_zend_read_property(Z_OBJCE_P(zfd), zfd, ZEND_STRL("pipe"), 0);
}
if (zsock == NULL || ZVAL_IS_NULL(zsock))
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client or swoole_process.");
return -1;
}
socket_fd = Z_LVAL_P(zsock);
}
else
{
return SW_ERR;
}
return socket_fd;
}
int swoole_convert_to_fd_ex(zval *zfd, int *async)
{
php_stream *stream;
int fd;
#ifdef SWOOLE_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
if (Z_TYPE_P(zfd) != IS_RESOURCE)
{
swoole_php_fatal_error(E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource");
return SW_ERR;
}
if (SW_ZEND_FETCH_RESOURCE_NO_RETURN(stream, php_stream *, &zfd, -1, NULL, php_file_le_stream()))
{
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void* )&fd, 1) == SUCCESS && fd >= 0)
{
*async = stream->wrapper->wops == php_plain_files_wrapper.wops ? 0 : 1;
return fd;
}
}
#ifdef SWOOLE_SOCKETS_SUPPORT
else if (SW_ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, &zfd, -1, NULL, php_sockets_le_socket()))
{
fd = php_sock->bsd_socket;
*async = 1;
}
#endif
swoole_php_fatal_error(E_WARNING, "invalid file descriptor passed");
return SW_ERR;
}
#ifdef SWOOLE_SOCKETS_SUPPORT
php_socket* swoole_convert_to_socket(int sock)
{
php_socket *socket_object = emalloc(sizeof *socket_object);
bzero(socket_object, sizeof(php_socket));
socket_object->bsd_socket = sock;
socket_object->blocking = 1;
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
if (getsockname(sock, (struct sockaddr*) &addr, &addr_len) == 0)
{
socket_object->type = addr.ss_family;
}
else
{
swoole_php_sys_error(E_WARNING, "unable to obtain socket family");
error:
efree(socket_object);
return NULL;
}
int t = fcntl(sock, F_GETFL);
if (t == -1)
{
swoole_php_sys_error(E_WARNING, "unable to obtain blocking state");
goto error;
}
else
{
socket_object->blocking = !(t & O_NONBLOCK);
}
return socket_object;
}
#endif
PHP_FUNCTION(swoole_event_add)
{
zval *cb_read = NULL;
zval *cb_write = NULL;
zval *zfd;
char *func_name = NULL;
long event_flag = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|zzl", &zfd, &cb_read, &cb_write, &event_flag) == FAILURE)
{
RETURN_FALSE;
}
if ((cb_read == NULL && cb_write == NULL) || (ZVAL_IS_NULL(cb_read) && ZVAL_IS_NULL(cb_write)))
{
swoole_php_fatal_error(E_WARNING, "no read or write event callback.");
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "unknow type.");
RETURN_FALSE;
}
if (socket_fd == 0 && (event_flag & SW_EVENT_WRITE))
{
swoole_php_fatal_error(E_WARNING, "invalid socket fd [%d].", socket_fd);
RETURN_FALSE;
}
php_reactor_fd *reactor_fd = emalloc(sizeof(php_reactor_fd));
reactor_fd->socket = zfd;
sw_copy_to_stack(reactor_fd->socket, reactor_fd->stack.socket);
Z_TRY_ADDREF_P(reactor_fd->socket);
if (cb_read!= NULL && !ZVAL_IS_NULL(cb_read))
{
if (!sw_zend_is_callable(cb_read, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
efree(func_name);
reactor_fd->cb_read = cb_read;
Z_TRY_ADDREF_P(cb_read);
sw_copy_to_stack(reactor_fd->cb_read, reactor_fd->stack.cb_read);
}
else
{
reactor_fd->cb_read = NULL;
}
if (cb_write!= NULL && !ZVAL_IS_NULL(cb_write))
{
if (!sw_zend_is_callable(cb_write, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
efree(func_name);
reactor_fd->cb_write = cb_write;
Z_TRY_ADDREF_P(cb_write);
sw_copy_to_stack(reactor_fd->cb_write, reactor_fd->stack.cb_write);
}
else
{
reactor_fd->cb_write = NULL;
}
php_swoole_check_reactor();
swSetNonBlock(socket_fd); //must be nonblock
if (SwooleG.main_reactor->add(SwooleG.main_reactor, socket_fd, SW_FD_USER | event_flag) < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event_add failed.");
RETURN_FALSE;
}
swConnection *socket = swReactor_get(SwooleG.main_reactor, socket_fd);
socket->object = reactor_fd;
socket->active = 1;
socket->nonblock = 1;
RETURN_LONG(socket_fd);
}
PHP_FUNCTION(swoole_event_write)
{
zval *zfd;
char *data;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &zfd, &data, &len) == FAILURE)
{
RETURN_FALSE;
}
if (len <= 0)
{
swoole_php_fatal_error(E_WARNING, "data empty.");
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "unknow type.");
RETURN_FALSE;
}
php_swoole_check_reactor();
if (SwooleG.main_reactor->write(SwooleG.main_reactor, socket_fd, data, len) < 0)
{
RETURN_FALSE;
}
else
{
RETURN_TRUE;
}
}
PHP_FUNCTION(swoole_event_set)
{
zval *cb_read = NULL;
zval *cb_write = NULL;
zval *zfd;
char *func_name = NULL;
long event_flag = 0;
if (!SwooleG.main_reactor)
{
swoole_php_fatal_error(E_WARNING, "reactor no ready, cannot swoole_event_set.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|zzl", &zfd, &cb_read, &cb_write, &event_flag) == FAILURE)
{
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "unknow type.");
RETURN_FALSE;
}
swConnection *socket = swReactor_get(SwooleG.main_reactor, socket_fd);
if (!socket->active)
{
swoole_php_fatal_error(E_WARNING, "socket[%d] is not found in the reactor.", socket_fd);
efree(func_name);
RETURN_FALSE;
}
php_reactor_fd *ev_set = socket->object;
if (cb_read != NULL && !ZVAL_IS_NULL(cb_read))
{
if (!sw_zend_is_callable(cb_read, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
else
{
if (ev_set->cb_read)
{
zval_ptr_dtor(ev_set->cb_read);
}
ev_set->cb_read = cb_read;
Z_TRY_ADDREF_P(cb_read);
sw_copy_to_stack(ev_set->cb_read, ev_set->stack.cb_read);
efree(func_name);
}
}
if (cb_write != NULL && !ZVAL_IS_NULL(cb_write))
{
if (socket_fd == 0 && (event_flag & SW_EVENT_WRITE))
{
swoole_php_fatal_error(E_WARNING, "invalid socket fd [%d].", socket_fd);
RETURN_FALSE;
}
if (!sw_zend_is_callable(cb_write, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
else
{
if (ev_set->cb_write)
{
zval_ptr_dtor(ev_set->cb_write);
}
ev_set->cb_write = cb_write;
Z_TRY_ADDREF_P(cb_write);
sw_copy_to_stack(ev_set->cb_write, ev_set->stack.cb_write);
efree(func_name);
}
}
if ((event_flag & SW_EVENT_READ) && ev_set->cb_read == NULL)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: no read callback.");
RETURN_FALSE;
}
if ((event_flag & SW_EVENT_WRITE) && ev_set->cb_write == NULL)
{
swoole_php_fatal_error(E_WARNING, "swoole_event: no write callback.");
RETURN_FALSE;
}
if (SwooleG.main_reactor->set(SwooleG.main_reactor, socket_fd, SW_FD_USER | event_flag) < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event_set failed.");
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(swoole_event_del)
{
zval *zfd;
if (!SwooleG.main_reactor)
{
swoole_php_fatal_error(E_WARNING, "reactor no ready, cannot swoole_event_del.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zfd) == FAILURE)
{
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "unknow type.");
RETURN_FALSE;
}
swConnection *socket = swReactor_get(SwooleG.main_reactor, socket_fd);
if (socket->object)
{
SwooleG.main_reactor->defer(SwooleG.main_reactor, free_event_callback, socket->object);
socket->object = NULL;
}
int ret = SwooleG.main_reactor->del(SwooleG.main_reactor, socket_fd);
socket->active = 0;
SW_CHECK_RETURN(ret);
}
PHP_FUNCTION(swoole_event_defer)
{
zval *callback;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callback) == FAILURE)
{
RETURN_FALSE;
}
char *func_name;
if (!sw_zend_is_callable(callback, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
efree(func_name);
php_swoole_check_reactor();
php_defer_callback *defer = emalloc(sizeof(php_defer_callback));
defer->callback = &defer->_callback;
memcpy(defer->callback, callback, sizeof(zval));
Z_TRY_ADDREF_P(callback);
SW_CHECK_RETURN(SwooleG.main_reactor->defer(SwooleG.main_reactor, php_swoole_event_onDefer, defer));
}
PHP_FUNCTION(swoole_event_cycle)
{
if (!SwooleG.main_reactor)
{
swoole_php_fatal_error(E_WARNING, "reactor no ready, cannot swoole_event_defer.");
RETURN_FALSE;
}
zval *callback;
zend_bool before = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &callback, &before) == FAILURE)
{
RETURN_FALSE;
}
if (ZVAL_IS_NULL(callback))
{
if (SwooleG.main_reactor->idle_task.callback == NULL)
{
RETURN_FALSE;
}
else
{
SwooleG.main_reactor->defer(SwooleG.main_reactor, free_callback, SwooleG.main_reactor->idle_task.data);
SwooleG.main_reactor->idle_task.callback = NULL;
SwooleG.main_reactor->idle_task.data = NULL;
RETURN_TRUE;
}
}
char *func_name;
if (!sw_zend_is_callable(callback, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
RETURN_FALSE;
}
efree(func_name);
php_defer_callback *cb = emalloc(sizeof(php_defer_callback));
cb->callback = &cb->_callback;
memcpy(cb->callback, callback, sizeof(zval));
Z_TRY_ADDREF_P(callback);
if (before == 0)
{
if (SwooleG.main_reactor->idle_task.data != NULL)
{
SwooleG.main_reactor->defer(SwooleG.main_reactor, free_callback, SwooleG.main_reactor->idle_task.data);
}
SwooleG.main_reactor->idle_task.callback = php_swoole_event_onEndCallback;
SwooleG.main_reactor->idle_task.data = cb;
}
else
{
if (SwooleG.main_reactor->future_task.data != NULL)
{
SwooleG.main_reactor->defer(SwooleG.main_reactor, free_callback, SwooleG.main_reactor->future_task.data);
}
SwooleG.main_reactor->future_task.callback = php_swoole_event_onEndCallback;
SwooleG.main_reactor->future_task.data = cb;
//Registration onBegin callback function
swReactor_activate_future_task(SwooleG.main_reactor);
}
RETURN_TRUE;
}
PHP_FUNCTION(swoole_event_exit)
{
php_swoole_event_exit();
}
PHP_FUNCTION(swoole_event_wait)
{
if (!SwooleG.main_reactor)
{
return;
}
php_swoole_event_wait();
}
PHP_FUNCTION(swoole_event_dispatch)
{
if (!SwooleG.main_reactor)
{
RETURN_FALSE;
}
SwooleG.main_reactor->once = 1;
#ifdef HAVE_SIGNALFD
if (SwooleG.main_reactor->check_signalfd)
{
swSignalfd_setup(SwooleG.main_reactor);
}
#endif
int ret = SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL);
if (ret < 0)
{
swoole_php_fatal_error(E_ERROR, "reactor wait failed. Error: %s [%d]", strerror(errno), errno);
}
SwooleG.main_reactor->once = 0;
RETURN_TRUE;
}
PHP_FUNCTION(swoole_event_isset)
{
if (!SwooleG.main_reactor)
{
RETURN_FALSE;
}
zval *zfd;
long events = SW_EVENT_READ | SW_EVENT_WRITE;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &zfd, &events) == FAILURE)
{
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(zfd);
if (socket_fd < 0)
{
swoole_php_fatal_error(E_WARNING, "unknow type.");
RETURN_FALSE;
}
swConnection *_socket = swReactor_get(SwooleG.main_reactor, socket_fd);
if (_socket == NULL || _socket->removed)
{
RETURN_FALSE;
}
if (_socket->events & events)
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}