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.13 by root, Mon Oct 29 18:33:02 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 evapi.loop = event_loop;
329
330 sv_setiv (sv, (IV)&evapi);
331 SvREADONLY_on (sv);
332 }
264} 333}
265 334
266double now () 335double now ()
267 CODE: 336 CODE:
268 RETVAL = e_now (); 337 RETVAL = e_now ();
283 352
284int event_dispatch () 353int event_dispatch ()
285 354
286int event_loop (int flags = 0) 355int event_loop (int flags = 0)
287 356
288int event_loopexit (double after) 357int event_loopexit (double after = 0)
289 CODE: 358 CODE:
290{ 359{
291 struct timeval tv; 360 struct timeval tv;
292 tv_set (&tv, after); 361 tv_set (&tv, after);
293 event_loopexit (&tv); 362 event_loopexit (&tv);
294} 363}
295 364
296Event event (SV *cb) 365Event event (SV *cb)
297 CODE: 366 CODE:
298 RETVAL = e_new (0, 0, cb); 367 RETVAL = e_new (NEWSV (0, 0), 0, cb);
299 OUTPUT: 368 OUTPUT:
300 RETVAL 369 RETVAL
301 370
302Event io (SV *fh, short events, SV *cb) 371Event io (SV *fh, short events, SV *cb)
303 ALIAS: 372 ALIAS:
306 RETVAL = e_new (fh, events, cb); 375 RETVAL = e_new (fh, events, cb);
307 if (!ix) e_start (RETVAL); 376 if (!ix) e_start (RETVAL);
308 OUTPUT: 377 OUTPUT:
309 RETVAL 378 RETVAL
310 379
380Event timed_io (SV *fh, short events, double timeout, SV *cb)
381 ALIAS:
382 timed_io_ns = 1
383 CODE:
384{
385 events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST;
386
387 RETVAL = e_new (fh, events, cb);
388
389 if (timeout)
390 {
391 RETVAL->timeout = timeout;
392 RETVAL->interval = 1;
393 }
394
395 if (!ix) e_start (RETVAL);
396}
397 OUTPUT:
398 RETVAL
399
311Event timer (double after, int repeat, SV *cb) 400Event timer (double after, int repeat, SV *cb)
312 ALIAS: 401 ALIAS:
313 timer_ns = 1 402 timer_ns = 1
314 CODE: 403 CODE:
315 RETVAL = e_new (0, 0, cb); 404 RETVAL = e_new (NEWSV (0, 0), 0, cb);
316 RETVAL->timeout = after; 405 RETVAL->timeout = after;
317 RETVAL->interval = repeat; 406 RETVAL->interval = repeat;
318 if (!ix) e_start (RETVAL); 407 if (!ix) e_start (RETVAL);
319 OUTPUT: 408 OUTPUT:
320 RETVAL 409 RETVAL
321 410
322Event timer_abs (double at, double interval, SV *cb) 411Event timer_abs (double at, double interval, SV *cb)
323 ALIAS: 412 ALIAS:
324 timer_abs_ns = 1 413 timer_abs_ns = 1
325 CODE: 414 CODE:
326 RETVAL = e_new (0, 0, cb); 415 RETVAL = e_new (NEWSV (0, 0), 0, cb);
327 RETVAL->timeout = at; 416 RETVAL->timeout = at;
328 RETVAL->interval = interval; 417 RETVAL->interval = interval;
329 RETVAL->abstime = 1; 418 RETVAL->abstime = 1;
330 if (!ix) e_start (RETVAL); 419 if (!ix) e_start (RETVAL);
331 OUTPUT: 420 OUTPUT:
332 RETVAL 421 RETVAL
333 422
334Event signal (SV *signal, SV *cb) 423Event signal (Signal signum, SV *cb)
335 ALIAS: 424 ALIAS:
336 signal_ns = 1 425 signal_ns = 1
337 CODE: 426 CODE:
338 RETVAL = e_new (signal, EV_SIGNAL | EV_PERSIST, cb); 427 RETVAL = e_new (ST (0), EV_SIGNAL | EV_PERSIST, cb);
428 RETVAL->ev.ev_fd = signum;
339 if (!ix) e_start (RETVAL); 429 if (!ix) e_start (RETVAL);
340 OUTPUT: 430 OUTPUT:
341 RETVAL 431 RETVAL
342 432
343PROTOTYPES: DISABLE 433PROTOTYPES: DISABLE
405 CODE: 495 CODE:
406 e_stop (ev); 496 e_stop (ev);
407 SvREFCNT_dec (ev->cb); 497 SvREFCNT_dec (ev->cb);
408 SvREFCNT_dec (ev->fh); 498 SvREFCNT_dec (ev->fh);
409 499
410void cb (Event ev, SV *new_cb) 500SV *cb (Event ev, SV *new_cb = 0)
411 CODE: 501 CODE:
502 RETVAL = newSVsv (ev->cb);
503 if (items > 1)
412 sv_setsv (ev->cb, new_cb); 504 sv_setsv (ev->cb, new_cb);
505 OUTPUT:
506 RETVAL
413 507
414SV *fh (Event ev, SV *new_fh = 0) 508SV *fh (Event ev, SV *new_fh = 0)
415 ALIAS:
416 signal = 0
417 CODE: 509 CODE:
418 RETVAL = newSVsv (ev->fh); 510 RETVAL = newSVsv (ev->fh);
419 if (items > 1) 511 if (items > 1)
420 { 512 {
421 if (ev->active) event_del (&ev->ev); 513 if (ev->active) event_del (&ev->ev);
422 sv_setsv (ev->fh, new_fh); 514 sv_setsv (ev->fh, new_fh);
423 ev->ev.ev_fd = e_fd (ev); 515 ev->ev.ev_fd = sv_fileno (ev->fh);
516 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
424 if (ev->active) event_add (&ev->ev, e_tv (ev)); 517 if (ev->active) event_add (&ev->ev, e_tv (ev));
425 } 518 }
519 OUTPUT:
520 RETVAL
521
522SV *signal (Event ev, SV *new_signal = 0)
523 CODE:
524{
525 Signal signum;
526
527 if (items > 1)
528 signum = sv_signum (new_signal); /* may croak here */
529
530 RETVAL = newSVsv (ev->fh);
531
532 if (items > 1)
533 {
534 if (ev->active) event_del (&ev->ev);
535 sv_setsv (ev->fh, new_signal);
536 ev->ev.ev_fd = signum;
537 ev->ev.ev_events |= EV_SIGNAL;
538 if (ev->active) event_add (&ev->ev, e_tv (ev));
539 }
540}
426 OUTPUT: 541 OUTPUT:
427 RETVAL 542 RETVAL
428 543
429short events (Event ev, short new_events = EV_UNDEF) 544short events (Event ev, short new_events = EV_UNDEF)
430 CODE: 545 CODE:
470 static const struct { 585 static const struct {
471 const char *name; 586 const char *name;
472 IV iv; 587 IV iv;
473 } *civ, const_iv[] = { 588 } *civ, const_iv[] = {
474# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 589# define const_iv(pfx, name) { # name, (IV) pfx ## name },
475
476 const_iv (DNS_, ERR_NONE) 590 const_iv (DNS_, ERR_NONE)
477 const_iv (DNS_, ERR_FORMAT) 591 const_iv (DNS_, ERR_FORMAT)
478 const_iv (DNS_, ERR_SERVERFAILED) 592 const_iv (DNS_, ERR_SERVERFAILED)
479 const_iv (DNS_, ERR_NOTEXIST) 593 const_iv (DNS_, ERR_NOTEXIST)
480 const_iv (DNS_, ERR_NOTIMPL) 594 const_iv (DNS_, ERR_NOTIMPL)
526 CODE: 640 CODE:
527{ 641{
528 STRLEN len; 642 STRLEN len;
529 char *data = SvPVbyte (addr, len); 643 char *data = SvPVbyte (addr, len);
530 if (len != (ix ? 16 : 4)) 644 if (len != (ix ? 16 : 4))
531 croak ("ipv4/ipv6 address to resolve must be given as 4/16 byte octet string"); 645 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
532 646
533 RETVAL = ix 647 RETVAL = ix
534 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) 648 ? 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)); 649 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
536} 650}
551 665
552void evdns_search_add (char *domain) 666void evdns_search_add (char *domain)
553 667
554void evdns_search_ndots_set (int ndots) 668void evdns_search_ndots_set (int ndots)
555 669
670
671MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
672
673BOOT:
674{
675 HV *stash = gv_stashpv ("EV::HTTP", 1);
676
677 static const struct {
678 const char *name;
679 IV iv;
680 } *civ, const_iv[] = {
681# define const_iv(pfx, name) { # name, (IV) pfx ## name },
682 const_iv (HTTP_, OK)
683 const_iv (HTTP_, NOCONTENT)
684 const_iv (HTTP_, MOVEPERM)
685 const_iv (HTTP_, MOVETEMP)
686 const_iv (HTTP_, NOTMODIFIED)
687 const_iv (HTTP_, BADREQUEST)
688 const_iv (HTTP_, NOTFOUND)
689 const_iv (HTTP_, SERVUNAVAIL)
690 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
691 const_iv (EVHTTP_, PROXY_REQUEST)
692 const_iv (EVHTTP_, REQ_GET)
693 const_iv (EVHTTP_, REQ_POST)
694 const_iv (EVHTTP_, REQ_HEAD)
695 const_iv (EVHTTP_, REQUEST)
696 const_iv (EVHTTP_, RESPONSE)
697 };
698
699 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
700 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
701}
702
703MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_
704
705#HttpRequest new (SV *klass, SV *cb)
706
707#void DESTROY (struct evhttp_request *req);
708
709
710
711
712
713
714
715

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines