… | |
… | |
5 | /*#include <netinet/in.h>*/ |
5 | /*#include <netinet/in.h>*/ |
6 | |
6 | |
7 | #define EV_PROTOTYPES 1 |
7 | #define EV_PROTOTYPES 1 |
8 | #include "EV/EVAPI.h" |
8 | #include "EV/EVAPI.h" |
9 | |
9 | |
|
|
10 | /* fix perl api breakage */ |
|
|
11 | #undef signal |
|
|
12 | #undef sigaction |
|
|
13 | |
|
|
14 | #define EV_SELECT_USE_WIN32_HANDLES 0 |
|
|
15 | #define EV_SELECT_USE_FD_SET 0 |
|
|
16 | /* due to bugs in OS X we have to use libev/ explicitly here */ |
10 | #include "libev/ev.c" |
17 | #include "libev/ev.c" |
11 | #include "libev/event.h" |
|
|
12 | #include "libev/event.c" |
18 | #include "event.c" |
|
|
19 | |
|
|
20 | #ifndef WIN32 |
|
|
21 | #define DNS_USE_GETTIMEOFDAY_FOR_ID 1 |
|
|
22 | #if !defined (WIN32) && !defined(__CYGWIN__) |
|
|
23 | # define HAVE_STRUCT_IN6_ADDR 1 |
|
|
24 | #endif |
|
|
25 | #undef HAVE_STRTOK_R |
|
|
26 | #undef strtok_r |
|
|
27 | #define strtok_r fake_strtok_r |
|
|
28 | #include "evdns.h" |
13 | #include "libev/evdns.c" |
29 | #include "evdns.c" |
|
|
30 | #endif |
|
|
31 | |
|
|
32 | #ifndef WIN32 |
|
|
33 | # include <pthread.h> |
|
|
34 | #endif |
14 | |
35 | |
15 | typedef int Signal; |
36 | typedef int Signal; |
16 | |
37 | |
17 | static struct EVAPI evapi; |
38 | static struct EVAPI evapi; |
18 | |
39 | |
… | |
… | |
25 | *stash_idle, |
46 | *stash_idle, |
26 | *stash_prepare, |
47 | *stash_prepare, |
27 | *stash_check, |
48 | *stash_check, |
28 | *stash_child; |
49 | *stash_child; |
29 | |
50 | |
|
|
51 | #ifndef SIG_SIZE |
|
|
52 | /* kudos to Slaven Rezic for the idea */ |
|
|
53 | static char sig_size [] = { SIG_NUM }; |
|
|
54 | # define SIG_SIZE (sizeof (sig_size) + 1) |
|
|
55 | #endif |
|
|
56 | |
30 | static int |
57 | static int |
31 | sv_signum (SV *sig) |
58 | sv_signum (SV *sig) |
32 | { |
59 | { |
33 | int signum; |
60 | int signum; |
34 | |
61 | |
… | |
… | |
76 | |
103 | |
77 | w = (struct ev_watcher *)SvPVX (self); |
104 | w = (struct ev_watcher *)SvPVX (self); |
78 | |
105 | |
79 | ev_watcher_init (w, e_cb); |
106 | ev_watcher_init (w, e_cb); |
80 | |
107 | |
|
|
108 | w->data = 0; |
81 | w->fh = 0; |
109 | w->fh = 0; |
82 | w->cb_sv = newSVsv (cb_sv); |
110 | w->cb_sv = newSVsv (cb_sv); |
83 | w->self = self; |
111 | w->self = self; |
84 | |
112 | |
85 | return (void *)w; |
113 | return (void *)w; |
86 | } |
114 | } |
87 | |
115 | |
88 | static void * |
116 | static void |
89 | e_destroy (void *w_) |
117 | e_destroy (void *w_) |
90 | { |
118 | { |
91 | struct ev_watcher *w = w_; |
119 | struct ev_watcher *w = (struct ev_watcher *)w_; |
92 | |
120 | |
93 | SvREFCNT_dec (w->fh ); w->fh = 0; |
121 | SvREFCNT_dec (w->fh ); w->fh = 0; |
94 | SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; |
122 | SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; |
|
|
123 | SvREFCNT_dec (w->data ); w->data = 0; |
95 | } |
124 | } |
96 | |
125 | |
97 | static SV * |
126 | static SV * |
98 | e_bless (struct ev_watcher *w, HV *stash) |
127 | e_bless (struct ev_watcher *w, HV *stash) |
99 | { |
128 | { |
… | |
… | |
132 | PUSHMARK (SP); |
161 | PUSHMARK (SP); |
133 | EXTEND (SP, 2); |
162 | EXTEND (SP, 2); |
134 | PUSHs (sv_self); |
163 | PUSHs (sv_self); |
135 | PUSHs (sv_events); |
164 | PUSHs (sv_events); |
136 | |
165 | |
137 | if (revents & EV_CHILD) |
|
|
138 | XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status)); |
|
|
139 | |
|
|
140 | PUTBACK; |
166 | PUTBACK; |
141 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
167 | call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); |
142 | SP = PL_stack_base + mark; PUTBACK; |
168 | SP = PL_stack_base + mark; PUTBACK; |
143 | |
169 | |
144 | SvREFCNT_dec (sv_self); |
170 | SvREFCNT_dec (sv_self); |
… | |
… | |
156 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
182 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
157 | SP = PL_stack_base + mark; PUTBACK; |
183 | SP = PL_stack_base + mark; PUTBACK; |
158 | } |
184 | } |
159 | } |
185 | } |
160 | |
186 | |
|
|
187 | static ev_tstamp |
|
|
188 | e_periodic_cb (struct ev_periodic *w, ev_tstamp now) |
|
|
189 | { |
|
|
190 | ev_tstamp retval; |
|
|
191 | int count; |
|
|
192 | dSP; |
|
|
193 | |
|
|
194 | ENTER; |
|
|
195 | SAVETMPS; |
|
|
196 | |
|
|
197 | PUSHMARK (SP); |
|
|
198 | EXTEND (SP, 2); |
|
|
199 | PUSHs (newRV_inc (w->self)); /* w->self MUST be blessed by now */ |
|
|
200 | PUSHs (newSVnv (now)); |
|
|
201 | |
|
|
202 | PUTBACK; |
|
|
203 | count = call_sv (w->fh, G_SCALAR | G_EVAL); |
|
|
204 | SPAGAIN; |
|
|
205 | |
|
|
206 | if (SvTRUE (ERRSV)) |
|
|
207 | { |
|
|
208 | PUSHMARK (SP); |
|
|
209 | PUTBACK; |
|
|
210 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
|
|
211 | SPAGAIN; |
|
|
212 | } |
|
|
213 | |
|
|
214 | if (count > 0) |
|
|
215 | { |
|
|
216 | retval = SvNV (TOPs); |
|
|
217 | |
|
|
218 | if (retval < now) |
|
|
219 | retval = now; |
|
|
220 | } |
|
|
221 | else |
|
|
222 | retval = now; |
|
|
223 | |
|
|
224 | FREETMPS; |
|
|
225 | LEAVE; |
|
|
226 | |
|
|
227 | return retval; |
|
|
228 | } |
|
|
229 | |
161 | ///////////////////////////////////////////////////////////////////////////// |
230 | ///////////////////////////////////////////////////////////////////////////// |
162 | // DNS |
231 | // DNS |
163 | |
232 | |
|
|
233 | #ifndef WIN32 |
164 | static void |
234 | static void |
165 | dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) |
235 | dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) |
166 | { |
236 | { |
167 | dSP; |
237 | dSP; |
168 | SV *cb = (SV *)arg; |
238 | SV *cb = (SV *)arg; |
… | |
… | |
207 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
277 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
208 | } |
278 | } |
209 | |
279 | |
210 | LEAVE; |
280 | LEAVE; |
211 | } |
281 | } |
|
|
282 | #endif |
212 | |
283 | |
213 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
284 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
214 | croak (# repeat " value must be >= 0"); |
285 | croak (# repeat " value must be >= 0"); |
215 | |
286 | |
216 | #define CHECK_FD(fh,fd) if ((fd) < 0) \ |
287 | #define CHECK_FD(fh,fd) if ((fd) < 0) \ |
… | |
… | |
223 | |
294 | |
224 | PROTOTYPES: ENABLE |
295 | PROTOTYPES: ENABLE |
225 | |
296 | |
226 | BOOT: |
297 | BOOT: |
227 | { |
298 | { |
228 | int i; |
|
|
229 | HV *stash = gv_stashpv ("EV", 1); |
299 | HV *stash = gv_stashpv ("EV", 1); |
230 | |
300 | |
231 | static const struct { |
301 | static const struct { |
232 | const char *name; |
302 | const char *name; |
233 | IV iv; |
303 | IV iv; |
… | |
… | |
279 | /* the poor man's shared library emulator */ |
349 | /* the poor man's shared library emulator */ |
280 | evapi.ver = EV_API_VERSION; |
350 | evapi.ver = EV_API_VERSION; |
281 | evapi.rev = EV_API_REVISION; |
351 | evapi.rev = EV_API_REVISION; |
282 | evapi.sv_fileno = sv_fileno; |
352 | evapi.sv_fileno = sv_fileno; |
283 | evapi.sv_signum = sv_signum; |
353 | evapi.sv_signum = sv_signum; |
284 | evapi.now = &ev_now; |
354 | evapi.now = ev_now; |
285 | evapi.method = &ev_method; |
355 | evapi.method = ev_method; |
286 | evapi.loop_done = &ev_loop_done; |
356 | evapi.unloop = ev_unloop; |
287 | evapi.time = ev_time; |
357 | evapi.time = ev_time; |
288 | evapi.loop = ev_loop; |
358 | evapi.loop = ev_loop; |
289 | evapi.once = ev_once; |
359 | evapi.once = ev_once; |
290 | evapi.io_start = ev_io_start; |
360 | evapi.io_start = ev_io_start; |
291 | evapi.io_stop = ev_io_stop; |
361 | evapi.io_stop = ev_io_stop; |
… | |
… | |
306 | evapi.child_stop = ev_child_stop; |
376 | evapi.child_stop = ev_child_stop; |
307 | |
377 | |
308 | sv_setiv (sv, (IV)&evapi); |
378 | sv_setiv (sv, (IV)&evapi); |
309 | SvREADONLY_on (sv); |
379 | SvREADONLY_on (sv); |
310 | } |
380 | } |
311 | |
381 | #ifndef WIN32 |
312 | pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); |
382 | pthread_atfork (0, 0, ev_default_fork); |
|
|
383 | #endif |
313 | } |
384 | } |
314 | |
385 | |
315 | NV ev_now () |
386 | NV ev_now () |
316 | CODE: |
|
|
317 | RETVAL = ev_now; |
|
|
318 | OUTPUT: |
|
|
319 | RETVAL |
|
|
320 | |
387 | |
321 | int ev_method () |
388 | int ev_method () |
322 | CODE: |
|
|
323 | RETVAL = ev_method; |
|
|
324 | OUTPUT: |
|
|
325 | RETVAL |
|
|
326 | |
389 | |
327 | NV ev_time () |
390 | NV ev_time () |
328 | |
391 | |
329 | void ev_init (int methods = EVMETHOD_AUTO) |
392 | int ev_default_loop (int methods = EVMETHOD_AUTO) |
330 | |
393 | |
331 | void ev_loop (int flags = 0) |
394 | void ev_loop (int flags = 0) |
332 | |
395 | |
333 | void ev_loop_done (int value = 1) |
396 | void ev_unloop (int how = 1) |
334 | CODE: |
|
|
335 | ev_loop_done = value; |
|
|
336 | |
397 | |
337 | struct ev_io *io (SV *fh, int events, SV *cb) |
398 | struct ev_io *io (SV *fh, int events, SV *cb) |
338 | ALIAS: |
399 | ALIAS: |
339 | io_ns = 1 |
400 | io_ns = 1 |
340 | CODE: |
401 | CODE: |
… | |
… | |
360 | ev_timer_set (RETVAL, after, repeat); |
421 | ev_timer_set (RETVAL, after, repeat); |
361 | if (!ix) ev_timer_start (RETVAL); |
422 | if (!ix) ev_timer_start (RETVAL); |
362 | OUTPUT: |
423 | OUTPUT: |
363 | RETVAL |
424 | RETVAL |
364 | |
425 | |
365 | struct ev_periodic *periodic (NV at, NV interval, SV *cb) |
426 | SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb) |
366 | ALIAS: |
427 | ALIAS: |
367 | periodic_ns = 1 |
428 | periodic_ns = 1 |
368 | INIT: |
429 | INIT: |
369 | CHECK_REPEAT (interval); |
430 | CHECK_REPEAT (interval); |
370 | CODE: |
431 | CODE: |
|
|
432 | { |
|
|
433 | struct ev_periodic *w; |
371 | RETVAL = e_new (sizeof (struct ev_periodic), cb); |
434 | w = e_new (sizeof (struct ev_periodic), cb); |
372 | ev_periodic_set (RETVAL, at, interval); |
435 | w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; |
|
|
436 | ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0); |
|
|
437 | RETVAL = e_bless ((struct ev_watcher *)w, stash_periodic); |
373 | if (!ix) ev_periodic_start (RETVAL); |
438 | if (!ix) ev_periodic_start (w); |
|
|
439 | } |
374 | OUTPUT: |
440 | OUTPUT: |
375 | RETVAL |
441 | RETVAL |
376 | |
442 | |
377 | struct ev_signal *signal (Signal signum, SV *cb) |
443 | struct ev_signal *signal (Signal signum, SV *cb) |
378 | ALIAS: |
444 | ALIAS: |
… | |
… | |
416 | |
482 | |
417 | struct ev_child *child (int pid, SV *cb) |
483 | struct ev_child *child (int pid, SV *cb) |
418 | ALIAS: |
484 | ALIAS: |
419 | check_ns = 1 |
485 | check_ns = 1 |
420 | CODE: |
486 | CODE: |
421 | RETVAL = e_new (sizeof (struct ev_check), cb); |
487 | RETVAL = e_new (sizeof (struct ev_child), cb); |
422 | ev_child_set (RETVAL, pid); |
488 | ev_child_set (RETVAL, pid); |
423 | if (!ix) ev_child_start (RETVAL); |
489 | if (!ix) ev_child_start (RETVAL); |
424 | OUTPUT: |
490 | OUTPUT: |
425 | RETVAL |
491 | RETVAL |
426 | |
492 | |
… | |
… | |
436 | { |
502 | { |
437 | RETVAL = newSVsv (w->cb_sv); |
503 | RETVAL = newSVsv (w->cb_sv); |
438 | |
504 | |
439 | if (items > 1) |
505 | if (items > 1) |
440 | sv_setsv (w->cb_sv, new_cb); |
506 | sv_setsv (w->cb_sv, new_cb); |
|
|
507 | } |
|
|
508 | OUTPUT: |
|
|
509 | RETVAL |
|
|
510 | |
|
|
511 | SV *data (struct ev_watcher *w, SV *new_data = 0) |
|
|
512 | CODE: |
|
|
513 | { |
|
|
514 | RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef; |
441 | } |
515 | } |
442 | OUTPUT: |
516 | OUTPUT: |
443 | RETVAL |
517 | RETVAL |
444 | |
518 | |
445 | void trigger (struct ev_watcher *w, int revents = EV_NONE) |
519 | void trigger (struct ev_watcher *w, int revents = EV_NONE) |
… | |
… | |
583 | } |
657 | } |
584 | } |
658 | } |
585 | OUTPUT: |
659 | OUTPUT: |
586 | RETVAL |
660 | RETVAL |
587 | |
661 | |
588 | MODULE = EV PACKAGE = EV::Time |
|
|
589 | |
|
|
590 | MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ |
662 | MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ |
591 | |
663 | |
592 | void ev_timer_start (struct ev_timer *w) |
664 | void ev_timer_start (struct ev_timer *w) |
593 | INIT: |
665 | INIT: |
594 | CHECK_REPEAT (w->repeat); |
666 | CHECK_REPEAT (w->repeat); |
… | |
… | |
621 | INIT: |
693 | INIT: |
622 | CHECK_REPEAT (w->interval); |
694 | CHECK_REPEAT (w->interval); |
623 | |
695 | |
624 | void ev_periodic_stop (struct ev_periodic *w) |
696 | void ev_periodic_stop (struct ev_periodic *w) |
625 | |
697 | |
|
|
698 | void ev_periodic_again (struct ev_periodic *w) |
|
|
699 | |
626 | void DESTROY (struct ev_periodic *w) |
700 | void DESTROY (struct ev_periodic *w) |
627 | CODE: |
701 | CODE: |
628 | ev_periodic_stop (w); |
702 | ev_periodic_stop (w); |
629 | e_destroy (w); |
703 | e_destroy (w); |
630 | |
704 | |
631 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
705 | void set (struct ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef) |
632 | INIT: |
706 | INIT: |
633 | CHECK_REPEAT (interval); |
707 | CHECK_REPEAT (interval); |
634 | CODE: |
708 | CODE: |
635 | { |
709 | { |
636 | int active = ev_is_active (w); |
710 | int active = ev_is_active (w); |
637 | if (active) ev_periodic_stop (w); |
711 | if (active) ev_periodic_stop (w); |
638 | |
712 | |
|
|
713 | SvREFCNT_dec (w->fh); |
|
|
714 | w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0; |
639 | ev_periodic_set (w, at, interval); |
715 | ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0); |
640 | |
716 | |
641 | if (active) ev_periodic_start (w); |
717 | if (active) ev_periodic_start (w); |
642 | } |
718 | } |
643 | |
719 | |
644 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
720 | MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ |
… | |
… | |
713 | } |
789 | } |
714 | OUTPUT: |
790 | OUTPUT: |
715 | RETVAL |
791 | RETVAL |
716 | |
792 | |
717 | |
793 | |
718 | int status (struct ev_child *w) |
794 | int rstatus (struct ev_child *w) |
|
|
795 | ALIAS: |
|
|
796 | rpid = 1 |
719 | CODE: |
797 | CODE: |
720 | RETVAL = w->status; |
798 | RETVAL = ix ? w->rpid : w->rstatus; |
721 | OUTPUT: |
799 | OUTPUT: |
722 | RETVAL |
800 | RETVAL |
|
|
801 | |
|
|
802 | #ifndef WIN32 |
723 | |
803 | |
724 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
804 | MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ |
725 | |
805 | |
726 | BOOT: |
806 | BOOT: |
727 | { |
807 | { |
… | |
… | |
852 | |
932 | |
853 | #void DESTROY (struct evhttp_request *req); |
933 | #void DESTROY (struct evhttp_request *req); |
854 | |
934 | |
855 | #endif |
935 | #endif |
856 | |
936 | |
|
|
937 | #endif |
857 | |
938 | |
858 | |
939 | |
859 | |
940 | |
860 | |
941 | |
861 | |
942 | |