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

Comparing EV/EV.xs (file contents):
Revision 1.2 by root, Fri Oct 26 16:51:27 2007 UTC vs.
Revision 1.9 by root, Mon Oct 29 07:24:37 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
56 65
57static void e_cb (int fd, short events, void *arg); 66static void e_cb (int fd, short events, void *arg);
58 67
59static int sv_fileno (SV *fh) 68static int sv_fileno (SV *fh)
60{ 69{
61 if (fh)
62 {
63 SvGETMAGIC (fh); 70 SvGETMAGIC (fh);
64 71
65 if (SvROK (fh)) 72 if (SvROK (fh))
66 fh = SvRV (fh); 73 fh = SvRV (fh);
67 74
68 if (SvTYPE (fh) == SVt_PVGV) 75 if (SvTYPE (fh) == SVt_PVGV)
69 return PerlIO_fileno (IoIFP (sv_2io (fh))); 76 return PerlIO_fileno (IoIFP (sv_2io (fh)));
70 77
71 if (SvIOK (fh)) 78 if (SvIOK (fh))
72 return SvIV (fh); 79 return SvIV (fh);
73 }
74 80
75 return -1; 81 return -1;
76} 82}
77 83
78static Event 84static Event
110 116
111 if (ev->abstime) 117 if (ev->abstime)
112 { 118 {
113 double now = e_now (); 119 double now = e_now ();
114 120
115 if (now > to && ev->interval) 121 if (ev->interval)
116 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval); 122 ev->timeout = (to += ceil ((now - to) / ev->interval) * ev->interval);
117 123
118 to -= now; 124 to -= now;
119 } 125 }
120 else if (to < 0.) 126 else if (to < 0.)
163 dSP; 169 dSP;
164 170
165 ENTER; 171 ENTER;
166 SAVETMPS; 172 SAVETMPS;
167 173
168 if (!(ev->ev.ev_events & EV_PERSIST)) 174 if (!(ev->ev.ev_events & EV_PERSIST) || (events & EV_TIMEOUT))
169 ev->active = 0; 175 ev->active = 0;
170 176
171 PUSHMARK (SP); 177 PUSHMARK (SP);
172 EXTEND (SP, 2); 178 EXTEND (SP, 2);
173 PUSHs (sv_2mortal (e_self (ev))); 179 PUSHs (sv_2mortal (e_self (ev)));
174 PUSHs (sv_2mortal (newSViv (events))); 180 PUSHs (sv_2mortal (newSViv (events)));
175 PUTBACK; 181 PUTBACK;
176 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL); 182 call_sv (ev->cb, G_DISCARD | G_VOID | G_EVAL);
177 /*TODO: if err, call some logging function */
178 183
179 if (ev->interval && !ev->active) 184 if (ev->interval && !ev->active)
180 e_start (ev); 185 e_start (ev);
181 186
182 FREETMPS; 187 FREETMPS;
188
189 if (SvTRUE (ERRSV))
190 {
191 PUSHMARK (SP);
192 PUTBACK;
193 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
194 }
195
183 LEAVE; 196 LEAVE;
184} 197}
185 198
186///////////////////////////////////////////////////////////////////////////// 199/////////////////////////////////////////////////////////////////////////////
187// DNS 200// DNS
222 235
223 PUTBACK; 236 PUTBACK;
224 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL); 237 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
225 238
226 FREETMPS; 239 FREETMPS;
240
241 if (SvTRUE (ERRSV))
242 {
243 PUSHMARK (SP);
244 PUTBACK;
245 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
246 }
247
227 LEAVE; 248 LEAVE;
228} 249}
229 250
230///////////////////////////////////////////////////////////////////////////// 251/////////////////////////////////////////////////////////////////////////////
231// XS interface functions 252// XS interface functions
283 304
284int event_dispatch () 305int event_dispatch ()
285 306
286int event_loop (int flags = 0) 307int event_loop (int flags = 0)
287 308
288int event_loopexit (double after) 309int event_loopexit (double after = 0)
289 CODE: 310 CODE:
290{ 311{
291 struct timeval tv; 312 struct timeval tv;
292 tv_set (&tv, after); 313 tv_set (&tv, after);
293 event_loopexit (&tv); 314 event_loopexit (&tv);
294} 315}
295 316
296Event event (SV *cb) 317Event event (SV *cb)
297 CODE: 318 CODE:
298 RETVAL = e_new (0, 0, cb); 319 RETVAL = e_new (NEWSV (0, 0), 0, cb);
299 OUTPUT: 320 OUTPUT:
300 RETVAL 321 RETVAL
301 322
302Event io (SV *fh, short events, SV *cb) 323Event io (SV *fh, short events, SV *cb)
303 ALIAS: 324 ALIAS:
306 RETVAL = e_new (fh, events, cb); 327 RETVAL = e_new (fh, events, cb);
307 if (!ix) e_start (RETVAL); 328 if (!ix) e_start (RETVAL);
308 OUTPUT: 329 OUTPUT:
309 RETVAL 330 RETVAL
310 331
332Event timed_io (SV *fh, short events, double timeout, SV *cb)
333 ALIAS:
334 timed_io_ns = 1
335 CODE:
336{
337 events = timeout ? events & ~EV_PERSIST : events | EV_PERSIST;
338
339 RETVAL = e_new (fh, events, cb);
340
341 if (timeout)
342 {
343 RETVAL->timeout = timeout;
344 RETVAL->interval = 1;
345 }
346
347 if (!ix) e_start (RETVAL);
348}
349 OUTPUT:
350 RETVAL
351
311Event timer (double after, int repeat, SV *cb) 352Event timer (double after, int repeat, SV *cb)
312 ALIAS: 353 ALIAS:
313 timer_ns = 1 354 timer_ns = 1
314 CODE: 355 CODE:
315 RETVAL = e_new (0, 0, cb); 356 RETVAL = e_new (NEWSV (0, 0), 0, cb);
316 RETVAL->timeout = after; 357 RETVAL->timeout = after;
317 RETVAL->interval = repeat; 358 RETVAL->interval = repeat;
318 if (!ix) e_start (RETVAL); 359 if (!ix) e_start (RETVAL);
319 OUTPUT: 360 OUTPUT:
320 RETVAL 361 RETVAL
321 362
322Event timer_abs (double at, double interval, SV *cb) 363Event timer_abs (double at, double interval, SV *cb)
323 ALIAS: 364 ALIAS:
324 timer_abs_ns = 1 365 timer_abs_ns = 1
325 CODE: 366 CODE:
326 RETVAL = e_new (0, 0, cb); 367 RETVAL = e_new (NEWSV (0, 0), 0, cb);
327 RETVAL->timeout = at; 368 RETVAL->timeout = at;
328 RETVAL->interval = interval; 369 RETVAL->interval = interval;
329 RETVAL->abstime = 1; 370 RETVAL->abstime = 1;
330 if (!ix) e_start (RETVAL); 371 if (!ix) e_start (RETVAL);
331 OUTPUT: 372 OUTPUT:
405 CODE: 446 CODE:
406 e_stop (ev); 447 e_stop (ev);
407 SvREFCNT_dec (ev->cb); 448 SvREFCNT_dec (ev->cb);
408 SvREFCNT_dec (ev->fh); 449 SvREFCNT_dec (ev->fh);
409 450
410void cb (Event ev, SV *new_cb) 451SV *cb (Event ev, SV *new_cb = 0)
411 CODE: 452 CODE:
453 RETVAL = newSVsv (ev->cb);
454 if (items > 1)
412 sv_setsv (ev->cb, new_cb); 455 sv_setsv (ev->cb, new_cb);
456 OUTPUT:
457 RETVAL
413 458
414SV *fh (Event ev, SV *new_fh = 0) 459SV *fh (Event ev, SV *new_fh = 0)
415 ALIAS: 460 ALIAS:
416 signal = 0 461 signal = 1
417 CODE: 462 CODE:
418 RETVAL = newSVsv (ev->fh); 463 RETVAL = newSVsv (ev->fh);
419 if (items > 1) 464 if (items > 1)
420 { 465 {
421 if (ev->active) event_del (&ev->ev); 466 if (ev->active) event_del (&ev->ev);
422 sv_setsv (ev->fh, new_fh); 467 sv_setsv (ev->fh, new_fh);
423 ev->ev.ev_fd = sv_fileno (ev->fh); 468 ev->ev.ev_fd = sv_fileno (ev->fh);
469 ev->ev.ev_events = signal ? ev->ev.ev_events | EV_SIGNAL : ev->ev.ev_events & ~EV_SIGNAL;
424 if (ev->active) event_add (&ev->ev, e_tv (ev)); 470 if (ev->active) event_add (&ev->ev, e_tv (ev));
425 } 471 }
426 OUTPUT: 472 OUTPUT:
427 RETVAL 473 RETVAL
428 474
470 static const struct { 516 static const struct {
471 const char *name; 517 const char *name;
472 IV iv; 518 IV iv;
473 } *civ, const_iv[] = { 519 } *civ, const_iv[] = {
474# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 520# define const_iv(pfx, name) { # name, (IV) pfx ## name },
475
476 const_iv (DNS_, ERR_NONE) 521 const_iv (DNS_, ERR_NONE)
477 const_iv (DNS_, ERR_FORMAT) 522 const_iv (DNS_, ERR_FORMAT)
478 const_iv (DNS_, ERR_SERVERFAILED) 523 const_iv (DNS_, ERR_SERVERFAILED)
479 const_iv (DNS_, ERR_NOTEXIST) 524 const_iv (DNS_, ERR_NOTEXIST)
480 const_iv (DNS_, ERR_NOTIMPL) 525 const_iv (DNS_, ERR_NOTIMPL)
526 CODE: 571 CODE:
527{ 572{
528 STRLEN len; 573 STRLEN len;
529 char *data = SvPVbyte (addr, len); 574 char *data = SvPVbyte (addr, len);
530 if (len != (ix ? 16 : 4)) 575 if (len != (ix ? 16 : 4))
531 croak ("ipv4/ipv6 address to resolve must be given as 4/16 byte octet string"); 576 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
532 577
533 RETVAL = ix 578 RETVAL = ix
534 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb)) 579 ? 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)); 580 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
536} 581}
551 596
552void evdns_search_add (char *domain) 597void evdns_search_add (char *domain)
553 598
554void evdns_search_ndots_set (int ndots) 599void evdns_search_ndots_set (int ndots)
555 600
601
602MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
603
604BOOT:
605{
606 HV *stash = gv_stashpv ("EV::HTTP", 1);
607
608 static const struct {
609 const char *name;
610 IV iv;
611 } *civ, const_iv[] = {
612# define const_iv(pfx, name) { # name, (IV) pfx ## name },
613 const_iv (HTTP_, OK)
614 const_iv (HTTP_, NOCONTENT)
615 const_iv (HTTP_, MOVEPERM)
616 const_iv (HTTP_, MOVETEMP)
617 const_iv (HTTP_, NOTMODIFIED)
618 const_iv (HTTP_, BADREQUEST)
619 const_iv (HTTP_, NOTFOUND)
620 const_iv (HTTP_, SERVUNAVAIL)
621 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
622 const_iv (EVHTTP_, PROXY_REQUEST)
623 const_iv (EVHTTP_, REQ_GET)
624 const_iv (EVHTTP_, REQ_POST)
625 const_iv (EVHTTP_, REQ_HEAD)
626 const_iv (EVHTTP_, REQUEST)
627 const_iv (EVHTTP_, RESPONSE)
628 };
629
630 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
631 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
632}
633
634MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_
635
636#HttpRequest new (SV *klass, SV *cb)
637
638#void DESTROY (struct evhttp_request *req);
639
640
641
642
643
644
645
646

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines