forked from expressif/pecl-event-libevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libevent.c
1745 lines (1462 loc) · 41.7 KB
/
libevent.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP 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: Antony Dovgal <[email protected]> |
| Arnaud Le Blanc <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_streams.h"
#include "php_network.h"
#include "php_libevent.h"
#include <signal.h>
#if PHP_VERSION_ID >= 50301 && (HAVE_SOCKETS || defined(COMPILE_DL_SOCKETS))
# include "ext/sockets/php_sockets.h"
# define LIBEVENT_SOCKETS_SUPPORT
#endif
#ifdef PHP_WIN32
/* XXX compiling with 2.x on Windows. Luckily the ext code works thanks to the
compat exports from the libevent. However it might need to be adapted to the
never version, so this ifdefs would go away. */
# include <event2/event.h>
# include <event2/event_compat.h>
# include <event2/event_struct.h>
# include <event2/bufferevent.h>
# include <event2/bufferevent_compat.h>
#else
# include <event.h>
#endif
static int le_event_base;
static int le_event;
static int le_bufferevent;
#ifdef COMPILE_DL_LIBEVENT
ZEND_GET_MODULE(libevent)
#endif
typedef struct _php_event_base_t { /* {{{ */
struct event_base *base;
zval *rsrc_id;
zend_uintptr_t events;
} php_event_base_t;
/* }}} */
typedef struct _php_event_callback_t { /* {{{ */
zval func;
zval arg;
} php_event_callback_t;
/* }}} */
typedef struct _php_event_t { /* {{{ */
struct event *event;
zval *rsrc_id;
zval stream_id;
php_event_base_t *base;
php_event_callback_t *callback;
#ifdef ZTS
void ***thread_ctx;
#endif
} php_event_t;
/* }}} */
typedef struct _php_bufferevent_t { /* {{{ */
struct bufferevent *bevent;
zval *rsrc_id;
php_event_base_t *base;
zval readcb;
zval writecb;
zval errorcb;
zval arg;
#ifdef ZTS
void ***thread_ctx;
#endif
} php_bufferevent_t;
/* }}} */
#define ZVAL_TO_BASE(zval) \
(php_event_base_t *)zend_fetch_resource2_ex(zval, "event base", le_event_base, le_event_base)
#define ZVAL_TO_EVENT(zval) \
(php_event_t *)zend_fetch_resource2_ex(zval, "event", le_event, le_event)
#define ZVAL_TO_BEVENT(zval) \
(php_bufferevent_t *)zend_fetch_resource2_ex(zval, "buffer event", le_bufferevent, le_bufferevent)
/* {{{ internal funcs */
static inline void _php_event_callback_free(php_event_callback_t *callback) /* {{{ */
{
if (!callback) {
return;
}
zval_ptr_dtor(&callback->func);
zval_ptr_dtor(&callback->arg);
safe_efree(callback);
}
/* }}} */
ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */
{
if (!res || !res->ptr) {
return;
}
php_event_base_t *base = (php_event_base_t*)res->ptr;
if (!base)
return;
if (base->base)
event_base_free(base->base);
safe_efree(base);
}
/* }}} */
ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */
{
if (!res || !res->ptr) {
return;
}
php_event_t *event = (php_event_t*)res->ptr;
zval *base_id = NULL;
if (!event) return;
if (event->base) {
base_id = event->base->rsrc_id;
--event->base->events;
}
if (Z_TYPE_P(&event->stream_id) != IS_NULL) {
zend_list_delete(Z_RES_P(&event->stream_id));
}
_php_event_callback_free(event->callback);
event_del(event->event);
safe_efree(event->event);
safe_efree(event);
if (base_id) {
zend_list_delete(Z_RES_P(base_id));
}
}
/* }}} */
ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */
{
if (!res || !res->ptr) {
return;
}
php_bufferevent_t *bevent = (php_bufferevent_t*)res->ptr;
zval *base_id = NULL;
if (!bevent)
return;
zval_ptr_dtor(&bevent->readcb);
zval_ptr_dtor(&bevent->writecb);
zval_ptr_dtor(&bevent->errorcb);
zval_ptr_dtor(&bevent->arg);
bufferevent_free(bevent->bevent);
if (bevent->base) {
base_id = bevent->base->rsrc_id;
--bevent->base->events;
if (base_id) {
zend_list_delete(Z_RES_P(base_id));
}
}
safe_efree(bevent);
}
/* }}} */
static void _php_event_callback(int fd, short events, void *arg) /* {{{ */
{
zval args[3];
php_event_t *event = (php_event_t *)arg;
php_event_callback_t *callback;
zval retval;
TSRMLS_FETCH_FROM_CTX(event ? event->thread_ctx : NULL);
if (!event || !event->callback || !event->base) {
return;
}
callback = event->callback;
if (Z_TYPE_P(&event->stream_id) != IS_NULL) {
args[0] = event->stream_id;
Z_ADDREF_P(&args[0]); /* we do refcount-- later in zval_ptr_dtor */
} else if (events & EV_SIGNAL) {
ZVAL_LONG(&args[0], (zend_long)fd);
} else {
ZVAL_NULL(&args[0]);
}
ZVAL_LONG(&args[1], (zend_long)events);
args[2] = callback->arg;
if (Z_TYPE_P(&args[2]) != IS_NULL) {
Z_TRY_ADDREF_P(&args[2]);
}
if (call_user_function(EG(function_table), NULL, &callback->func, &retval, 3, args) == SUCCESS) {
zval_dtor(&retval);
}
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[2]);
}
/* }}} */
static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */
{
zval args[2];
zval retval;
php_bufferevent_t *bevent = (php_bufferevent_t *)arg;
TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL);
if (!bevent || !bevent->base || Z_ISUNDEF(bevent->readcb)) {
return;
}
ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
args[1] = bevent->arg;
if (Z_TYPE_P(&args[1]) != IS_NULL) {
Z_TRY_ADDREF_P(&args[1]);
}
if (call_user_function(EG(function_table), NULL, &bevent->readcb, &retval, 2, args) == SUCCESS) {
zval_dtor(&retval);
}
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]);
}
/* }}} */
static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ */
{
zval args[2];
zval retval;
php_bufferevent_t *bevent = (php_bufferevent_t *)arg;
TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL);
if (!bevent || !bevent->base || Z_ISUNDEF(bevent->writecb)) {
return;
}
ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
args[1] = bevent->arg;
if (Z_TYPE_P(&args[1]) != IS_NULL) {
Z_TRY_ADDREF_P(&args[1]);
}
if (call_user_function(EG(function_table), NULL, &bevent->writecb, &retval, 2, args) == SUCCESS) {
zval_dtor(&retval);
}
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]);
}
/* }}} */
static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *arg) /* {{{ */
{
zval args[3];
zval retval;
int args_size = 2;
php_bufferevent_t *bevent = (php_bufferevent_t *)arg;
TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL);
if (!bevent || !bevent->base || Z_ISUNDEF(bevent->errorcb)) {
return;
}
if (!bevent->rsrc_id)
return;
ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
ZVAL_LONG(&args[1], (zend_long)what);
args[2] = bevent->arg;
if (Z_TYPE_P(&args[2]) != IS_NULL) {
Z_TRY_ADDREF_P(&args[2]);
}
if (call_user_function(EG(function_table), NULL, &bevent->errorcb, &retval, 3, args) == SUCCESS) {
zval_dtor(&retval);
}
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[2]);
}
/* }}} */
/* }}} */
/* {{{ proto resource event_base_new()
*/
static PHP_FUNCTION(event_base_new)
{
php_event_base_t *base;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
base = emalloc(sizeof(php_event_base_t));
base->rsrc_id = NULL;
base->base = event_base_new();
if (!base->base) {
safe_efree(base);
RETURN_FALSE;
}
base->events = 0;
base->rsrc_id = zend_list_insert(base, le_event_base);
ZVAL_COPY_VALUE(return_value, base->rsrc_id);
Z_ADDREF_P(base->rsrc_id);
}
/* }}} */
/* {{{ proto bool event_base_reinit()
*/
static PHP_FUNCTION(event_base_reinit) {
zval *zbase;
php_event_base_t *base;
int r = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) {
return;
}
if (!(base = ZVAL_TO_BASE(zbase)))
RETURN_FALSE;
r = event_reinit(base->base);
if (r == -1) {
RETURN_FALSE
} else {
RETURN_TRUE;
}
}
/* }}} */
/* {{{ proto void event_base_free(resource base)
* return type is defined void at http://php.net/manual/en/function.event-base-free.php
*/
static PHP_FUNCTION(event_base_free)
{
zval *zbase;
php_event_base_t *base;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) {
return;
}
if (!(base = ZVAL_TO_BASE(zbase)))
return;
if (base->events > 0) {
php_error_docref(NULL, E_WARNING, "base has events attached to it and cannot be freed");
RETURN_FALSE;
}
zend_list_close(Z_RES_P(base->rsrc_id));
}
/* }}} */
/* {{{ proto int event_base_loop(resource base[, int flags])
*/
static PHP_FUNCTION(event_base_loop)
{
zval *zbase;
php_event_base_t *base;
zend_long flags = 0;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zbase, &flags) != SUCCESS) {
return;
}
base = ZVAL_TO_BASE(zbase);
Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */
ret = event_base_loop(base->base, (int)flags);
RETURN_LONG(ret);
}
/* }}} */
/* {{{ proto bool event_base_loopbreak(resource base)
*/
static PHP_FUNCTION(event_base_loopbreak)
{
zval *zbase;
php_event_base_t *base;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) {
return;
}
base = ZVAL_TO_BASE(zbase);
ret = event_base_loopbreak(base->base);
if (ret == 0) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool event_base_loopexit(resource base[, int timeout])
*/
static PHP_FUNCTION(event_base_loopexit)
{
zval *zbase;
php_event_base_t *base;
int ret;
zend_long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zbase, &timeout) != SUCCESS) {
return;
}
if (!(base = ZVAL_TO_BASE(zbase)))
RETURN_FALSE;
if (timeout < 0) {
ret = event_base_loopexit(base->base, NULL);
} else {
struct timeval time;
time.tv_usec = timeout % 1000000;
time.tv_sec = (long)timeout / 1000000;
ret = event_base_loopexit(base->base, &time);
}
if (ret == 0) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool event_base_set(resource event, resource base)
*/
static PHP_FUNCTION(event_base_set)
{
zval *zbase, *zevent;
php_event_base_t *base, *old_base;
php_event_t *event;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &zevent, &zbase) != SUCCESS) {
return;
}
if (!(base = ZVAL_TO_BASE(zbase)))
RETURN_FALSE;
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
old_base = event->base;
ret = event_base_set(base->base, event->event);
if (ret == 0) {
if (base != old_base) {
/* make sure the base is destroyed after the event */
Z_ADDREF_P(base->rsrc_id);
++base->events;
/* deference the event from the old base */
if (old_base) {
--old_base->events;
if (old_base->rsrc_id != NULL) {
zend_list_delete(Z_RES_P(old_base->rsrc_id));
}
}
}
event->base = base;
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool event_base_priority_init(resource base, int npriorities)
*/
static PHP_FUNCTION(event_base_priority_init)
{
zval *zbase;
php_event_base_t *base;
zend_long npriorities;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbase, &npriorities) != SUCCESS) {
return;
}
if (!(base = ZVAL_TO_BASE(zbase)))
RETURN_FALSE;
if (npriorities < 0) {
php_error_docref(NULL, E_WARNING, "npriorities cannot be less than zero");
RETURN_FALSE;
}
ret = event_base_priority_init(base->base, npriorities);
if (ret == 0) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto resource event_new()
*/
static PHP_FUNCTION(event_new)
{
php_event_t *event;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
event = emalloc(sizeof(php_event_t));
event->event = ecalloc(1, sizeof(struct event));
event->rsrc_id = NULL;
event->callback = NULL;
event->base = NULL;
ZVAL_NULL(&event->stream_id);
TSRMLS_SET_CTX(event->thread_ctx);
event->rsrc_id = zend_list_insert(event, le_event);
ZVAL_COPY_VALUE(return_value, event->rsrc_id);
}
/* }}} */
/* {{{ proto void event_free(resource event)
*/
static PHP_FUNCTION(event_free)
{
zval *zevent;
php_event_t *event;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zevent) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
return;
if (event->base) {
--event->base->events;
if (event->base->rsrc_id) {
zend_list_delete(Z_RES_P(event->base->rsrc_id));
}
event->base = NULL;
}
event_del (event->event);
zend_list_delete(Z_RES_P(event->rsrc_id));
}
/* }}} */
/* {{{ proto bool event_add(resource event[, int timeout])
*/
static PHP_FUNCTION(event_add)
{
zval *zevent;
php_event_t *event;
int ret;
zend_long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zevent, &timeout) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (!event->base) {
php_error_docref(NULL, E_WARNING, "Unable to add event without an event base");
RETURN_FALSE;
}
if (timeout < 0) {
ret = event_add(event->event, NULL);
} else {
struct timeval time;
time.tv_usec = timeout % 1000000;
time.tv_sec = timeout / 1000000;
ret = event_add(event->event, &time);
}
if (ret != 0) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool event_set(resource event, mixed fd, int events, mixed callback[, mixed arg])
*/
static PHP_FUNCTION(event_set)
{
zval *zevent, *fd, *zcallback, *zarg = NULL;
php_event_t *event;
zend_long events = 0;
php_event_callback_t *callback, *old_callback;
zend_string *func_name;
php_stream *stream = NULL;
php_socket_t file_desc;
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rzlz|z", &zevent, &fd, &events, &zcallback, &zarg) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (events & EV_TIMEOUT) {
file_desc = -1;
fd = 0;
events = 0;
} else if (events & EV_SIGNAL) {
/* signal support */
convert_to_long_ex(fd);
file_desc = Z_LVAL_P(fd);
if (file_desc < 0 || file_desc >= NSIG) {
php_error_docref(NULL, E_WARNING, "invalid signal passed");
RETURN_FALSE;
}
} else {
if (Z_TYPE_P(fd) == IS_LONG) {
fd = zend_hash_index_find(&EG(regular_list), Z_RES_P(fd));
if (!fd) {
php_error_docref(NULL, E_WARNING, "invalid file descriptor passed");
RETURN_FALSE;
}
}
if (Z_TYPE_P(fd) == IS_RESOURCE) {
stream = zend_fetch_resource2_ex(fd, NULL, php_file_le_stream(), php_file_le_pstream());
if (stream) {
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) {
RETURN_FALSE;
}
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_sock = (php_socket *)zend_fetch_resource_ex(fd, NULL, php_sockets_le_socket());
if (php_sock) {
file_desc = php_sock->bsd_socket;
} else {
php_error_docref(NULL, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource");
RETURN_FALSE;
}
#else
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource");
RETURN_FALSE;
#endif
}
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long");
#else
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long");
#endif
RETURN_FALSE;
}
}
if (!zend_is_callable(zcallback, 0, &func_name)) {
php_error_docref(NULL, E_WARNING, "'%s' is not a valid callback", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
callback = emalloc(sizeof(php_event_callback_t));
ZVAL_COPY(&callback->func, zcallback);
if(zarg) {
ZVAL_COPY(&callback->arg, zarg);
} else {
ZVAL_NULL(&callback->arg);
}
old_callback = event->callback;
event->callback = callback;
if (!fd) {
if (Z_TYPE_P(&event->stream_id) != IS_NULL) {
zend_list_close(Z_RES_P(&event->stream_id));
ZVAL_NULL(&event->stream_id);
}
} else if (events & EV_SIGNAL) {
ZVAL_NULL(&event->stream_id);
} else {
ZVAL_COPY(&event->stream_id, fd);
}
event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event);
if (old_callback) {
_php_event_callback_free(old_callback);
}
if (fd && event->base) {
ret = event_base_set(event->base->base, event->event);
if (ret != 0) {
RETURN_FALSE;
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool event_del(resource event)
*/
static PHP_FUNCTION(event_del)
{
zval *zevent;
php_event_t *event;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zevent) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (!event->base) {
php_error_docref(NULL, E_WARNING, "Unable to delete event without an event base");
RETURN_FALSE;
}
if (event_del(event->event) == 0) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool event_priority_set(resource event, int priority)
*/
static PHP_FUNCTION(event_priority_set)
{
zval *zevent;
php_event_t *event;
zend_long priority = 0;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zevent, &priority) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (!event->base) {
php_error_docref(NULL, E_WARNING, "Unable to set event priority without an event base");
RETURN_FALSE;
}
ret = event_priority_set(event->event, priority);
if (ret == 0) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool event_timer_set(resource event, mixed callback[, mixed arg])
*/
static PHP_FUNCTION(event_timer_set)
{
zval *zevent, *zcallback, *zarg = NULL;
php_event_t *event;
php_event_callback_t *callback, *old_callback;
zend_string *func_name;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|z", &zevent, &zcallback, &zarg) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (!zend_is_callable(zcallback, 0, &func_name)) {
php_error_docref(NULL, E_WARNING, "'%s' is not a valid callback", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
callback = emalloc(sizeof(php_event_callback_t));
ZVAL_COPY(&callback->func, zcallback);
if (zarg) {
ZVAL_COPY(&callback->arg, zarg);
}
else {
ZVAL_NULL(&callback->arg);
}
old_callback = event->callback;
event->callback = callback;
if (Z_TYPE_P(&event->stream_id) != IS_NULL) {
zend_list_close(Z_RES_P(&event->stream_id));
ZVAL_NULL(&event->stream_id);
}
event_set(event->event, -1, 0, _php_event_callback, event);
if (old_callback) {
_php_event_callback_free(old_callback);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool event_timer_pending(resource event[, int timeout])
*/
static PHP_FUNCTION(event_timer_pending)
{
zval *zevent;
php_event_t *event;
int ret;
zend_long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zevent, &timeout) != SUCCESS) {
return;
}
if (!(event = ZVAL_TO_EVENT(zevent)))
RETURN_FALSE;
if (timeout < 0) {
ret = event_pending(event->event, EV_TIMEOUT, NULL);
} else {
struct timeval time;
time.tv_usec = timeout % 1000000;
time.tv_sec = timeout / 1000000;
ret = event_pending(event->event, EV_TIMEOUT, &time);
}
if (ret != 0) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto resource event_buffer_new(mixed fd, mixed readcb, mixed writecb, mixed errorcb[, mixed arg])
*/
static PHP_FUNCTION(event_buffer_new)
{
php_bufferevent_t *bevent;
php_stream *stream;
zval *zfd, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL;
php_socket_t fd;
zend_string *func_name;
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzzz|z", &zfd, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) {
return;
}
if (Z_TYPE_P(zfd) == IS_LONG) {
zfd = zend_hash_index_find(&EG(regular_list), Z_RES_P(zfd));
if (!zfd) {
php_error_docref(NULL, E_WARNING, "invalid file descriptor passed");
RETURN_FALSE;
}
}
if (Z_TYPE_P(zfd) == IS_RESOURCE) {
stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream());
if (stream) {
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) {
RETURN_FALSE;
}
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_sock = (php_socket *)zend_fetch_resource_ex(zfd, NULL, php_sockets_le_socket());
if (php_sock) {
fd = php_sock->bsd_socket;
} else {
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long");
RETURN_FALSE;
}
#else
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long");
RETURN_FALSE;
#endif
}
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long");
#else
php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long");
#endif
RETURN_FALSE;
}
if (Z_TYPE_P(zreadcb) != IS_NULL) {
if (!zend_is_callable(zreadcb, 0, &func_name)) {
php_error_docref(NULL, E_WARNING, "'%s' is not a valid read callback", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
} else {
zreadcb = NULL;
}
if (Z_TYPE_P(zwritecb) != IS_NULL) {
if (!zend_is_callable(zwritecb, 0, &func_name)) {
php_error_docref(NULL, E_WARNING, "'%s' is not a valid write callback", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
} else {
zwritecb = NULL;
}
if (!zend_is_callable(zerrorcb, 0, &func_name)) {
php_error_docref(NULL, E_WARNING, "'%s' is not a valid error callback", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
bevent = emalloc(sizeof(php_bufferevent_t));
bevent->bevent = bufferevent_new(fd, _php_bufferevent_readcb, _php_bufferevent_writecb, _php_bufferevent_errorcb, bevent);
bevent->rsrc_id = NULL;
bevent->base = NULL;
if (zreadcb) {
ZVAL_COPY(&bevent->readcb, zreadcb);
}
if (zwritecb) {
ZVAL_COPY(&bevent->writecb, zwritecb);