… | |
… | |
23 | #define EV_NONE 0 |
23 | #define EV_NONE 0 |
24 | #define EV_UNDEF -1 |
24 | #define EV_UNDEF -1 |
25 | |
25 | |
26 | #define TIMEOUT_NONE HUGE_VAL |
26 | #define TIMEOUT_NONE HUGE_VAL |
27 | |
27 | |
|
|
28 | #include "EV/EVAPI.h" |
|
|
29 | |
28 | typedef struct event_base *Base; |
30 | typedef struct event_base *Base; |
|
|
31 | typedef int Signal; |
|
|
32 | |
|
|
33 | static struct EVAPI evapi; |
29 | |
34 | |
30 | static HV *stash_base, *stash_event; |
35 | static HV *stash_base, *stash_event; |
31 | |
36 | |
32 | static double tv_get (struct timeval *tv) |
37 | static double tv_get (struct timeval *tv) |
33 | { |
38 | { |
… | |
… | |
37 | static void tv_set (struct timeval *tv, double val) |
42 | static void tv_set (struct timeval *tv, double val) |
38 | { |
43 | { |
39 | tv->tv_sec = (long)val; |
44 | tv->tv_sec = (long)val; |
40 | tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6); |
45 | tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6); |
41 | |
46 | |
|
|
47 | } |
|
|
48 | |
|
|
49 | static int |
|
|
50 | sv_signum (SV *sig) |
|
|
51 | { |
|
|
52 | int signum; |
|
|
53 | |
|
|
54 | if (SvIV (sig) > 0) |
|
|
55 | return SvIV (sig); |
|
|
56 | |
|
|
57 | for (signum = 1; signum < SIG_SIZE; ++signum) |
|
|
58 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
|
|
59 | return signum; |
|
|
60 | |
|
|
61 | return -1; |
|
|
62 | } |
|
|
63 | |
|
|
64 | static void |
|
|
65 | api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg) |
|
|
66 | { |
|
|
67 | if (timeout) |
|
|
68 | { |
|
|
69 | struct timeval tv; |
|
|
70 | tv_set (&tv, timeout); |
|
|
71 | event_once (fd, events, cb, arg, &tv); |
|
|
72 | } |
|
|
73 | else |
|
|
74 | event_once (fd, events, cb, arg, 0); |
42 | } |
75 | } |
43 | |
76 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
77 | ///////////////////////////////////////////////////////////////////////////// |
45 | // Event |
78 | // Event |
46 | |
79 | |
… | |
… | |
53 | unsigned char active; |
86 | unsigned char active; |
54 | unsigned char abstime; |
87 | unsigned char abstime; |
55 | } *Event; |
88 | } *Event; |
56 | |
89 | |
57 | static double |
90 | static double |
58 | e_now () |
91 | e_now (void) |
59 | { |
92 | { |
60 | struct timeval tv; |
93 | struct timeval tv; |
61 | gettimeofday (&tv, 0); |
94 | gettimeofday (&tv, 0); |
62 | |
95 | |
63 | return tv_get (&tv); |
96 | return tv_get (&tv); |
… | |
… | |
116 | |
149 | |
117 | if (ev->abstime) |
150 | if (ev->abstime) |
118 | { |
151 | { |
119 | double now = e_now (); |
152 | double now = e_now (); |
120 | |
153 | |
121 | if (now > to && ev->interval) |
154 | if (ev->interval) |
122 | ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); |
155 | ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); |
123 | |
156 | |
124 | to -= now; |
157 | to -= now; |
125 | } |
158 | } |
126 | else if (to < 0.) |
159 | else if (to < 0.) |
… | |
… | |
169 | dSP; |
202 | dSP; |
170 | |
203 | |
171 | ENTER; |
204 | ENTER; |
172 | SAVETMPS; |
205 | SAVETMPS; |
173 | |
206 | |
174 | if (!(ev->ev.ev_events & EV_PERSIST)) |
207 | if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT)) |
175 | ev->active = 0; |
208 | ev->active = 0; |
176 | |
209 | |
177 | PUSHMARK (SP); |
210 | PUSHMARK (SP); |
178 | EXTEND (SP, 2); |
211 | EXTEND (SP, 2); |
179 | PUSHs (sv_2mortal (e_self (ev))); |
212 | PUSHs (sv_2mortal (e_self (ev))); |
180 | PUSHs (sv_2mortal (newSViv (events))); |
213 | PUSHs (sv_2mortal (newSViv (events))); |
181 | PUTBACK; |
214 | PUTBACK; |
182 | call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); |
215 | call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); |
183 | /*TODO: if err, call some logging function */ |
|
|
184 | |
216 | |
185 | if (ev->interval && !ev->active) |
217 | if (ev->interval && !ev->active) |
186 | e_start (ev); |
218 | e_start (ev); |
187 | |
219 | |
188 | FREETMPS; |
220 | FREETMPS; |
|
|
221 | |
|
|
222 | if (SvTRUE (ERRSV)) |
|
|
223 | { |
|
|
224 | PUSHMARK (SP); |
|
|
225 | PUTBACK; |
|
|
226 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
|
|
227 | } |
|
|
228 | |
189 | LEAVE; |
229 | LEAVE; |
190 | } |
230 | } |
191 | |
231 | |
192 | ///////////////////////////////////////////////////////////////////////////// |
232 | ///////////////////////////////////////////////////////////////////////////// |
193 | // DNS |
233 | // DNS |
… | |
… | |
228 | |
268 | |
229 | PUTBACK; |
269 | PUTBACK; |
230 | call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); |
270 | call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); |
231 | |
271 | |
232 | FREETMPS; |
272 | FREETMPS; |
|
|
273 | |
|
|
274 | if (SvTRUE (ERRSV)) |
|
|
275 | { |
|
|
276 | PUSHMARK (SP); |
|
|
277 | PUTBACK; |
|
|
278 | call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); |
|
|
279 | } |
|
|
280 | |
233 | LEAVE; |
281 | LEAVE; |
234 | } |
282 | } |
235 | |
283 | |
236 | ///////////////////////////////////////////////////////////////////////////// |
284 | ///////////////////////////////////////////////////////////////////////////// |
237 | // XS interface functions |
285 | // XS interface functions |
238 | |
286 | |
239 | MODULE = EV PACKAGE = EV PREFIX = event_ |
287 | MODULE = EV PACKAGE = EV PREFIX = event_ |
240 | |
288 | |
241 | BOOT: |
289 | BOOT: |
242 | { |
290 | { |
|
|
291 | int i; |
243 | HV *stash = gv_stashpv ("EV", 1); |
292 | HV *stash = gv_stashpv ("EV", 1); |
244 | |
293 | |
245 | static const struct { |
294 | static const struct { |
246 | const char *name; |
295 | const char *name; |
247 | IV iv; |
296 | IV iv; |
… | |
… | |
265 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
314 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
266 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
315 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
267 | |
316 | |
268 | stash_base = gv_stashpv ("EV::Base" , 1); |
317 | stash_base = gv_stashpv ("EV::Base" , 1); |
269 | stash_event = gv_stashpv ("EV::Event", 1); |
318 | stash_event = gv_stashpv ("EV::Event", 1); |
|
|
319 | |
|
|
320 | { |
|
|
321 | SV *sv = perl_get_sv ("EV::API", TRUE); |
|
|
322 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
|
|
323 | |
|
|
324 | evapi.ver = EV_API_VERSION; |
|
|
325 | evapi.rev = EV_API_REVISION; |
|
|
326 | evapi.now = e_now; |
|
|
327 | evapi.once = api_once; |
|
|
328 | |
|
|
329 | sv_setiv (sv, (IV)&evapi); |
|
|
330 | SvREADONLY_on (sv); |
|
|
331 | } |
270 | } |
332 | } |
271 | |
333 | |
272 | double now () |
334 | double now () |
273 | CODE: |
335 | CODE: |
274 | RETVAL = e_now (); |
336 | RETVAL = e_now (); |
… | |
… | |
309 | ALIAS: |
371 | ALIAS: |
310 | io_ns = 1 |
372 | io_ns = 1 |
311 | CODE: |
373 | CODE: |
312 | RETVAL = e_new (fh, events, cb); |
374 | RETVAL = e_new (fh, events, cb); |
313 | if (!ix) e_start (RETVAL); |
375 | if (!ix) e_start (RETVAL); |
|
|
376 | OUTPUT: |
|
|
377 | RETVAL |
|
|
378 | |
|
|
379 | Event timed_io (SV *fh, short events, double timeout, SV *cb) |
|
|
380 | ALIAS: |
|
|
381 | timed_io_ns = 1 |
|
|
382 | CODE: |
|
|
383 | { |
|
|
384 | events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST; |
|
|
385 | |
|
|
386 | RETVAL = e_new (fh, events, cb); |
|
|
387 | |
|
|
388 | if (timeout) |
|
|
389 | { |
|
|
390 | RETVAL->timeout = timeout; |
|
|
391 | RETVAL->interval = 1; |
|
|
392 | } |
|
|
393 | |
|
|
394 | if (!ix) e_start (RETVAL); |
|
|
395 | } |
314 | OUTPUT: |
396 | OUTPUT: |
315 | RETVAL |
397 | RETVAL |
316 | |
398 | |
317 | Event timer (double after, int repeat, SV *cb) |
399 | Event timer (double after, int repeat, SV *cb) |
318 | ALIAS: |
400 | ALIAS: |
… | |
… | |
335 | RETVAL->abstime = 1; |
417 | RETVAL->abstime = 1; |
336 | if (!ix) e_start (RETVAL); |
418 | if (!ix) e_start (RETVAL); |
337 | OUTPUT: |
419 | OUTPUT: |
338 | RETVAL |
420 | RETVAL |
339 | |
421 | |
340 | Event signal (SV *signal, SV *cb) |
422 | Event signal (Signal signum, SV *cb) |
341 | ALIAS: |
423 | ALIAS: |
342 | signal_ns = 1 |
424 | signal_ns = 1 |
343 | CODE: |
425 | CODE: |
344 | RETVAL = e_new (signal, EV_SIGNAL | EV_PERSIST, cb); |
426 | RETVAL = e_new (ST (0), EV_SIGNAL | EV_PERSIST, cb); |
|
|
427 | RETVAL->ev.ev_fd = signum; |
345 | if (!ix) e_start (RETVAL); |
428 | if (!ix) e_start (RETVAL); |
346 | OUTPUT: |
429 | OUTPUT: |
347 | RETVAL |
430 | RETVAL |
348 | |
431 | |
349 | PROTOTYPES: DISABLE |
432 | PROTOTYPES: DISABLE |
… | |
… | |
420 | sv_setsv (ev->cb, new_cb); |
503 | sv_setsv (ev->cb, new_cb); |
421 | OUTPUT: |
504 | OUTPUT: |
422 | RETVAL |
505 | RETVAL |
423 | |
506 | |
424 | SV *fh (Event ev, SV *new_fh = 0) |
507 | SV *fh (Event ev, SV *new_fh = 0) |
425 | ALIAS: |
|
|
426 | signal = 0 |
|
|
427 | CODE: |
508 | CODE: |
428 | RETVAL = newSVsv (ev->fh); |
509 | RETVAL = newSVsv (ev->fh); |
429 | if (items > 1) |
510 | if (items > 1) |
430 | { |
511 | { |
431 | if (ev->active) event_del (&ev->ev); |
512 | if (ev->active) event_del (&ev->ev); |
432 | sv_setsv (ev->fh, new_fh); |
513 | sv_setsv (ev->fh, new_fh); |
433 | ev->ev.ev_fd = sv_fileno (ev->fh); |
514 | ev->ev.ev_fd = sv_fileno (ev->fh); |
|
|
515 | ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL; |
434 | if (ev->active) event_add (&ev->ev, e_tv (ev)); |
516 | if (ev->active) event_add (&ev->ev, e_tv (ev)); |
435 | } |
517 | } |
|
|
518 | OUTPUT: |
|
|
519 | RETVAL |
|
|
520 | |
|
|
521 | SV *signal (Event ev, SV *new_signal = 0) |
|
|
522 | CODE: |
|
|
523 | { |
|
|
524 | Signal signum; |
|
|
525 | |
|
|
526 | if (items > 1) |
|
|
527 | signum = sv_signum (new_signal); /* may croak here */ |
|
|
528 | |
|
|
529 | RETVAL = newSVsv (ev->fh); |
|
|
530 | |
|
|
531 | if (items > 1) |
|
|
532 | { |
|
|
533 | if (ev->active) event_del (&ev->ev); |
|
|
534 | sv_setsv (ev->fh, new_signal); |
|
|
535 | ev->ev.ev_fd = signum; |
|
|
536 | ev->ev.ev_events |= EV_SIGNAL; |
|
|
537 | if (ev->active) event_add (&ev->ev, e_tv (ev)); |
|
|
538 | } |
|
|
539 | } |
436 | OUTPUT: |
540 | OUTPUT: |
437 | RETVAL |
541 | RETVAL |
438 | |
542 | |
439 | short events (Event ev, short new_events = EV_UNDEF) |
543 | short events (Event ev, short new_events = EV_UNDEF) |
440 | CODE: |
544 | CODE: |
… | |
… | |
480 | static const struct { |
584 | static const struct { |
481 | const char *name; |
585 | const char *name; |
482 | IV iv; |
586 | IV iv; |
483 | } *civ, const_iv[] = { |
587 | } *civ, const_iv[] = { |
484 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
588 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
485 | |
|
|
486 | const_iv (DNS_, ERR_NONE) |
589 | const_iv (DNS_, ERR_NONE) |
487 | const_iv (DNS_, ERR_FORMAT) |
590 | const_iv (DNS_, ERR_FORMAT) |
488 | const_iv (DNS_, ERR_SERVERFAILED) |
591 | const_iv (DNS_, ERR_SERVERFAILED) |
489 | const_iv (DNS_, ERR_NOTEXIST) |
592 | const_iv (DNS_, ERR_NOTEXIST) |
490 | const_iv (DNS_, ERR_NOTIMPL) |
593 | const_iv (DNS_, ERR_NOTIMPL) |
… | |
… | |
536 | CODE: |
639 | CODE: |
537 | { |
640 | { |
538 | STRLEN len; |
641 | STRLEN len; |
539 | char *data = SvPVbyte (addr, len); |
642 | char *data = SvPVbyte (addr, len); |
540 | if (len != (ix ? 16 : 4)) |
643 | if (len != (ix ? 16 : 4)) |
541 | croak ("ipv4/ipv6 address to resolve must be given as 4/16 byte octet string"); |
644 | croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string"); |
542 | |
645 | |
543 | RETVAL = ix |
646 | RETVAL = ix |
544 | ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) |
647 | ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) |
545 | : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)); |
648 | : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)); |
546 | } |
649 | } |
… | |
… | |
561 | |
664 | |
562 | void evdns_search_add (char *domain) |
665 | void evdns_search_add (char *domain) |
563 | |
666 | |
564 | void evdns_search_ndots_set (int ndots) |
667 | void evdns_search_ndots_set (int ndots) |
565 | |
668 | |
|
|
669 | |
|
|
670 | MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_ |
|
|
671 | |
|
|
672 | BOOT: |
|
|
673 | { |
|
|
674 | HV *stash = gv_stashpv ("EV::HTTP", 1); |
|
|
675 | |
|
|
676 | static const struct { |
|
|
677 | const char *name; |
|
|
678 | IV iv; |
|
|
679 | } *civ, const_iv[] = { |
|
|
680 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
|
|
681 | const_iv (HTTP_, OK) |
|
|
682 | const_iv (HTTP_, NOCONTENT) |
|
|
683 | const_iv (HTTP_, MOVEPERM) |
|
|
684 | const_iv (HTTP_, MOVETEMP) |
|
|
685 | const_iv (HTTP_, NOTMODIFIED) |
|
|
686 | const_iv (HTTP_, BADREQUEST) |
|
|
687 | const_iv (HTTP_, NOTFOUND) |
|
|
688 | const_iv (HTTP_, SERVUNAVAIL) |
|
|
689 | const_iv (EVHTTP_, REQ_OWN_CONNECTION) |
|
|
690 | const_iv (EVHTTP_, PROXY_REQUEST) |
|
|
691 | const_iv (EVHTTP_, REQ_GET) |
|
|
692 | const_iv (EVHTTP_, REQ_POST) |
|
|
693 | const_iv (EVHTTP_, REQ_HEAD) |
|
|
694 | const_iv (EVHTTP_, REQUEST) |
|
|
695 | const_iv (EVHTTP_, RESPONSE) |
|
|
696 | }; |
|
|
697 | |
|
|
698 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
|
|
699 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
|
|
700 | } |
|
|
701 | |
|
|
702 | MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_ |
|
|
703 | |
|
|
704 | #HttpRequest new (SV *klass, SV *cb) |
|
|
705 | |
|
|
706 | #void DESTROY (struct evhttp_request *req); |
|
|
707 | |
|
|
708 | |
|
|
709 | |
|
|
710 | |
|
|
711 | |
|
|
712 | |
|
|
713 | |
|
|
714 | |