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.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);
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.)
156static void 198static void
157e_cb (int fd, short events, void *arg) 199e_cb (int fd, short events, void *arg)
158{ 200{
159 struct ev *ev = (struct ev*)arg; 201 struct ev *ev = (struct ev*)arg;
160 dSP; 202 dSP;
203 I32 mark = SP - PL_stack_base;
204 SV *sv_self, *sv_events;
205 static SV *sv_events_cache;
161 206
162 ENTER;
163 SAVETMPS;
164
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;
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);
167 219
168 PUSHMARK (SP); 220 PUSHMARK (SP);
169 EXTEND (SP, 2); 221 EXTEND (SP, 2);
170 PUSHs (sv_2mortal (e_self (ev))); 222 PUSHs (sv_self);
171 PUSHs (sv_2mortal (newSViv (events))); 223 PUSHs (sv_events);
172 PUTBACK; 224 PUTBACK;
173 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 225 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
174 /*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;
175 234
176 if (ev->interval && !ev->active) 235 if (ev->interval && !ev->active)
177 e_start (ev); 236 e_start (ev);
178 237
179 FREETMPS; 238 if (SvTRUE (ERRSV))
180 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 }
181} 245}
182 246
183///////////////////////////////////////////////////////////////////////////// 247/////////////////////////////////////////////////////////////////////////////
184// DNS 248// DNS
185 249
219 283
220 PUTBACK; 284 PUTBACK;
221 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 285 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
222 286
223 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
224 LEAVE; 296 LEAVE;
225} 297}
226 298
227///////////////////////////////////////////////////////////////////////////// 299/////////////////////////////////////////////////////////////////////////////
228// XS interface functions 300// XS interface functions
229 301
230MODULE = EV PACKAGE = EV PREFIX = event_ 302MODULE = EV PACKAGE = EV PREFIX = event_
231 303
232BOOT: 304BOOT:
233{ 305{
306 int i;
234 HV *stash = gv_stashpv ("EV", 1); 307 HV *stash = gv_stashpv ("EV", 1);
235 308
236 static const struct { 309 static const struct {
237 const char *name; 310 const char *name;
238 IV iv; 311 IV iv;
256 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; )
257 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 330 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
258 331
259 stash_base = gv_stashpv ("EV::Base" , 1); 332 stash_base = gv_stashpv ("EV::Base" , 1);
260 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 }
261} 348}
262 349
263double now () 350double now ()
264 CODE: 351 CODE:
265 RETVAL = e_now (); 352 RETVAL = e_now ();
300 ALIAS: 387 ALIAS:
301 io_ns = 1 388 io_ns = 1
302 CODE: 389 CODE:
303 RETVAL = e_new (fh, events, cb); 390 RETVAL = e_new (fh, events, cb);
304 if (!ix) e_start (RETVAL); 391 if (!ix) e_start (RETVAL);
392 OUTPUT:
393 RETVAL
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}
305 OUTPUT: 412 OUTPUT:
306 RETVAL 413 RETVAL
307 414
308Event timer (double after, int repeat, SV *cb) 415Event timer (double after, int repeat, SV *cb)
309 ALIAS: 416 ALIAS:
326 RETVAL->abstime = 1; 433 RETVAL->abstime = 1;
327 if (!ix) e_start (RETVAL); 434 if (!ix) e_start (RETVAL);
328 OUTPUT: 435 OUTPUT:
329 RETVAL 436 RETVAL
330 437
331Event signal (SV *signal, SV *cb) 438Event signal (Signal signum, SV *cb)
332 ALIAS: 439 ALIAS:
333 signal_ns = 1 440 signal_ns = 1
334 CODE: 441 CODE:
335 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;
336 if (!ix) e_start (RETVAL); 444 if (!ix) e_start (RETVAL);
337 OUTPUT: 445 OUTPUT:
338 RETVAL 446 RETVAL
339 447
340PROTOTYPES: DISABLE 448PROTOTYPES: DISABLE
411 sv_setsv (ev->cb, new_cb); 519 sv_setsv (ev->cb, new_cb);
412 OUTPUT: 520 OUTPUT:
413 RETVAL 521 RETVAL
414 522
415SV *fh (Event ev, SV *new_fh = 0) 523SV *fh (Event ev, SV *new_fh = 0)
416 ALIAS:
417 signal = 0
418 CODE: 524 CODE:
419 RETVAL = newSVsv (ev->fh); 525 RETVAL = newSVsv (ev->fh);
420 if (items > 1) 526 if (items > 1)
421 { 527 {
422 if (ev->active) event_del (&ev->ev); 528 if (ev->active) event_del (&ev->ev);
423 sv_setsv (ev->fh, new_fh); 529 sv_setsv (ev->fh, new_fh);
424 ev->ev.ev_fd = sv_fileno (ev->fh); 530 ev->ev.ev_fd = sv_fileno (ev->fh);
531 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
425 if (ev->active) event_add (&ev->ev, e_tv (ev)); 532 if (ev->active) event_add (&ev->ev, e_tv (ev));
426 } 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}
427 OUTPUT: 556 OUTPUT:
428 RETVAL 557 RETVAL
429 558
430short events (Event ev, short new_events = EV_UNDEF) 559short events (Event ev, short new_events = EV_UNDEF)
431 CODE: 560 CODE:
471 static const struct { 600 static const struct {
472 const char *name; 601 const char *name;
473 IV iv; 602 IV iv;
474 } *civ, const_iv[] = { 603 } *civ, const_iv[] = {
475# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 604# define const_iv(pfx, name) { # name, (IV) pfx ## name },
476
477 const_iv (DNS_, ERR_NONE) 605 const_iv (DNS_, ERR_NONE)
478 const_iv (DNS_, ERR_FORMAT) 606 const_iv (DNS_, ERR_FORMAT)
479 const_iv (DNS_, ERR_SERVERFAILED) 607 const_iv (DNS_, ERR_SERVERFAILED)
480 const_iv (DNS_, ERR_NOTEXIST) 608 const_iv (DNS_, ERR_NOTEXIST)
481 const_iv (DNS_, ERR_NOTIMPL) 609 const_iv (DNS_, ERR_NOTIMPL)
527 CODE: 655 CODE:
528{ 656{
529 STRLEN len; 657 STRLEN len;
530 char *data = SvPVbyte (addr, len); 658 char *data = SvPVbyte (addr, len);
531 if (len != (ix ? 16 : 4)) 659 if (len != (ix ? 16 : 4))
532 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");
533 661
534 RETVAL = ix 662 RETVAL = ix
535 ? 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))
536 : 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));
537} 665}
552 680
553void evdns_search_add (char *domain) 681void evdns_search_add (char *domain)
554 682
555void evdns_search_ndots_set (int ndots) 683void evdns_search_ndots_set (int ndots)
556 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