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.10 by root, Mon Oct 29 07:56:03 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
19typedef struct event_base *Base; 28typedef struct event_base *Base;
29typedef int Signal;
20 30
21static HV *stash_base, *stash_event; 31static HV *stash_base, *stash_event;
22 32
23static double tv_get (struct timeval *tv) 33static double tv_get (struct timeval *tv)
24{ 34{
28static void tv_set (struct timeval *tv, double val) 38static void tv_set (struct timeval *tv, double val)
29{ 39{
30 tv->tv_sec = (long)val; 40 tv->tv_sec = (long)val;
31 tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6); 41 tv->tv_usec = (long)((val - (double)tv->tv_sec) * 1e6);
32 42
43}
44
45static int
46sv_signum (SV *sig)
47{
48 int signum;
49
50 if (SvIV (sig) > 0)
51 return SvIV (sig);
52
53 for (signum = 1; signum < SIG_SIZE; ++signum)
54 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
55 return signum;
56
57 return -1;
33} 58}
34 59
35///////////////////////////////////////////////////////////////////////////// 60/////////////////////////////////////////////////////////////////////////////
36// Event 61// Event
37 62
107 132
108 if (ev->abstime) 133 if (ev->abstime)
109 { 134 {
110 double now = e_now (); 135 double now = e_now ();
111 136
112 if (now > to && ev->interval) 137 if (ev->interval)
113 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); 138 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval);
114 139
115 to -= now; 140 to -= now;
116 } 141 }
117 else if (to < 0.) 142 else if (to < 0.)
160 dSP; 185 dSP;
161 186
162 ENTER; 187 ENTER;
163 SAVETMPS; 188 SAVETMPS;
164 189
165 if (!(ev->ev.ev_events & EV_PERSIST)) 190 if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT))
166 ev->active = 0; 191 ev->active = 0;
167 192
168 PUSHMARK (SP); 193 PUSHMARK (SP);
169 EXTEND (SP, 2); 194 EXTEND (SP, 2);
170 PUSHs (sv_2mortal (e_self (ev))); 195 PUSHs (sv_2mortal (e_self (ev)));
171 PUSHs (sv_2mortal (newSViv (events))); 196 PUSHs (sv_2mortal (newSViv (events)));
172 PUTBACK; 197 PUTBACK;
173 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 198 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
174 /*TODO: if err, call some logging function */
175 199
176 if (ev->interval && !ev->active) 200 if (ev->interval && !ev->active)
177 e_start (ev); 201 e_start (ev);
178 202
179 FREETMPS; 203 FREETMPS;
204
205 if (SvTRUE (ERRSV))
206 {
207 PUSHMARK (SP);
208 PUTBACK;
209 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
210 }
211
180 LEAVE; 212 LEAVE;
181} 213}
182 214
183///////////////////////////////////////////////////////////////////////////// 215/////////////////////////////////////////////////////////////////////////////
184// DNS 216// DNS
219 251
220 PUTBACK; 252 PUTBACK;
221 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 253 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
222 254
223 FREETMPS; 255 FREETMPS;
256
257 if (SvTRUE (ERRSV))
258 {
259 PUSHMARK (SP);
260 PUTBACK;
261 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
262 }
263
224 LEAVE; 264 LEAVE;
225} 265}
226 266
227///////////////////////////////////////////////////////////////////////////// 267/////////////////////////////////////////////////////////////////////////////
228// XS interface functions 268// XS interface functions
229 269
230MODULE = EV PACKAGE = EV PREFIX = event_ 270MODULE = EV PACKAGE = EV PREFIX = event_
231 271
232BOOT: 272BOOT:
233{ 273{
274 int i;
234 HV *stash = gv_stashpv ("EV", 1); 275 HV *stash = gv_stashpv ("EV", 1);
235 276
236 static const struct { 277 static const struct {
237 const char *name; 278 const char *name;
238 IV iv; 279 IV iv;
303 RETVAL = e_new (fh, events, cb); 344 RETVAL = e_new (fh, events, cb);
304 if (!ix) e_start (RETVAL); 345 if (!ix) e_start (RETVAL);
305 OUTPUT: 346 OUTPUT:
306 RETVAL 347 RETVAL
307 348
349Event timed_io (SV *fh, short events, double timeout, SV *cb)
350 ALIAS:
351 timed_io_ns = 1
352 CODE:
353{
354 events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST;
355
356 RETVAL = e_new (fh, events, cb);
357
358 if (timeout)
359 {
360 RETVAL->timeout = timeout;
361 RETVAL->interval = 1;
362 }
363
364 if (!ix) e_start (RETVAL);
365}
366 OUTPUT:
367 RETVAL
368
308Event timer (double after, int repeat, SV *cb) 369Event timer (double after, int repeat, SV *cb)
309 ALIAS: 370 ALIAS:
310 timer_ns = 1 371 timer_ns = 1
311 CODE: 372 CODE:
312 RETVAL = e_new (NEWSV (0, 0), 0, cb); 373 RETVAL = e_new (NEWSV (0, 0), 0, cb);
326 RETVAL->abstime = 1; 387 RETVAL->abstime = 1;
327 if (!ix) e_start (RETVAL); 388 if (!ix) e_start (RETVAL);
328 OUTPUT: 389 OUTPUT:
329 RETVAL 390 RETVAL
330 391
331Event signal (SV *signal, SV *cb) 392Event signal (Signal signum, SV *cb)
332 ALIAS: 393 ALIAS:
333 signal_ns = 1 394 signal_ns = 1
334 CODE: 395 CODE:
335 RETVAL = e_new (signal, EV_SIGNAL | EV_PERSIST, cb); 396 RETVAL = e_new (ST (0), EV_SIGNAL | EV_PERSIST, cb);
397 RETVAL->ev.ev_fd = signum;
336 if (!ix) e_start (RETVAL); 398 if (!ix) e_start (RETVAL);
337 OUTPUT: 399 OUTPUT:
338 RETVAL 400 RETVAL
339 401
340PROTOTYPES: DISABLE 402PROTOTYPES: DISABLE
411 sv_setsv (ev->cb, new_cb); 473 sv_setsv (ev->cb, new_cb);
412 OUTPUT: 474 OUTPUT:
413 RETVAL 475 RETVAL
414 476
415SV *fh (Event ev, SV *new_fh = 0) 477SV *fh (Event ev, SV *new_fh = 0)
416 ALIAS:
417 signal = 0
418 CODE: 478 CODE:
419 RETVAL = newSVsv (ev->fh); 479 RETVAL = newSVsv (ev->fh);
420 if (items > 1) 480 if (items > 1)
421 { 481 {
422 if (ev->active) event_del (&ev->ev); 482 if (ev->active) event_del (&ev->ev);
423 sv_setsv (ev->fh, new_fh); 483 sv_setsv (ev->fh, new_fh);
424 ev->ev.ev_fd = sv_fileno (ev->fh); 484 ev->ev.ev_fd = sv_fileno (ev->fh);
485 ev->ev.ev_events &= ev->ev.ev_events & ~EV_SIGNAL;
425 if (ev->active) event_add (&ev->ev, e_tv (ev)); 486 if (ev->active) event_add (&ev->ev, e_tv (ev));
426 } 487 }
488 OUTPUT:
489 RETVAL
490
491SV *signal (Event ev, SV *new_signal = 0)
492 CODE:
493{
494 Signal signum;
495
496 if (items > 1)
497 signum = sv_signum (new_signal); /* may croak here */
498
499 RETVAL = newSVsv (ev->fh);
500
501 if (items > 1)
502 {
503 if (ev->active) event_del (&ev->ev);
504 sv_setsv (ev->fh, new_signal);
505 ev->ev.ev_fd = signum;
506 ev->ev.ev_events |= EV_SIGNAL;
507 if (ev->active) event_add (&ev->ev, e_tv (ev));
508 }
509}
427 OUTPUT: 510 OUTPUT:
428 RETVAL 511 RETVAL
429 512
430short events (Event ev, short new_events = EV_UNDEF) 513short events (Event ev, short new_events = EV_UNDEF)
431 CODE: 514 CODE:
471 static const struct { 554 static const struct {
472 const char *name; 555 const char *name;
473 IV iv; 556 IV iv;
474 } *civ, const_iv[] = { 557 } *civ, const_iv[] = {
475# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 558# define const_iv(pfx, name) { # name, (IV) pfx ## name },
476
477 const_iv (DNS_, ERR_NONE) 559 const_iv (DNS_, ERR_NONE)
478 const_iv (DNS_, ERR_FORMAT) 560 const_iv (DNS_, ERR_FORMAT)
479 const_iv (DNS_, ERR_SERVERFAILED) 561 const_iv (DNS_, ERR_SERVERFAILED)
480 const_iv (DNS_, ERR_NOTEXIST) 562 const_iv (DNS_, ERR_NOTEXIST)
481 const_iv (DNS_, ERR_NOTIMPL) 563 const_iv (DNS_, ERR_NOTIMPL)
527 CODE: 609 CODE:
528{ 610{
529 STRLEN len; 611 STRLEN len;
530 char *data = SvPVbyte (addr, len); 612 char *data = SvPVbyte (addr, len);
531 if (len != (ix ? 16 : 4)) 613 if (len != (ix ? 16 : 4))
532 croak ("ipv4/ipv6 address to resolve must be given as 4/16 byte octet string"); 614 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
533 615
534 RETVAL = ix 616 RETVAL = ix
535 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) 617 ? 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)); 618 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
537} 619}
552 634
553void evdns_search_add (char *domain) 635void evdns_search_add (char *domain)
554 636
555void evdns_search_ndots_set (int ndots) 637void evdns_search_ndots_set (int ndots)
556 638
639
640MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
641
642BOOT:
643{
644 HV *stash = gv_stashpv ("EV::HTTP", 1);
645
646 static const struct {
647 const char *name;
648 IV iv;
649 } *civ, const_iv[] = {
650# define const_iv(pfx, name) { # name, (IV) pfx ## name },
651 const_iv (HTTP_, OK)
652 const_iv (HTTP_, NOCONTENT)
653 const_iv (HTTP_, MOVEPERM)
654 const_iv (HTTP_, MOVETEMP)
655 const_iv (HTTP_, NOTMODIFIED)
656 const_iv (HTTP_, BADREQUEST)
657 const_iv (HTTP_, NOTFOUND)
658 const_iv (HTTP_, SERVUNAVAIL)
659 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
660 const_iv (EVHTTP_, PROXY_REQUEST)
661 const_iv (EVHTTP_, REQ_GET)
662 const_iv (EVHTTP_, REQ_POST)
663 const_iv (EVHTTP_, REQ_HEAD)
664 const_iv (EVHTTP_, REQUEST)
665 const_iv (EVHTTP_, RESPONSE)
666 };
667
668 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
669 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
670}
671
672MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_
673
674#HttpRequest new (SV *klass, SV *cb)
675
676#void DESTROY (struct evhttp_request *req);
677
678
679
680
681
682
683
684

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines