ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
Revision: 1.15
Committed: Wed Oct 31 10:50:05 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
Changes since 1.14: +220 -303 lines
Log Message:
EV first rough cut

File Contents

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