ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
Revision: 1.21
Committed: Wed Oct 31 14:44:35 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
Changes since 1.20: +3 -6 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 /*#include <netinet/in.h>*/
6
7 #define TIMEOUT_NONE HUGE_VAL
8 #define HAVE_EPOLL 0
9
10 #define EV_PROTOTYPES 1
11 #include "EV/EVAPI.h"
12
13 #include "libev/ev.c"
14
15 typedef int Signal;
16
17 static struct EVAPI evapi;
18
19 static HV
20 *stash_watcher,
21 *stash_io,
22 *stash_time,
23 *stash_timer,
24 *stash_periodic,
25 *stash_signal,
26 *stash_idle,
27 *stash_check;
28
29 static int
30 sv_signum (SV *sig)
31 {
32 int signum;
33
34 if (SvIV (sig) > 0)
35 return SvIV (sig);
36
37 for (signum = 1; signum < SIG_SIZE; ++signum)
38 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
39 return signum;
40
41 return -1;
42 }
43
44 /////////////////////////////////////////////////////////////////////////////
45 // Event
46
47 static void e_cb (struct ev_watcher *w, int revents);
48
49 static int
50 sv_fileno (SV *fh)
51 {
52 SvGETMAGIC (fh);
53
54 if (SvROK (fh))
55 fh = SvRV (fh);
56
57 if (SvTYPE (fh) == SVt_PVGV)
58 return PerlIO_fileno (IoIFP (sv_2io (fh)));
59
60 if (SvIOK (fh))
61 return SvIV (fh);
62
63 return -1;
64 }
65
66 static void *
67 e_new (int size, SV *cb_sv)
68 {
69 struct ev_watcher *w;
70 SV *self = NEWSV (0, size);
71 SvPOK_only (self);
72 SvCUR_set (self, size);
73
74 w = (struct ev_watcher *)SvPVX (self);
75
76 evw_init (w, e_cb);
77
78 w->fh = 0;
79 w->cb_sv = newSVsv (cb_sv);
80 w->self = self;
81
82 return (void *)w;
83 }
84
85 static SV *
86 e_bless (struct ev_watcher *w, HV *stash)
87 {
88 SV *rv;
89
90 if (SvOBJECT (w->self))
91 rv = newRV_inc (w->self);
92 else
93 {
94 rv = newRV_noinc (w->self);
95 sv_bless (rv, stash);
96 SvREADONLY_on (w->self);
97 }
98
99 return rv;
100 }
101
102 static void
103 e_cb (struct ev_watcher *w, int revents)
104 {
105 dSP;
106 I32 mark = SP - PL_stack_base;
107 SV *sv_self, *sv_events;
108 static SV *sv_events_cache;
109
110 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
111
112 if (sv_events_cache)
113 {
114 sv_events = sv_events_cache; sv_events_cache = 0;
115 SvIV_set (sv_events, revents);
116 }
117 else
118 sv_events = newSViv (revents);
119
120 PUSHMARK (SP);
121 EXTEND (SP, 2);
122 PUSHs (sv_self);
123 PUSHs (sv_events);
124 PUTBACK;
125 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
126 SP = PL_stack_base + mark; PUTBACK;
127
128 SvREFCNT_dec (sv_self);
129
130 if (sv_events_cache)
131 SvREFCNT_dec (sv_events);
132 else
133 sv_events_cache = sv_events;
134
135 if (SvTRUE (ERRSV))
136 {
137 PUSHMARK (SP);
138 PUTBACK;
139 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
140 SP = PL_stack_base + mark; PUTBACK;
141 }
142 }
143
144 #if 0
145 /////////////////////////////////////////////////////////////////////////////
146 // DNS
147
148 static void
149 dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
150 {
151 dSP;
152 SV *cb = (SV *)arg;
153
154 ENTER;
155 SAVETMPS;
156 PUSHMARK (SP);
157 EXTEND (SP, count + 3);
158 PUSHs (sv_2mortal (newSViv (result)));
159
160 if (result == DNS_ERR_NONE && ttl >= 0)
161 {
162 int i;
163
164 PUSHs (sv_2mortal (newSViv (type)));
165 PUSHs (sv_2mortal (newSViv (ttl)));
166
167 for (i = 0; i < count; ++i)
168 switch (type)
169 {
170 case DNS_IPv6_AAAA:
171 PUSHs (sv_2mortal (newSVpvn (i * 16 + (char *)addresses, 16)));
172 break;
173 case DNS_IPv4_A:
174 PUSHs (sv_2mortal (newSVpvn (i * 4 + (char *)addresses, 4)));
175 break;
176 case DNS_PTR:
177 PUSHs (sv_2mortal (newSVpv (*(char **)addresses, 0)));
178 break;
179 }
180 }
181
182 PUTBACK;
183 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
184
185 FREETMPS;
186
187 if (SvTRUE (ERRSV))
188 {
189 PUSHMARK (SP);
190 PUTBACK;
191 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
192 }
193
194 LEAVE;
195 }
196 #endif
197
198 #define CHECK_REPEAT(repeat) if (repeat < 0.) \
199 croak (# repeat " value must be >= 0");
200
201 /////////////////////////////////////////////////////////////////////////////
202 // XS interface functions
203
204 MODULE = EV PACKAGE = EV PREFIX = ev_
205
206 PROTOTYPES: ENABLE
207
208 BOOT:
209 {
210 int i;
211 HV *stash = gv_stashpv ("EV", 1);
212
213 static const struct {
214 const char *name;
215 IV iv;
216 } *civ, const_iv[] = {
217 # define const_iv(pfx, name) { # name, (IV) pfx ## name },
218 const_iv (EV_, UNDEF)
219 const_iv (EV_, NONE)
220 const_iv (EV_, TIMEOUT)
221 const_iv (EV_, READ)
222 const_iv (EV_, WRITE)
223 const_iv (EV_, SIGNAL)
224 const_iv (EV_, IDLE)
225 const_iv (EV_, CHECK)
226 const_iv (EV_, ERROR)
227
228 const_iv (EV, LOOP_ONESHOT)
229 const_iv (EV, LOOP_NONBLOCK)
230
231 const_iv (EV, METHOD_NONE)
232 const_iv (EV, METHOD_SELECT)
233 const_iv (EV, METHOD_EPOLL)
234 };
235
236 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
237 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
238
239 stash_watcher = gv_stashpv ("EV::Watcher" , 1);
240 stash_io = gv_stashpv ("EV::IO" , 1);
241 stash_time = gv_stashpv ("EV::Time" , 1);
242 stash_timer = gv_stashpv ("EV::Timer" , 1);
243 stash_periodic = gv_stashpv ("EV::Periodic", 1);
244 stash_signal = gv_stashpv ("EV::Signal" , 1);
245 stash_idle = gv_stashpv ("EV::Idle" , 1);
246 stash_check = gv_stashpv ("EV::Check" , 1);
247
248 {
249 SV *sv = perl_get_sv ("EV::API", TRUE);
250 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
251
252 /* the poor man's shared library emulator */
253 evapi.ver = EV_API_VERSION;
254 evapi.rev = EV_API_REVISION;
255 evapi.sv_fileno = sv_fileno;
256 evapi.sv_signum = sv_signum;
257 evapi.now = &ev_now;
258 evapi.method = &ev_method;
259 evapi.loop_done = &ev_loop_done;
260 evapi.time = ev_time;
261 evapi.loop = ev_loop;
262 evapi.once = ev_once;
263 evapi.io_start = evio_start;
264 evapi.io_stop = evio_stop;
265 evapi.timer_start = evtimer_start;
266 evapi.timer_stop = evtimer_stop;
267 evapi.timer_again = evtimer_again;
268 evapi.periodic_start = evperiodic_start;
269 evapi.periodic_stop = evperiodic_stop;
270 evapi.signal_start = evsignal_start;
271 evapi.signal_stop = evsignal_stop;
272 evapi.idle_start = evidle_start;
273 evapi.idle_stop = evidle_stop;
274 evapi.check_start = evcheck_start;
275 evapi.check_stop = evcheck_stop;
276
277 sv_setiv (sv, (IV)&evapi);
278 SvREADONLY_on (sv);
279 }
280 }
281
282 NV ev_now ()
283 CODE:
284 RETVAL = ev_now;
285 OUTPUT:
286 RETVAL
287
288 int ev_method ()
289 CODE:
290 RETVAL = ev_method;
291 OUTPUT:
292 RETVAL
293
294 NV ev_time ()
295
296 void ev_init (int flags = 0)
297
298 void ev_loop (int flags = 0)
299
300 void ev_loop_done (int value = 1)
301 CODE:
302 ev_loop_done = value;
303
304 struct ev_io *io (SV *fh, int events, SV *cb)
305 ALIAS:
306 io_ns = 1
307 CODE:
308 RETVAL = e_new (sizeof (struct ev_io), cb);
309 RETVAL->fh = newSVsv (fh);
310 evio_set (RETVAL, sv_fileno (RETVAL->fh), events);
311 if (!ix) evio_start (RETVAL);
312 OUTPUT:
313 RETVAL
314
315 struct ev_timer *timer (NV after, NV repeat, SV *cb)
316 ALIAS:
317 timer_ns = 1
318 INIT:
319 CHECK_REPEAT (repeat);
320 CODE:
321 RETVAL = e_new (sizeof (struct ev_timer), cb);
322 evtimer_set (RETVAL, after, repeat);
323 if (!ix) evtimer_start (RETVAL);
324 OUTPUT:
325 RETVAL
326
327 struct ev_periodic *periodic (NV at, NV interval, SV *cb)
328 ALIAS:
329 periodic_ns = 1
330 INIT:
331 CHECK_REPEAT (interval);
332 CODE:
333 RETVAL = e_new (sizeof (struct ev_periodic), cb);
334 evperiodic_set (RETVAL, at, interval);
335 if (!ix) evperiodic_start (RETVAL);
336 OUTPUT:
337 RETVAL
338
339 struct ev_signal *signal (Signal signum, SV *cb)
340 ALIAS:
341 signal_ns = 1
342 CODE:
343 RETVAL = e_new (sizeof (struct ev_signal), cb);
344 evsignal_set (RETVAL, signum);
345 if (!ix) evsignal_start (RETVAL);
346 OUTPUT:
347 RETVAL
348
349 struct ev_idle *idle (SV *cb)
350 ALIAS:
351 idle_ns = 1
352 CODE:
353 RETVAL = e_new (sizeof (struct ev_idle), cb);
354 evidle_set (RETVAL);
355 if (!ix) evidle_start (RETVAL);
356 OUTPUT:
357 RETVAL
358
359 struct ev_check *check (SV *cb)
360 ALIAS:
361 check_ns = 1
362 CODE:
363 RETVAL = e_new (sizeof (struct ev_check), cb);
364 evcheck_set (RETVAL);
365 if (!ix) evcheck_start (RETVAL);
366 OUTPUT:
367 RETVAL
368
369
370 PROTOTYPES: DISABLE
371
372 MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
373
374 int ev_is_active (struct ev_watcher *w)
375
376 SV *cb (struct ev_watcher *w, SV *new_cb = 0)
377 CODE:
378 {
379 RETVAL = newSVsv (w->cb_sv);
380
381 if (items > 1)
382 sv_setsv (w->cb_sv, new_cb);
383 }
384 OUTPUT:
385 RETVAL
386
387 MODULE = EV PACKAGE = EV::IO PREFIX = evio_
388
389 void evio_start (struct ev_io *w)
390
391 void evio_stop (struct ev_io *w)
392
393 void set (struct ev_io *w, SV *fh, int events)
394 CODE:
395 {
396 int active = w->active;
397 if (active) evio_stop (w);
398
399 sv_setsv (w->fh, fh);
400 evio_set (w, sv_fileno (w->fh), events);
401
402 if (active) evio_start (w);
403 }
404
405 SV *fh (struct ev_io *w, SV *new_fh = 0)
406 CODE:
407 {
408 RETVAL = newSVsv (w->fh);
409
410 if (items > 1)
411 {
412 int active = w->active;
413 if (active) evio_stop (w);
414
415 sv_setsv (w->fh, new_fh);
416 evio_set (w, sv_fileno (w->fh), w->events);
417
418 if (active) evio_start (w);
419 }
420 }
421 OUTPUT:
422 RETVAL
423
424 short events (struct ev_io *w, short new_events = EV_UNDEF)
425 CODE:
426 {
427 RETVAL = w->events;
428
429 if (items > 1)
430 {
431 int active = w->active;
432 if (active) evio_stop (w);
433
434 evio_set (w, w->fd, new_events);
435
436 if (active) evio_start (w);
437 }
438 }
439 OUTPUT:
440 RETVAL
441
442 MODULE = EV PACKAGE = EV::Signal PREFIX = evsignal_
443
444 void evsignal_start (struct ev_signal *w)
445
446 void evsignal_stop (struct ev_signal *w)
447
448 void set (struct ev_signal *w, SV *signal = 0)
449 CODE:
450 {
451 Signal signum = sv_signum (signal); /* may croak here */
452 int active = w->active;
453
454 if (active) evsignal_stop (w);
455 evsignal_set (w, signum);
456 if (active) evsignal_start (w);
457 }
458
459 MODULE = EV PACKAGE = EV::Time
460
461 MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_
462
463 void evtimer_start (struct ev_timer *w)
464 INIT:
465 CHECK_REPEAT (w->repeat);
466
467 void evtimer_stop (struct ev_timer *w)
468
469 void evtimer_again (struct ev_timer *w)
470 INIT:
471 CHECK_REPEAT (w->repeat);
472
473 void set (struct ev_timer *w, NV after, NV repeat = 0.)
474 INIT:
475 CHECK_REPEAT (repeat);
476 CODE:
477 {
478 int active = w->active;
479 if (active) evtimer_stop (w);
480 evtimer_set (w, after, repeat);
481 if (active) evtimer_start (w);
482 }
483
484 MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_
485
486 void evperiodic_start (struct ev_periodic *w)
487 INIT:
488 CHECK_REPEAT (w->interval);
489
490 void evperiodic_stop (struct ev_periodic *w)
491
492 void set (struct ev_periodic *w, NV at, NV interval = 0.)
493 INIT:
494 CHECK_REPEAT (interval);
495 CODE:
496 {
497 int active = w->active;
498 if (active) evperiodic_stop (w);
499 evperiodic_set (w, at, interval);
500 if (active) evperiodic_start (w);
501 }
502
503 MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_
504
505 void evidle_start (struct ev_idle *w)
506
507 void evidle_stop (struct ev_idle *w)
508
509 MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_
510
511 void evcheck_start (struct ev_check *w)
512
513 void evcheck_stop (struct ev_check *w)
514
515 #if 0
516
517 MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
518
519 BOOT:
520 {
521 HV *stash = gv_stashpv ("EV::DNS", 1);
522
523 static const struct {
524 const char *name;
525 IV iv;
526 } *civ, const_iv[] = {
527 # define const_iv(pfx, name) { # name, (IV) pfx ## name },
528 const_iv (DNS_, ERR_NONE)
529 const_iv (DNS_, ERR_FORMAT)
530 const_iv (DNS_, ERR_SERVERFAILED)
531 const_iv (DNS_, ERR_NOTEXIST)
532 const_iv (DNS_, ERR_NOTIMPL)
533 const_iv (DNS_, ERR_REFUSED)
534 const_iv (DNS_, ERR_TRUNCATED)
535 const_iv (DNS_, ERR_UNKNOWN)
536 const_iv (DNS_, ERR_TIMEOUT)
537 const_iv (DNS_, ERR_SHUTDOWN)
538 const_iv (DNS_, IPv4_A)
539 const_iv (DNS_, PTR)
540 const_iv (DNS_, IPv6_AAAA)
541 const_iv (DNS_, QUERY_NO_SEARCH)
542 const_iv (DNS_, OPTION_SEARCH)
543 const_iv (DNS_, OPTION_NAMESERVERS)
544 const_iv (DNS_, OPTION_MISC)
545 const_iv (DNS_, OPTIONS_ALL)
546 const_iv (DNS_, NO_SEARCH)
547 };
548
549 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
550 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
551 }
552
553 int evdns_init ()
554
555 void evdns_shutdown (int fail_requests = 1)
556
557 const char *evdns_err_to_string (int err)
558
559 int evdns_nameserver_add (U32 address)
560
561 int evdns_count_nameservers ()
562
563 int evdns_clear_nameservers_and_suspend ()
564
565 int evdns_resume ()
566
567 int evdns_nameserver_ip_add (char *ip_as_string)
568
569 int evdns_resolve_ipv4 (const char *name, int flags, SV *cb)
570 C_ARGS: name, flags, dns_cb, (void *)SvREFCNT_inc (cb)
571
572 int evdns_resolve_ipv6 (const char *name, int flags, SV *cb)
573 C_ARGS: name, flags, dns_cb, (void *)SvREFCNT_inc (cb)
574
575 int evdns_resolve_reverse (SV *addr, int flags, SV *cb)
576 ALIAS:
577 evdns_resolve_reverse_ipv6 = 1
578 CODE:
579 {
580 STRLEN len;
581 char *data = SvPVbyte (addr, len);
582 if (len != (ix ? 16 : 4))
583 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
584
585 RETVAL = ix
586 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb))
587 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
588 }
589 OUTPUT:
590 RETVAL
591
592 int evdns_set_option (char *option, char *val, int flags)
593
594 int evdns_resolv_conf_parse (int flags, const char *filename)
595
596 #ifdef MS_WINDOWS
597
598 int evdns_config_windows_nameservers ()
599
600 #endif
601
602 void evdns_search_clear ()
603
604 void evdns_search_add (char *domain)
605
606 void evdns_search_ndots_set (int ndots)
607
608
609 MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
610
611 BOOT:
612 {
613 HV *stash = gv_stashpv ("EV::HTTP", 1);
614
615 static const struct {
616 const char *name;
617 IV iv;
618 } *civ, const_iv[] = {
619 # define const_iv(pfx, name) { # name, (IV) pfx ## name },
620 const_iv (HTTP_, OK)
621 const_iv (HTTP_, NOCONTENT)
622 const_iv (HTTP_, MOVEPERM)
623 const_iv (HTTP_, MOVETEMP)
624 const_iv (HTTP_, NOTMODIFIED)
625 const_iv (HTTP_, BADREQUEST)
626 const_iv (HTTP_, NOTFOUND)
627 const_iv (HTTP_, SERVUNAVAIL)
628 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
629 const_iv (EVHTTP_, PROXY_REQUEST)
630 const_iv (EVHTTP_, REQ_GET)
631 const_iv (EVHTTP_, REQ_POST)
632 const_iv (EVHTTP_, REQ_HEAD)
633 const_iv (EVHTTP_, REQUEST)
634 const_iv (EVHTTP_, RESPONSE)
635 };
636
637 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
638 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
639 }
640
641 MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_
642
643 #HttpRequest new (SV *klass, SV *cb)
644
645 #void DESTROY (struct evhttp_request *req);
646
647 #endif
648
649
650
651
652
653
654
655