--- EV-ADNS/ADNS.xs 2007/12/01 13:53:11 1.1 +++ EV-ADNS/ADNS.xs 2007/12/01 14:14:00 1.2 @@ -7,11 +7,13 @@ #include "EVAPI.h" +static HV *stash; static adns_state ads; struct ctx { - SV *req; + SV *self; + adns_query query; SV *cb; }; @@ -35,15 +37,15 @@ static ev_io *iow; static ev_timer tw; static ev_prepare prepare_ev; -static struct timeval *tv_now; +static struct timeval tv_now; static void update_now (EV_P) { - ev_tstamp t = ev_now (EV_P); + ev_tstamp t = ev_now (); - tv.tv_sec = (long)t; - tv.tv_usec = (long)((t - (ev_tstamp)tv.tv_sec) * 1e-6); + tv_now.tv_sec = (long)t; + tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e-6); } static void @@ -102,8 +104,8 @@ BOOT: { + stash = gv_stashpv ("EV::ADNS", 1); #if 0 - HV *stash = gv_stashpv ("EV::ADNS", 1); static const struct { const char *name; @@ -202,19 +204,54 @@ adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); } -int adns_submit (char *owner, int type, int flags, SV *cb) - CODE: +void adns_submit (char *owner, int type, int flags, SV *cb) + PPCODE: { - struct ctx *c = (struct ctx *)malloc (sizeof (ctx)); - adns_query q; - int r = adns_submit (owner, type, flags, (void *)c, &q); + SV *csv = NEWSV (0, sizeof (struct ctx)); + struct ctx *c = (struct ctx *)SvPVX (csv); + int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); if (r) { - free (c); + SvREFCNT_dec (csv); XSRETURN_EMPTY; } + else + { + SvPOK_only (csv); + SvCUR_set (csv, sizeof (struct ctx)); + c->self = csv; + c->cb = newSVsv (cb); + + if (GIMME_V != G_VOID) + { + csv = sv_2mortal (newRV_inc (csv)); + sv_bless (csv, stash); + XPUSHs (csv); + } + } } +void DESTROY (SV *req) + CODE: +{ + struct ctx *c; + + if (!(SvROK (req) && SvOBJECT (SvRV (req)) + && (SvSTASH (SvRV (req)) == stash))) + croak ("object is not of type EV::ADNS"); + + c = (struct ctx *)SvPVX (SvRV (req)); + + if (c->cb) + { + adns_cancel (c->query); + SvREFCNT_dec (c->cb); + } + + SvREFCNT_dec (c->self); +} + +