ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
(Generate patch)

Comparing EV/EV.xs (file contents):
Revision 1.1 by root, Fri Oct 26 16:50:05 2007 UTC vs.
Revision 1.14 by root, Tue Oct 30 12:48:29 2007 UTC

7 7
8#include <sys/time.h> 8#include <sys/time.h>
9#include <time.h> 9#include <time.h>
10#include <event.h> 10#include <event.h>
11#include <evdns.h> 11#include <evdns.h>
12/*include <evhttp.h>*/ /* does not compile */ 12
13/* workaround for evhttp.h requiring obscure bsd headers */
14#ifndef TAILQ_ENTRY
15#define TAILQ_ENTRY(type) \
16struct { \
17 struct type *tqe_next; /* next element */ \
18 struct type **tqe_prev; /* address of previous next element */ \
19}
20#endif /* !TAILQ_ENTRY */
21#include <evhttp.h>
13 22
14#define EV_NONE 0 23#define EV_NONE 0
15#define EV_UNDEF -1 24#define EV_UNDEF -1
16 25
17#define TIMEOUT_NONE HUGE_VAL 26#define TIMEOUT_NONE HUGE_VAL
18 27
28#include "EV/EVAPI.h"
29
19typedef struct event_base *Base; 30typedef struct event_base *Base;
31typedef int Signal;
32
33static struct EVAPI evapi;
20 34
21static HV *stash_base, *stash_event; 35static HV *stash_base, *stash_event;
22 36
23static double tv_get (struct timeval *tv) 37static double tv_get (struct timeval *tv)
24{ 38{
28static void tv_set (struct timeval *tv, double val) 42static void tv_set (struct timeval *tv, double val)
29{ 43{
30 tv->tv_sec = (long)val; 44 tv->tv_sec = (long)val;
31 tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6); 45 tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6);
32 46
47}
48
49static int
50sv_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
64static void
65api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg)
66{
67 if (timeout >= 0.)
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);
33} 75}
34 76
35///////////////////////////////////////////////////////////////////////////// 77/////////////////////////////////////////////////////////////////////////////
36// Event 78// Event
37 79
44 unsigned char active; 86 unsigned char active;
45 unsigned char abstime; 87 unsigned char abstime;
46} *Event; 88} *Event;
47 89
48static double 90static double
49e_now () 91e_now (void)
50{ 92{
51 struct timeval tv; 93 struct timeval tv;
52 gettimeofday (&tv, 0); 94 gettimeofday (&tv, 0);
53 95
54 return tv_get (&tv); 96 return tv_get (&tv);
56 98
57static void e_cb (int fd, short events, void *arg); 99static void e_cb (int fd, short events, void *arg);
58 100
59static int sv_fileno (SV *fh) 101static int sv_fileno (SV *fh)
60{ 102{
61 if (fh)
62 {
63 SvGETMAGIC (fh); 103 SvGETMAGIC (fh);
64 104
65 if (SvROK (fh)) 105 if (SvROK (fh))
66 fh = SvRV (fh); 106 fh = SvRV (fh);
67 107
68 if (SvTYPE (fh) == SVt_PVGV) 108 if (SvTYPE (fh) == SVt_PVGV)
69 return PerlIO_fileno (IoIFP (sv_2io (fh))); 109 return PerlIO_fileno (IoIFP (sv_2io (fh)));
70 110
71 if (SvIOK (fh)) 111 if (SvIOK (fh))
72 return SvIV (fh); 112 return SvIV (fh);
73 }
74 113
75 return -1; 114 return -1;
76} 115}
77 116
78static Event 117static Event
110 149
111 if (ev->abstime) 150 if (ev->abstime)
112 { 151 {
113 double now = e_now (); 152 double now = e_now ();
114 153
115 if (now > to && ev->interval) 154 if (ev->interval)
116 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); 155 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval);
117 156
118 to -= now; 157 to -= now;
119 } 158 }
120 else if (to < 0.) 159 else if (to < 0.)
159static void 198static void
160e_cb (int fd, short events, void *arg) 199e_cb (int fd, short events, void *arg)
161{ 200{
162 struct ev *ev = (struct ev*)arg; 201 struct ev *ev = (struct ev*)arg;
163 dSP; 202 dSP;
203 I32 mark = SP - PL_stack_base;
204 SV *sv_self, *sv_events;
205 static SV *sv_events_cache;
164 206
165 ENTER;
166 SAVETMPS;
167
168 if (!(ev->ev.ev_events & EV_PERSIST)) 207 if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT))
169 ev->active = 0; 208 ev->active = 0;
209
210 sv_self = e_self (ev);
211
212 if (sv_events_cache)
213 {
214 sv_events = sv_events_cache; sv_events_cache = 0;
215 SvIV_set (sv_events, events);
216 }
217 else
218 sv_events = newSViv (events);
170 219
171 PUSHMARK (SP); 220 PUSHMARK (SP);
172 EXTEND (SP, 2); 221 EXTEND (SP, 2);
173 PUSHs (sv_2mortal (e_self (ev))); 222 PUSHs (sv_self);
174 PUSHs (sv_2mortal (newSViv (events))); 223 PUSHs (sv_events);
175 PUTBACK; 224 PUTBACK;
176 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 225 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
177 /*TODO: if err, call some logging function */ 226 SP = PL_stack_base + mark; PUTBACK;
227
228 SvREFCNT_dec (sv_self);
229
230 if (sv_events_cache)
231 SvREFCNT_dec (sv_events);
232 else
233 sv_events_cache = sv_events;
178 234
179 if (ev->interval && !ev->active) 235 if (ev->interval && !ev->active)
180 e_start (ev); 236 e_start (ev);
181 237
182 FREETMPS; 238 if (SvTRUE (ERRSV))
183 LEAVE; 239 {
240 PUSHMARK (SP);
241 PUTBACK;
242 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
243 SP = PL_stack_base + mark; PUTBACK;
244 }
184} 245}
185 246
186///////////////////////////////////////////////////////////////////////////// 247/////////////////////////////////////////////////////////////////////////////
187// DNS 248// DNS
188 249
222 283
223 PUTBACK; 284 PUTBACK;
224 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 285 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
225 286
226 FREETMPS; 287 FREETMPS;
288
289 if (SvTRUE (ERRSV))
290 {
291 PUSHMARK (SP);
292 PUTBACK;
293 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
294 }
295
227 LEAVE; 296 LEAVE;
228} 297}
229 298
230///////////////////////////////////////////////////////////////////////////// 299/////////////////////////////////////////////////////////////////////////////
231// XS interface functions 300// XS interface functions
232 301
233MODULE = EV PACKAGE = EV PREFIX = event_ 302MODULE = EV PACKAGE = EV PREFIX = event_
234 303
235BOOT: 304BOOT:
236{ 305{
306 int i;
237 HV *stash = gv_stashpv ("EV", 1); 307 HV *stash = gv_stashpv ("EV", 1);
238 308
239 static const struct { 309 static const struct {
240 const char *name; 310 const char *name;
241 IV iv; 311 IV iv;
259 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 329 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
260 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 330 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
261 331
262 stash_base = gv_stashpv ("EV::Base" , 1); 332 stash_base = gv_stashpv ("EV::Base" , 1);
263 stash_event = gv_stashpv ("EV::Event", 1); 333 stash_event = gv_stashpv ("EV::Event", 1);
334
335 {
336 SV *sv = perl_get_sv ("EV::API", TRUE);
337 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
338
339 evapi.ver = EV_API_VERSION;
340 evapi.rev = EV_API_REVISION;
341 evapi.now = e_now;
342 evapi.once = api_once;
343 evapi.loop = event_loop;
344
345 sv_setiv (sv, (IV)&evapi);
346 SvREADONLY_on (sv);
347 }
264} 348}
265 349
266double now () 350double now ()
267 CODE: 351 CODE:
268 RETVAL = e_now (); 352 RETVAL = e_now ();
283 367
284int event_dispatch () 368int event_dispatch ()
285 369
286int event_loop (int flags = 0) 370int event_loop (int flags = 0)
287 371
288int event_loopexit (double after) 372int event_loopexit (double after = 0)
289 CODE: 373 CODE:
290{ 374{
291 struct timeval tv; 375 struct timeval tv;
292 tv_set (&tv, after); 376 tv_set (&tv, after);
293 event_loopexit (&tv); 377 event_loopexit (&tv);
294} 378}
295 379
296Event event (SV *cb) 380Event event (SV *cb)
297 CODE: 381 CODE:
298 RETVAL = e_new (0, 0, cb); 382 RETVAL = e_new (NEWSV (0, 0), 0, cb);
299 OUTPUT: 383 OUTPUT:
300 RETVAL 384 RETVAL
301 385
302Event io (SV *fh, short events, SV *cb) 386Event io (SV *fh, short events, SV *cb)
303 ALIAS: 387 ALIAS:
306 RETVAL = e_new (fh, events, cb); 390 RETVAL = e_new (fh, events, cb);
307 if (!ix) e_start (RETVAL); 391 if (!ix) e_start (RETVAL);
308 OUTPUT: 392 OUTPUT:
309 RETVAL 393 RETVAL
310 394
395Event timed_io (SV *fh, short events, double timeout, SV *cb)
396 ALIAS:
397 timed_io_ns = 1
398 CODE:
399{
400 events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST;
401
402 RETVAL = e_new (fh, events, cb);
403
404 if (timeout)
405 {
406 RETVAL->timeout = timeout;
407 RETVAL->interval = 1;
408 }
409
410 if (!ix) e_start (RETVAL);
411}
412 OUTPUT:
413 RETVAL
414
311Event timer (double after, int repeat, SV *cb) 415Event timer (double after, int repeat, SV *cb)
312 ALIAS: 416 ALIAS:
313 timer_ns = 1 417 timer_ns = 1
314 CODE: 418 CODE:
315 RETVAL = e_new (0, 0, cb); 419 RETVAL = e_new (NEWSV (0, 0), 0, cb);
316 RETVAL->timeout = after; 420 RETVAL->timeout = after;
317 RETVAL->interval = repeat; 421 RETVAL->interval = repeat;
318 if (!ix) e_start (RETVAL); 422 if (!ix) e_start (RETVAL);
319 OUTPUT: 423 OUTPUT:
320 RETVAL 424 RETVAL
321 425
322Event timer_abs (double at, double interval, SV *cb) 426Event timer_abs (double at, double interval, SV *cb)
323 ALIAS: 427 ALIAS:
324 timer_abs_ns = 1 428 timer_abs_ns = 1
325 CODE: 429 CODE:
326 RETVAL = e_new (0, 0, cb); 430 RETVAL = e_new (NEWSV (0, 0), 0, cb);
327 RETVAL->timeout = at; 431 RETVAL->timeout = at;
328 RETVAL->interval = interval; 432 RETVAL->interval = interval;
329 RETVAL->abstime = 1; 433 RETVAL->abstime = 1;
330 if (!ix) e_start (RETVAL); 434 if (!ix) e_start (RETVAL);
331 OUTPUT: 435 OUTPUT:
332 RETVAL 436 RETVAL
333 437
334Event signal (SV *signal, SV *cb) 438Event signal (Signal signum, SV *cb)
335 ALIAS: 439 ALIAS:
336 signal_ns = 1 440 signal_ns = 1
337 CODE: 441 CODE:
338 RETVAL = e_new (signal, EV_SIGNAL | EV_PERSIST, cb); 442 RETVAL = e_new (ST (0), EV_SIGNAL | EV_PERSIST, cb);
443 RETVAL->ev.ev_fd = signum;
339 if (!ix) e_start (RETVAL); 444 if (!ix) e_start (RETVAL);
340 OUTPUT: 445 OUTPUT:
341 RETVAL 446 RETVAL
342 447
343PROTOTYPES: DISABLE 448PROTOTYPES: DISABLE
405 CODE: 510 CODE:
406 e_stop (ev); 511 e_stop (ev);
407 SvREFCNT_dec (ev->cb); 512 SvREFCNT_dec (ev->cb);
408 SvREFCNT_dec (ev->fh); 513 SvREFCNT_dec (ev->fh);
409 514
410void cb (Event ev, SV *new_cb) 515SV *cb (Event ev, SV *new_cb = 0)
411 CODE: 516 CODE:
517 RETVAL = newSVsv (ev->cb);
518 if (items > 1)
412 sv_setsv (ev->cb, new_cb); 519 sv_setsv (ev->cb, new_cb);
520 OUTPUT:
521 RETVAL
413 522
414SV *fh (Event ev, SV *new_fh = 0) 523SV *fh (Event ev, SV *new_fh = 0)
415 ALIAS:
416 signal = 0
417 CODE: 524 CODE:
418 RETVAL = newSVsv (ev->fh); 525 RETVAL = newSVsv (ev->fh);
419 if (items > 1) 526 if (items > 1)
420 { 527 {
421 if (ev->active) event_del (&ev->ev); 528 if (ev->active) event_del (&ev->ev);
422 sv_setsv (ev->fh, new_fh); 529 sv_setsv (ev->fh, new_fh);
423 ev->ev.ev_fd = e_fd (ev); 530 ev->ev.ev_fd = sv_fileno (ev->fh);
531 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
424 if (ev->active) event_add (&ev->ev, e_tv (ev)); 532 if (ev->active) event_add (&ev->ev, e_tv (ev));
425 } 533 }
534 OUTPUT:
535 RETVAL
536
537SV *signal (Event ev, SV *new_signal = 0)
538 CODE:
539{
540 Signal signum;
541
542 if (items > 1)
543 signum = sv_signum (new_signal); /* may croak here */
544
545 RETVAL = newSVsv (ev->fh);
546
547 if (items > 1)
548 {
549 if (ev->active) event_del (&ev->ev);
550 sv_setsv (ev->fh, new_signal);
551 ev->ev.ev_fd = signum;
552 ev->ev.ev_events |= EV_SIGNAL;
553 if (ev->active) event_add (&ev->ev, e_tv (ev));
554 }
555}
426 OUTPUT: 556 OUTPUT:
427 RETVAL 557 RETVAL
428 558
429short events (Event ev, short new_events = EV_UNDEF) 559short events (Event ev, short new_events = EV_UNDEF)
430 CODE: 560 CODE:
470 static const struct { 600 static const struct {
471 const char *name; 601 const char *name;
472 IV iv; 602 IV iv;
473 } *civ, const_iv[] = { 603 } *civ, const_iv[] = {
474# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 604# define const_iv(pfx, name) { # name, (IV) pfx ## name },
475
476 const_iv (DNS_, ERR_NONE) 605 const_iv (DNS_, ERR_NONE)
477 const_iv (DNS_, ERR_FORMAT) 606 const_iv (DNS_, ERR_FORMAT)
478 const_iv (DNS_, ERR_SERVERFAILED) 607 const_iv (DNS_, ERR_SERVERFAILED)
479 const_iv (DNS_, ERR_NOTEXIST) 608 const_iv (DNS_, ERR_NOTEXIST)
480 const_iv (DNS_, ERR_NOTIMPL) 609 const_iv (DNS_, ERR_NOTIMPL)
526 CODE: 655 CODE:
527{ 656{
528 STRLEN len; 657 STRLEN len;
529 char *data = SvPVbyte (addr, len); 658 char *data = SvPVbyte (addr, len);
530 if (len != (ix ? 16 : 4)) 659 if (len != (ix ? 16 : 4))
531 croak ("ipv4/ipv6 address to resolve must be given as 4/16 byte octet string"); 660 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
532 661
533 RETVAL = ix 662 RETVAL = ix
534 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) 663 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb))
535 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)); 664 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
536} 665}
551 680
552void evdns_search_add (char *domain) 681void evdns_search_add (char *domain)
553 682
554void evdns_search_ndots_set (int ndots) 683void evdns_search_ndots_set (int ndots)
555 684
685
686MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
687
688BOOT:
689{
690 HV *stash = gv_stashpv ("EV::HTTP", 1);
691
692 static const struct {
693 const char *name;
694 IV iv;
695 } *civ, const_iv[] = {
696# define const_iv(pfx, name) { # name, (IV) pfx ## name },
697 const_iv (HTTP_, OK)
698 const_iv (HTTP_, NOCONTENT)
699 const_iv (HTTP_, MOVEPERM)
700 const_iv (HTTP_, MOVETEMP)
701 const_iv (HTTP_, NOTMODIFIED)
702 const_iv (HTTP_, BADREQUEST)
703 const_iv (HTTP_, NOTFOUND)
704 const_iv (HTTP_, SERVUNAVAIL)
705 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
706 const_iv (EVHTTP_, PROXY_REQUEST)
707 const_iv (EVHTTP_, REQ_GET)
708 const_iv (EVHTTP_, REQ_POST)
709 const_iv (EVHTTP_, REQ_HEAD)
710 const_iv (EVHTTP_, REQUEST)
711 const_iv (EVHTTP_, RESPONSE)
712 };
713
714 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
715 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
716}
717
718MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_
719
720#HttpRequest new (SV *klass, SV *cb)
721
722#void DESTROY (struct evhttp_request *req);
723
724
725
726
727
728
729
730

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines