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

Comparing EV/EV.xs (file contents):
Revision 1.3 by root, Fri Oct 26 17:24:18 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);
107 149
108 if (ev->abstime) 150 if (ev->abstime)
109 { 151 {
110 double now = e_now (); 152 double now = e_now ();
111 153
112 if (now > to && ev->interval) 154 if (ev->interval)
113 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); 155 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval);
114 156
115 to -= now; 157 to -= now;
116 } 158 }
117 else if (to < 0.) 159 else if (to < 0.)
160 dSP; 202 dSP;
161 203
162 ENTER; 204 ENTER;
163 SAVETMPS; 205 SAVETMPS;
164 206
165 if (!(ev->ev.ev_events & EV_PERSIST)) 207 if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT))
166 ev->active = 0; 208 ev->active = 0;
167 209
168 PUSHMARK (SP); 210 PUSHMARK (SP);
169 EXTEND (SP, 2); 211 EXTEND (SP, 2);
170 PUSHs (sv_2mortal (e_self (ev))); 212 PUSHs (sv_2mortal (e_self (ev)));
171 PUSHs (sv_2mortal (newSViv (events))); 213 PUSHs (sv_2mortal (newSViv (events)));
172 PUTBACK; 214 PUTBACK;
173 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 215 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
174 /*TODO: if err, call some logging function */
175 216
176 if (ev->interval && !ev->active) 217 if (ev->interval && !ev->active)
177 e_start (ev); 218 e_start (ev);
178 219
179 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
180 LEAVE; 229 LEAVE;
181} 230}
182 231
183///////////////////////////////////////////////////////////////////////////// 232/////////////////////////////////////////////////////////////////////////////
184// DNS 233// DNS
219 268
220 PUTBACK; 269 PUTBACK;
221 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 270 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
222 271
223 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
224 LEAVE; 281 LEAVE;
225} 282}
226 283
227///////////////////////////////////////////////////////////////////////////// 284/////////////////////////////////////////////////////////////////////////////
228// XS interface functions 285// XS interface functions
229 286
230MODULE = EV PACKAGE = EV PREFIX = event_ 287MODULE = EV PACKAGE = EV PREFIX = event_
231 288
232BOOT: 289BOOT:
233{ 290{
291 int i;
234 HV *stash = gv_stashpv ("EV", 1); 292 HV *stash = gv_stashpv ("EV", 1);
235 293
236 static const struct { 294 static const struct {
237 const char *name; 295 const char *name;
238 IV iv; 296 IV iv;
256 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; )
257 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 315 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
258 316
259 stash_base = gv_stashpv ("EV::Base" , 1); 317 stash_base = gv_stashpv ("EV::Base" , 1);
260 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 }
261} 332}
262 333
263double now () 334double now ()
264 CODE: 335 CODE:
265 RETVAL = e_now (); 336 RETVAL = e_now ();
300 ALIAS: 371 ALIAS:
301 io_ns = 1 372 io_ns = 1
302 CODE: 373 CODE:
303 RETVAL = e_new (fh, events, cb); 374 RETVAL = e_new (fh, events, cb);
304 if (!ix) e_start (RETVAL); 375 if (!ix) e_start (RETVAL);
376 OUTPUT:
377 RETVAL
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}
305 OUTPUT: 396 OUTPUT:
306 RETVAL 397 RETVAL
307 398
308Event timer (double after, int repeat, SV *cb) 399Event timer (double after, int repeat, SV *cb)
309 ALIAS: 400 ALIAS:
326 RETVAL->abstime = 1; 417 RETVAL->abstime = 1;
327 if (!ix) e_start (RETVAL); 418 if (!ix) e_start (RETVAL);
328 OUTPUT: 419 OUTPUT:
329 RETVAL 420 RETVAL
330 421
331Event signal (SV *signal, SV *cb) 422Event signal (Signal signum, SV *cb)
332 ALIAS: 423 ALIAS:
333 signal_ns = 1 424 signal_ns = 1
334 CODE: 425 CODE:
335 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;
336 if (!ix) e_start (RETVAL); 428 if (!ix) e_start (RETVAL);
337 OUTPUT: 429 OUTPUT:
338 RETVAL 430 RETVAL
339 431
340PROTOTYPES: DISABLE 432PROTOTYPES: DISABLE
411 sv_setsv (ev->cb, new_cb); 503 sv_setsv (ev->cb, new_cb);
412 OUTPUT: 504 OUTPUT:
413 RETVAL 505 RETVAL
414 506
415SV *fh (Event ev, SV *new_fh = 0) 507SV *fh (Event ev, SV *new_fh = 0)
416 ALIAS:
417 signal = 0
418 CODE: 508 CODE:
419 RETVAL = newSVsv (ev->fh); 509 RETVAL = newSVsv (ev->fh);
420 if (items > 1) 510 if (items > 1)
421 { 511 {
422 if (ev->active) event_del (&ev->ev); 512 if (ev->active) event_del (&ev->ev);
423 sv_setsv (ev->fh, new_fh); 513 sv_setsv (ev->fh, new_fh);
424 ev->ev.ev_fd = sv_fileno (ev->fh); 514 ev->ev.ev_fd = sv_fileno (ev->fh);
515 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
425 if (ev->active) event_add (&ev->ev, e_tv (ev)); 516 if (ev->active) event_add (&ev->ev, e_tv (ev));
426 } 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}
427 OUTPUT: 540 OUTPUT:
428 RETVAL 541 RETVAL
429 542
430short events (Event ev, short new_events = EV_UNDEF) 543short events (Event ev, short new_events = EV_UNDEF)
431 CODE: 544 CODE:
471 static const struct { 584 static const struct {
472 const char *name; 585 const char *name;
473 IV iv; 586 IV iv;
474 } *civ, const_iv[] = { 587 } *civ, const_iv[] = {
475# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 588# define const_iv(pfx, name) { # name, (IV) pfx ## name },
476
477 const_iv (DNS_, ERR_NONE) 589 const_iv (DNS_, ERR_NONE)
478 const_iv (DNS_, ERR_FORMAT) 590 const_iv (DNS_, ERR_FORMAT)
479 const_iv (DNS_, ERR_SERVERFAILED) 591 const_iv (DNS_, ERR_SERVERFAILED)
480 const_iv (DNS_, ERR_NOTEXIST) 592 const_iv (DNS_, ERR_NOTEXIST)
481 const_iv (DNS_, ERR_NOTIMPL) 593 const_iv (DNS_, ERR_NOTIMPL)
527 CODE: 639 CODE:
528{ 640{
529 STRLEN len; 641 STRLEN len;
530 char *data = SvPVbyte (addr, len); 642 char *data = SvPVbyte (addr, len);
531 if (len != (ix ? 16 : 4)) 643 if (len != (ix ? 16 : 4))
532 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");
533 645
534 RETVAL = ix 646 RETVAL = ix
535 ? 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))
536 : 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));
537} 649}
552 664
553void evdns_search_add (char *domain) 665void evdns_search_add (char *domain)
554 666
555void evdns_search_ndots_set (int ndots) 667void evdns_search_ndots_set (int ndots)
556 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