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

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5     #include <math.h>
6     #include <netinet/in.h>
7    
8 root 1.15 #define TIMEOUT_NONE HUGE_VAL
9 root 1.1
10 root 1.15 #define EV_COMMON \
11     SV *self; /* contains this struct */ \
12     SV *cb_sv, *fh;
13 root 1.1
14 root 1.11 #include "EV/EVAPI.h"
15    
16 root 1.10 typedef int Signal;
17 root 1.1
18 root 1.11 static struct EVAPI evapi;
19    
20 root 1.15 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 root 1.1
30 root 1.10 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 root 1.11 static void
46     api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg)
47     {
48 root 1.15 #if 0
49 root 1.12 if (timeout >= 0.)
50 root 1.11 {
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 root 1.15 #endif
58 root 1.11 }
59    
60 root 1.1 /////////////////////////////////////////////////////////////////////////////
61     // Event
62    
63 root 1.15 static void e_cb (struct ev_watcher *w, int revents);
64 root 1.1
65 root 1.15 static int
66     sv_fileno (SV *fh)
67 root 1.1 {
68 root 1.3 SvGETMAGIC (fh);
69 root 1.1
70 root 1.3 if (SvROK (fh))
71     fh = SvRV (fh);
72 root 1.1
73 root 1.3 if (SvTYPE (fh) == SVt_PVGV)
74     return PerlIO_fileno (IoIFP (sv_2io (fh)));
75 root 1.1
76 root 1.3 if (SvIOK (fh))
77     return SvIV (fh);
78 root 1.1
79     return -1;
80     }
81    
82 root 1.15 static void *
83     e_new (int size, SV *cb_sv)
84 root 1.1 {
85 root 1.15 struct ev_watcher *w;
86     SV *self = NEWSV (0, size);
87 root 1.1 SvPOK_only (self);
88 root 1.15 SvCUR_set (self, size);
89 root 1.1
90 root 1.15 w = (struct ev_watcher *)SvPVX (self);
91 root 1.1
92 root 1.15 evw_init (w, e_cb);
93 root 1.1
94 root 1.15 w->fh = 0;
95     w->cb_sv = newSVsv (cb_sv);
96     w->self = self;
97 root 1.1
98 root 1.15 return (void *)w;
99 root 1.1 }
100    
101 root 1.15 static SV *
102     e_bless (struct ev_watcher *w, HV *stash)
103 root 1.1 {
104     SV *rv;
105    
106 root 1.15 if (SvOBJECT (w->self))
107     rv = newRV_inc (w->self);
108 root 1.1 else
109     {
110 root 1.15 rv = newRV_noinc (w->self);
111     sv_bless (rv, stash);
112     SvREADONLY_on (w->self);
113 root 1.1 }
114    
115     return rv;
116     }
117    
118     static void
119 root 1.15 e_cb (struct ev_watcher *w, int revents)
120 root 1.1 {
121     dSP;
122 root 1.14 I32 mark = SP - PL_stack_base;
123     SV *sv_self, *sv_events;
124     static SV *sv_events_cache;
125 root 1.1
126 root 1.15 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
127 root 1.14
128     if (sv_events_cache)
129     {
130     sv_events = sv_events_cache; sv_events_cache = 0;
131 root 1.15 SvIV_set (sv_events, revents);
132 root 1.14 }
133     else
134 root 1.15 sv_events = newSViv (revents);
135 root 1.14
136 root 1.1 PUSHMARK (SP);
137     EXTEND (SP, 2);
138 root 1.14 PUSHs (sv_self);
139     PUSHs (sv_events);
140 root 1.1 PUTBACK;
141 root 1.15 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
142 root 1.14 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 root 1.1
151 root 1.8 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 root 1.14 SP = PL_stack_base + mark; PUTBACK;
157 root 1.8 }
158 root 1.1 }
159    
160 root 1.15 #if 0
161 root 1.1 /////////////////////////////////////////////////////////////////////////////
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 root 1.8
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 root 1.1 LEAVE;
211     }
212 root 1.15 #endif
213 root 1.1
214     /////////////////////////////////////////////////////////////////////////////
215     // XS interface functions
216    
217 root 1.15 MODULE = EV PACKAGE = EV PREFIX = ev_
218 root 1.1
219     BOOT:
220     {
221 root 1.10 int i;
222 root 1.1 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 root 1.15 const_iv (EV_, IDLE)
235     const_iv (EV_, CHECK)
236    
237     const_iv (EV, LOOP_ONESHOT)
238 root 1.1 const_iv (EV, LOOP_NONBLOCK)
239 root 1.15
240     const_iv (EV, METHOD_NONE)
241     const_iv (EV, METHOD_SELECT)
242     const_iv (EV, METHOD_EPOLL)
243 root 1.1 };
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 root 1.15 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 root 1.11
257     {
258     SV *sv = perl_get_sv ("EV::API", TRUE);
259     perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
260    
261 root 1.15 /* 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 root 1.11
285     sv_setiv (sv, (IV)&evapi);
286     SvREADONLY_on (sv);
287     }
288 root 1.1 }
289    
290 root 1.15 NV ev_now ()
291 root 1.1 CODE:
292 root 1.15 RETVAL = ev_now;
293 root 1.1 OUTPUT:
294     RETVAL
295    
296 root 1.15 int ev_method ()
297 root 1.1 CODE:
298 root 1.15 RETVAL = ev_method;
299 root 1.1 OUTPUT:
300     RETVAL
301    
302 root 1.15 NV ev_time ()
303 root 1.1
304 root 1.15 void ev_init (int flags = 0)
305 root 1.1
306 root 1.15 void ev_loop (int flags = 0)
307 root 1.1
308 root 1.15 void ev_loop_done (int value = 1)
309 root 1.1 CODE:
310 root 1.15 ev_loop_done = value;
311 root 1.1
312 root 1.15 struct ev_io *io (SV *fh, int events, SV *cb)
313     ALIAS:
314     io_ns = 1
315 root 1.1 CODE:
316 root 1.15 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 root 1.1 OUTPUT:
321     RETVAL
322    
323 root 1.15 struct ev_timer *timer (NV after, NV repeat, SV *cb)
324 root 1.1 ALIAS:
325 root 1.15 timer_ns = 1
326 root 1.1 CODE:
327 root 1.15 RETVAL = e_new (sizeof (struct ev_timer), cb);
328     evtimer_set (RETVAL, after, repeat);
329     if (!ix) evtimer_start (RETVAL);
330 root 1.1 OUTPUT:
331     RETVAL
332    
333 root 1.15 struct ev_periodic *periodic (NV at, NV interval, SV *cb)
334 root 1.9 ALIAS:
335 root 1.15 periodic_ns = 1
336 root 1.9 CODE:
337 root 1.15 RETVAL = e_new (sizeof (struct ev_periodic), cb);
338     evperiodic_set (RETVAL, at, interval);
339     if (!ix) evperiodic_start (RETVAL);
340 root 1.9 OUTPUT:
341     RETVAL
342    
343 root 1.15 struct ev_signal *signal (Signal signum, SV *cb)
344 root 1.1 ALIAS:
345 root 1.15 signal_ns = 1
346 root 1.1 CODE:
347 root 1.15 RETVAL = e_new (sizeof (struct ev_signal), cb);
348     evsignal_set (RETVAL, signum);
349     if (!ix) evsignal_start (RETVAL);
350 root 1.1 OUTPUT:
351     RETVAL
352    
353 root 1.15 struct ev_idle *idle (SV *cb)
354 root 1.1 ALIAS:
355 root 1.15 idle_ns = 1
356 root 1.1 CODE:
357 root 1.15 RETVAL = e_new (sizeof (struct ev_idle), cb);
358     evidle_set (RETVAL);
359     if (!ix) evidle_start (RETVAL);
360 root 1.1 OUTPUT:
361     RETVAL
362    
363 root 1.15 struct ev_check *check (SV *cb)
364 root 1.1 ALIAS:
365 root 1.15 check_ns = 1
366 root 1.1 CODE:
367 root 1.15 RETVAL = e_new (sizeof (struct ev_check), cb);
368     evcheck_set (RETVAL);
369     if (!ix) evcheck_start (RETVAL);
370 root 1.1 OUTPUT:
371     RETVAL
372    
373 root 1.15
374 root 1.1 PROTOTYPES: DISABLE
375    
376 root 1.15 MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
377 root 1.1
378 root 1.15 int ev_is_active (struct ev_watcher *w)
379 root 1.1
380 root 1.15 SV *cb (struct ev_watcher *w, SV *new_cb = 0)
381 root 1.1 CODE:
382 root 1.15 {
383     RETVAL = newSVsv (w->cb_sv);
384    
385     if (items > 1)
386     sv_setsv (w->cb_sv, new_cb);
387     }
388 root 1.1 OUTPUT:
389     RETVAL
390    
391 root 1.15 MODULE = EV PACKAGE = EV::IO PREFIX = evio_
392 root 1.1
393 root 1.15 void evio_start (struct ev_io *w)
394 root 1.1
395 root 1.15 void evio_stop (struct ev_io *w)
396    
397     void set (struct ev_io *w, SV *fh, int events)
398 root 1.1 CODE:
399     {
400 root 1.15 int active = w->active;
401     if (active) evio_stop (w);
402 root 1.1
403 root 1.15 sv_setsv (w->fh, fh);
404     evio_set (w, sv_fileno (w->fh), events);
405 root 1.1
406 root 1.15 if (active) evio_start (w);
407     }
408 root 1.1
409 root 1.15 SV *fh (struct ev_io *w, SV *new_fh = 0)
410 root 1.1 CODE:
411 root 1.15 {
412     RETVAL = newSVsv (w->fh);
413 root 1.1
414 root 1.15 if (items > 1)
415     {
416     int active = w->active;
417     if (active) evio_stop (w);
418 root 1.1
419 root 1.15 sv_setsv (w->fh, new_fh);
420     evio_set (w, sv_fileno (w->fh), w->events);
421 root 1.1
422 root 1.15 if (active) evio_start (w);
423     }
424     }
425 root 1.1 OUTPUT:
426     RETVAL
427    
428 root 1.15 short events (struct ev_io *w, short new_events = EV_UNDEF)
429 root 1.1 CODE:
430 root 1.15 {
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 root 1.1 OUTPUT:
444     RETVAL
445    
446 root 1.15 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 root 1.1 CODE:
454 root 1.15 {
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 root 1.1
471 root 1.15 void set (struct ev_timer *w, NV after, NV repeat = 0.)
472 root 1.1 CODE:
473 root 1.15 {
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 root 1.1
480 root 1.15 MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_
481    
482     void evperiodic_start (struct ev_periodic *w)
483 root 1.1
484 root 1.15 void evperiodic_stop (struct ev_periodic *w)
485 root 1.1
486 root 1.15 void set (struct ev_periodic *w, NV at, NV interval = 0.)
487 root 1.10 CODE:
488     {
489 root 1.15 int active = w->active;
490     if (active) evperiodic_stop (w);
491     evperiodic_set (w, at, interval);
492     if (active) evperiodic_start (w);
493     }
494 root 1.10
495 root 1.15 MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_
496 root 1.10
497 root 1.15 void evidle_start (struct ev_idle *w)
498 root 1.10
499 root 1.15 void evidle_stop (struct ev_idle *w)
500 root 1.10
501 root 1.15 MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_
502 root 1.1
503 root 1.15 void evcheck_start (struct ev_check *w)
504 root 1.1
505 root 1.15 void evcheck_stop (struct ev_check *w)
506 root 1.1
507 root 1.15 #if 0
508 root 1.1
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 root 1.8 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
576 root 1.1
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 root 1.5
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 root 1.15 #endif
640    
641 root 1.5
642    
643    
644    
645    
646    
647