forked from php/pecl-event-libevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libevent.c
1698 lines (1423 loc) · 41.2 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
#ifndef ZEND_FETCH_RESOURCE_NO_RETURN
# define ZEND_FETCH_RESOURCE_NO_RETURN(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \
(rsrc = (rsrc_type) zend_fetch_resource(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type))
#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
#if PHP_MAJOR_VERSION < 5
# ifdef PHP_WIN32
typedef SOCKET php_socket_t;
# else
typedef int php_socket_t;
# endif
# ifdef ZTS
# define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
# define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls
# else
# define TSRMLS_FETCH_FROM_CTX(ctx)
# define TSRMLS_SET_CTX(ctx)
# endif
# ifndef Z_ADDREF_P
# define Z_ADDREF_P(x) (x)->refcount++
# endif
#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;
int rsrc_id;
zend_uint 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;
int rsrc_id;
int stream_id;
php_event_base_t *base;
php_event_callback_t *callback;
#ifdef ZTS
void ***thread_ctx;
#endif
int in_free;
} php_event_t;
/* }}} */
typedef struct _php_bufferevent_t { /* {{{ */
struct bufferevent *bevent;
int 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, base) \
ZEND_FETCH_RESOURCE(base, php_event_base_t *, &zval, -1, "event base", le_event_base)
#define ZVAL_TO_EVENT(zval, event) \
ZEND_FETCH_RESOURCE(event, php_event_t *, &zval, -1, "event", le_event)
#define ZVAL_TO_BEVENT(zval, bevent) \
ZEND_FETCH_RESOURCE(bevent, php_bufferevent_t *, &zval, -1, "buffer event", le_bufferevent)
/* {{{ internal funcs */
static inline void _php_event_callback_free(php_event_callback_t *callback) /* {{{ */
{
if (!callback) {
return;
}
zval_ptr_dtor(&callback->func);
if (callback->arg) {
zval_ptr_dtor(&callback->arg);
}
efree(callback);
}
/* }}} */
static void _php_event_base_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
{
php_event_base_t *base = (php_event_base_t*)rsrc->ptr;
event_base_free(base->base);
efree(base);
}
/* }}} */
static void _php_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
{
php_event_t *event = (php_event_t*)rsrc->ptr;
int base_id = -1;
if (event->in_free) {
return;
}
event->in_free = 1;
if (event->base) {
base_id = event->base->rsrc_id;
--event->base->events;
}
if (event->stream_id >= 0) {
zend_list_delete(event->stream_id);
}
event_del(event->event);
_php_event_callback_free(event->callback);
efree(event->event);
efree(event);
if (base_id >= 0) {
zend_list_delete(base_id);
}
}
/* }}} */
static void _php_bufferevent_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
{
php_bufferevent_t *bevent = (php_bufferevent_t*)rsrc->ptr;
int base_id = -1;
if (bevent->base) {
base_id = bevent->base->rsrc_id;
--bevent->base->events;
}
if (bevent->readcb) {
zval_ptr_dtor(&(bevent->readcb));
}
if (bevent->writecb) {
zval_ptr_dtor(&(bevent->writecb));
}
if (bevent->errorcb) {
zval_ptr_dtor(&(bevent->errorcb));
}
if (bevent->arg) {
zval_ptr_dtor(&(bevent->arg));
}
bufferevent_free(bevent->bevent);
efree(bevent);
if (base_id >= 0) {
zend_list_delete(base_id);
}
}
/* }}} */
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;
MAKE_STD_ZVAL(args[0]);
if (event->stream_id >= 0) {
ZVAL_RESOURCE(args[0], event->stream_id);
zend_list_addref(event->stream_id);
} else if (events & EV_SIGNAL) {
ZVAL_LONG(args[0], fd);
} else {
ZVAL_NULL(args[0]);
}
MAKE_STD_ZVAL(args[1]);
ZVAL_LONG(args[1], events);
args[2] = callback->arg;
Z_ADDREF_P(callback->arg);
if (call_user_function(EG(function_table), NULL, callback->func, &retval, 3, args TSRMLS_CC) == 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 || !bevent->readcb) {
return;
}
MAKE_STD_ZVAL(args[0]);
ZVAL_RESOURCE(args[0], bevent->rsrc_id);
zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
args[1] = bevent->arg;
Z_ADDREF_P(args[1]);
if (call_user_function(EG(function_table), NULL, bevent->readcb, &retval, 2, args TSRMLS_CC) == 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 || !bevent->writecb) {
return;
}
MAKE_STD_ZVAL(args[0]);
ZVAL_RESOURCE(args[0], bevent->rsrc_id);
zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
args[1] = bevent->arg;
Z_ADDREF_P(args[1]);
if (call_user_function(EG(function_table), NULL, bevent->writecb, &retval, 2, args TSRMLS_CC) == 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;
php_bufferevent_t *bevent = (php_bufferevent_t *)arg;
TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL);
if (!bevent || !bevent->base || !bevent->errorcb) {
return;
}
MAKE_STD_ZVAL(args[0]);
ZVAL_RESOURCE(args[0], bevent->rsrc_id);
zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */
MAKE_STD_ZVAL(args[1]);
ZVAL_LONG(args[1], what);
args[2] = bevent->arg;
Z_ADDREF_P(args[2]);
if (call_user_function(EG(function_table), NULL, bevent->errorcb, &retval, 3, args TSRMLS_CC) == 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() TSRMLS_CC, "") != SUCCESS) {
return;
}
base = emalloc(sizeof(php_event_base_t));
base->base = event_base_new();
if (!base->base) {
efree(base);
RETURN_FALSE;
}
base->events = 0;
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
base->rsrc_id = zend_list_insert(base, le_event_base TSRMLS_CC);
#else
base->rsrc_id = zend_list_insert(base, le_event_base);
#endif
RETURN_RESOURCE(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() TSRMLS_CC, "r", &zbase) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
r = event_reinit(base->base);
if (r == -1) {
RETURN_FALSE
} else {
RETURN_TRUE;
}
}
/* }}} */
/* {{{ proto void event_base_free(resource base)
*/
static PHP_FUNCTION(event_base_free)
{
zval *zbase;
php_event_base_t *base;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zbase) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
if (base->events > 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "base has events attached to it and cannot be freed");
RETURN_FALSE;
}
zend_list_delete(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;
long flags = 0;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &flags) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
zend_list_addref(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */
ret = event_base_loop(base->base, flags);
zend_list_delete(base->rsrc_id);
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() TSRMLS_CC, "r", &zbase) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
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;
long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &timeout) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
if (timeout < 0) {
ret = event_base_loopexit(base->base, NULL);
} else {
struct timeval time;
time.tv_usec = timeout % 1000000;
time.tv_sec = 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() TSRMLS_CC, "rr", &zevent, &zbase) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
ZVAL_TO_EVENT(zevent, event);
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 */
zend_list_addref(base->rsrc_id);
++base->events;
}
if (old_base && base != old_base) {
--old_base->events;
zend_list_delete(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;
long npriorities;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbase, &npriorities) != SUCCESS) {
return;
}
ZVAL_TO_BASE(zbase, base);
if (npriorities < 0) {
php_error_docref(NULL TSRMLS_CC, 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() TSRMLS_CC, "") != SUCCESS) {
return;
}
event = emalloc(sizeof(php_event_t));
event->event = ecalloc(1, sizeof(struct event));
event->stream_id = -1;
event->callback = NULL;
event->base = NULL;
event->in_free = 0;
TSRMLS_SET_CTX(event->thread_ctx);
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
event->rsrc_id = zend_list_insert(event, le_event TSRMLS_CC);
#else
event->rsrc_id = zend_list_insert(event, le_event);
#endif
RETURN_RESOURCE(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() TSRMLS_CC, "r", &zevent) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
zend_list_delete(event->rsrc_id);
}
/* }}} */
/* {{{ proto bool event_add(resource event[, int timeout])
*/
static PHP_FUNCTION(event_add)
{
zval *zevent;
php_event_t *event;
int ret;
long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zevent, &timeout) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
if (!event->base) {
php_error_docref(NULL TSRMLS_CC, 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;
long events;
php_event_callback_t *callback, *old_callback;
char *func_name;
php_stream *stream;
php_socket_t file_desc;
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZlz|z", &zevent, &fd, &events, &zcallback, &zarg) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
if (events & EV_SIGNAL) {
/* signal support */
convert_to_long_ex(fd);
file_desc = Z_LVAL_PP(fd);
if (file_desc < 0 || file_desc >= NSIG) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid signal passed");
RETURN_FALSE;
}
} else {
if (Z_TYPE_PP(fd) == IS_RESOURCE) {
if (ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, fd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) {
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
if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, fd, -1, NULL, php_sockets_le_socket())) {
file_desc = php_sock->bsd_socket;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource");
RETURN_FALSE;
}
#else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource");
RETURN_FALSE;
#endif
}
} else if (Z_TYPE_PP(fd) == IS_LONG) {
file_desc = Z_LVAL_PP(fd);
if (file_desc < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid file descriptor passed");
RETURN_FALSE;
}
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long");
#else
php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
zval_add_ref(&zcallback);
if (zarg) {
zval_add_ref(&zarg);
} else {
ALLOC_INIT_ZVAL(zarg);
}
callback = emalloc(sizeof(php_event_callback_t));
callback->func = zcallback;
callback->arg = zarg;
old_callback = event->callback;
event->callback = callback;
if (events & EV_SIGNAL) {
event->stream_id = -1;
} else {
zend_list_addref(Z_LVAL_PP(fd));
event->stream_id = Z_LVAL_PP(fd);
}
event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event);
if (old_callback) {
_php_event_callback_free(old_callback);
}
if (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() TSRMLS_CC, "r", &zevent) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
if (!event->base) {
php_error_docref(NULL TSRMLS_CC, 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;
long priority;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zevent, &priority) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
if (!event->base) {
php_error_docref(NULL TSRMLS_CC, 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;
char *func_name;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &zevent, &zcallback, &zarg) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
zval_add_ref(&zcallback);
if (zarg) {
zval_add_ref(&zarg);
} else {
ALLOC_INIT_ZVAL(zarg);
}
callback = emalloc(sizeof(php_event_callback_t));
callback->func = zcallback;
callback->arg = zarg;
old_callback = event->callback;
event->callback = callback;
if (event->stream_id >= 0) {
zend_list_delete(event->stream_id);
}
event->stream_id = -1;
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;
long timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zevent, &timeout) != SUCCESS) {
return;
}
ZVAL_TO_EVENT(zevent, event);
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;
char *func_name;
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz|z", &zfd, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) {
return;
}
if (Z_TYPE_P(zfd) == IS_RESOURCE) {
if (ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, &zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) {
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
if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, &zfd, -1, NULL, php_sockets_le_socket())) {
fd = php_sock->bsd_socket;
} else {
php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long");
RETURN_FALSE;
#endif
}
} else if (Z_TYPE_P(zfd) == IS_LONG) {
fd = Z_LVAL_P(zfd);
} else {
#ifdef LIBEVENT_SOCKETS_SUPPORT
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long");
#else
php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid read callback", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
} else {
zreadcb = NULL;
}
if (Z_TYPE_P(zwritecb) != IS_NULL) {
if (!zend_is_callable(zwritecb, 0, &func_name TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
} else {
zwritecb = NULL;
}
if (!zend_is_callable(zerrorcb, 0, &func_name TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
bevent = emalloc(sizeof(php_bufferevent_t));
bevent->bevent = bufferevent_new(fd, _php_bufferevent_readcb, _php_bufferevent_writecb, _php_bufferevent_errorcb, bevent);
bevent->base = NULL;
if (zreadcb) {
zval_add_ref(&zreadcb);
}
bevent->readcb = zreadcb;
if (zwritecb) {
zval_add_ref(&zwritecb);
}
bevent->writecb = zwritecb;
zval_add_ref(&zerrorcb);
bevent->errorcb = zerrorcb;
if (zarg) {
zval_add_ref(&zarg);
bevent->arg = zarg;
} else {
ALLOC_INIT_ZVAL(bevent->arg);
}
TSRMLS_SET_CTX(bevent->thread_ctx);
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent TSRMLS_CC);
#else
bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent);
#endif
RETURN_RESOURCE(bevent->rsrc_id);
}