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.12 by root, Mon Oct 29 08:51:44 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.)
163 dSP; 202 dSP;
164 203
165 ENTER; 204 ENTER;
166 SAVETMPS; 205 SAVETMPS;
167 206
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;
170 209
171 PUSHMARK (SP); 210 PUSHMARK (SP);
172 EXTEND (SP, 2); 211 EXTEND (SP, 2);
173 PUSHs (sv_2mortal (e_self (ev))); 212 PUSHs (sv_2mortal (e_self (ev)));
174 PUSHs (sv_2mortal (newSViv (events))); 213 PUSHs (sv_2mortal (newSViv (events)));
175 PUTBACK; 214 PUTBACK;
176 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 215 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
177 /*TODO: if err, call some logging function */
178 216
179 if (ev->interval && !ev->active) 217 if (ev->interval && !ev->active)
180 e_start (ev); 218 e_start (ev);
181 219
182 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
183 LEAVE; 229 LEAVE;
184} 230}
185 231
186///////////////////////////////////////////////////////////////////////////// 232/////////////////////////////////////////////////////////////////////////////
187// DNS 233// DNS
222 268
223 PUTBACK; 269 PUTBACK;
224 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 270 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
225 271
226 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
227 LEAVE; 281 LEAVE;
228} 282}
229 283
230///////////////////////////////////////////////////////////////////////////// 284/////////////////////////////////////////////////////////////////////////////
231// XS interface functions 285// XS interface functions
232 286
233MODULE = EV PACKAGE = EV PREFIX = event_ 287MODULE = EV PACKAGE = EV PREFIX = event_
234 288
235BOOT: 289BOOT:
236{ 290{
291 int i;
237 HV *stash = gv_stashpv ("EV", 1); 292 HV *stash = gv_stashpv ("EV", 1);
238 293
239 static const struct { 294 static const struct {
240 const char *name; 295 const char *name;
241 IV iv; 296 IV iv;
259 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; )
260 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 315 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
261 316
262 stash_base = gv_stashpv ("EV::Base" , 1); 317 stash_base = gv_stashpv ("EV::Base" , 1);
263 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 }
264} 332}
265 333
266double now () 334double now ()
267 CODE: 335 CODE:
268 RETVAL = e_now (); 336 RETVAL = e_now ();
283 351
284int event_dispatch () 352int event_dispatch ()
285 353
286int event_loop (int flags = 0) 354int event_loop (int flags = 0)
287 355
288int event_loopexit (double after) 356int event_loopexit (double after = 0)
289 CODE: 357 CODE:
290{ 358{
291 struct timeval tv; 359 struct timeval tv;
292 tv_set (&tv, after); 360 tv_set (&tv, after);
293 event_loopexit (&tv); 361 event_loopexit (&tv);
294} 362}
295 363
296Event event (SV *cb) 364Event event (SV *cb)
297 CODE: 365 CODE:
298 RETVAL = e_new (0, 0, cb); 366 RETVAL = e_new (NEWSV (0, 0), 0, cb);
299 OUTPUT: 367 OUTPUT:
300 RETVAL 368 RETVAL
301 369
302Event io (SV *fh, short events, SV *cb) 370Event io (SV *fh, short events, SV *cb)
303 ALIAS: 371 ALIAS:
306 RETVAL = e_new (fh, events, cb); 374 RETVAL = e_new (fh, events, cb);
307 if (!ix) e_start (RETVAL); 375 if (!ix) e_start (RETVAL);
308 OUTPUT: 376 OUTPUT:
309 RETVAL 377 RETVAL
310 378
379Event 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}
396 OUTPUT:
397 RETVAL
398
311Event timer (double after, int repeat, SV *cb) 399Event timer (double after, int repeat, SV *cb)
312 ALIAS: 400 ALIAS:
313 timer_ns = 1 401 timer_ns = 1
314 CODE: 402 CODE:
315 RETVAL = e_new (0, 0, cb); 403 RETVAL = e_new (NEWSV (0, 0), 0, cb);
316 RETVAL->timeout = after; 404 RETVAL->timeout = after;
317 RETVAL->interval = repeat; 405 RETVAL->interval = repeat;
318 if (!ix) e_start (RETVAL); 406 if (!ix) e_start (RETVAL);
319 OUTPUT: 407 OUTPUT:
320 RETVAL 408 RETVAL
321 409
322Event timer_abs (double at, double interval, SV *cb) 410Event timer_abs (double at, double interval, SV *cb)
323 ALIAS: 411 ALIAS:
324 timer_abs_ns = 1 412 timer_abs_ns = 1
325 CODE: 413 CODE:
326 RETVAL = e_new (0, 0, cb); 414 RETVAL = e_new (NEWSV (0, 0), 0, cb);
327 RETVAL->timeout = at; 415 RETVAL->timeout = at;
328 RETVAL->interval = interval; 416 RETVAL->interval = interval;
329 RETVAL->abstime = 1; 417 RETVAL->abstime = 1;
330 if (!ix) e_start (RETVAL); 418 if (!ix) e_start (RETVAL);
331 OUTPUT: 419 OUTPUT:
332 RETVAL 420 RETVAL
333 421
334Event signal (SV *signal, SV *cb) 422Event signal (Signal signum, SV *cb)
335 ALIAS: 423 ALIAS:
336 signal_ns = 1 424 signal_ns = 1
337 CODE: 425 CODE:
338 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;
339 if (!ix) e_start (RETVAL); 428 if (!ix) e_start (RETVAL);
340 OUTPUT: 429 OUTPUT:
341 RETVAL 430 RETVAL
342 431
343PROTOTYPES: DISABLE 432PROTOTYPES: DISABLE
405 CODE: 494 CODE:
406 e_stop (ev); 495 e_stop (ev);
407 SvREFCNT_dec (ev->cb); 496 SvREFCNT_dec (ev->cb);
408 SvREFCNT_dec (ev->fh); 497 SvREFCNT_dec (ev->fh);
409 498
410void cb (Event ev, SV *new_cb) 499SV *cb (Event ev, SV *new_cb = 0)
411 CODE: 500 CODE:
501 RETVAL = newSVsv (ev->cb);
502 if (items > 1)
412 sv_setsv (ev->cb, new_cb); 503 sv_setsv (ev->cb, new_cb);
504 OUTPUT:
505 RETVAL
413 506
414SV *fh (Event ev, SV *new_fh = 0) 507SV *fh (Event ev, SV *new_fh = 0)
415 ALIAS:
416 signal = 0
417 CODE: 508 CODE:
418 RETVAL = newSVsv (ev->fh); 509 RETVAL = newSVsv (ev->fh);
419 if (items > 1) 510 if (items > 1)
420 { 511 {
421 if (ev->active) event_del (&ev->ev); 512 if (ev->active) event_del (&ev->ev);
422 sv_setsv (ev->fh, new_fh); 513 sv_setsv (ev->fh, new_fh);
423 ev->ev.ev_fd = e_fd (ev); 514 ev->ev.ev_fd = sv_fileno (ev->fh);
515 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
424 if (ev->active) event_add (&ev->ev, e_tv (ev)); 516 if (ev->active) event_add (&ev->ev, e_tv (ev));
425 } 517 }
518 OUTPUT:
519 RETVAL
520
521SV *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}
426 OUTPUT: 540 OUTPUT:
427 RETVAL 541 RETVAL
428 542
429short events (Event ev, short new_events = EV_UNDEF) 543short events (Event ev, short new_events = EV_UNDEF)
430 CODE: 544 CODE:
470 static const struct { 584 static const struct {
471 const char *name; 585 const char *name;
472 IV iv; 586 IV iv;
473 } *civ, const_iv[] = { 587 } *civ, const_iv[] = {
474# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 588# define const_iv(pfx, name) { # name, (IV) pfx ## name },
475
476 const_iv (DNS_, ERR_NONE) 589 const_iv (DNS_, ERR_NONE)
477 const_iv (DNS_, ERR_FORMAT) 590 const_iv (DNS_, ERR_FORMAT)
478 const_iv (DNS_, ERR_SERVERFAILED) 591 const_iv (DNS_, ERR_SERVERFAILED)
479 const_iv (DNS_, ERR_NOTEXIST) 592 const_iv (DNS_, ERR_NOTEXIST)
480 const_iv (DNS_, ERR_NOTIMPL) 593 const_iv (DNS_, ERR_NOTIMPL)
526 CODE: 639 CODE:
527{ 640{
528 STRLEN len; 641 STRLEN len;
529 char *data = SvPVbyte (addr, len); 642 char *data = SvPVbyte (addr, len);
530 if (len != (ix ? 16 : 4)) 643 if (len != (ix ? 16 : 4))
531 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");
532 645
533 RETVAL = ix 646 RETVAL = ix
534 ? 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))
535 : 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));
536} 649}
551 664
552void evdns_search_add (char *domain) 665void evdns_search_add (char *domain)
553 666
554void evdns_search_ndots_set (int ndots) 667void evdns_search_ndots_set (int ndots)
555 668
669
670MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
671
672BOOT:
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
702MODULE = 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines