… | |
… | |
5 | #include <poll.h> |
5 | #include <poll.h> |
6 | #include <adns.h> |
6 | #include <adns.h> |
7 | |
7 | |
8 | #include "EVAPI.h" |
8 | #include "EVAPI.h" |
9 | |
9 | |
|
|
10 | static HV *stash; |
10 | static adns_state ads; |
11 | static adns_state ads; |
11 | |
12 | |
12 | struct ctx |
13 | struct ctx |
13 | { |
14 | { |
14 | SV *req; |
15 | SV *self; |
|
|
16 | adns_query query; |
15 | SV *cb; |
17 | SV *cb; |
16 | }; |
18 | }; |
17 | |
19 | |
18 | static void |
20 | static void |
19 | process () |
21 | process () |
… | |
… | |
33 | static struct pollfd *fds; |
35 | static struct pollfd *fds; |
34 | static int nfd; |
36 | static int nfd; |
35 | static ev_io *iow; |
37 | static ev_io *iow; |
36 | static ev_timer tw; |
38 | static ev_timer tw; |
37 | static ev_prepare prepare_ev; |
39 | static ev_prepare prepare_ev; |
38 | static struct timeval *tv_now; |
40 | static struct timeval tv_now; |
39 | |
41 | |
40 | static void |
42 | static void |
41 | update_now (EV_P) |
43 | update_now (EV_P) |
42 | { |
44 | { |
43 | ev_tstamp t = ev_now (EV_P); |
45 | ev_tstamp t = ev_now (); |
44 | |
46 | |
45 | tv.tv_sec = (long)t; |
47 | tv_now.tv_sec = (long)t; |
46 | tv.tv_usec = (long)((t - (ev_tstamp)tv.tv_sec) * 1e-6); |
48 | tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e-6); |
47 | } |
49 | } |
48 | |
50 | |
49 | static void |
51 | static void |
50 | timer_cb (EV_P_ ev_timer *w, int revents) |
52 | timer_cb (EV_P_ ev_timer *w, int revents) |
51 | { |
53 | { |
… | |
… | |
100 | |
102 | |
101 | PROTOTYPES: ENABLE |
103 | PROTOTYPES: ENABLE |
102 | |
104 | |
103 | BOOT: |
105 | BOOT: |
104 | { |
106 | { |
|
|
107 | stash = gv_stashpv ("EV::ADNS", 1); |
105 | #if 0 |
108 | #if 0 |
106 | HV *stash = gv_stashpv ("EV::ADNS", 1); |
|
|
107 | |
109 | |
108 | static const struct { |
110 | static const struct { |
109 | const char *name; |
111 | const char *name; |
110 | IV iv; |
112 | IV iv; |
111 | } *civ, const_iv[] = { |
113 | } *civ, const_iv[] = { |
… | |
… | |
200 | ev_init (&tw, timer_cb); |
202 | ev_init (&tw, timer_cb); |
201 | |
203 | |
202 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
204 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
203 | } |
205 | } |
204 | |
206 | |
205 | int adns_submit (char *owner, int type, int flags, SV *cb) |
207 | void adns_submit (char *owner, int type, int flags, SV *cb) |
206 | CODE: |
208 | PPCODE: |
207 | { |
209 | { |
|
|
210 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
208 | struct ctx *c = (struct ctx *)malloc (sizeof (ctx)); |
211 | struct ctx *c = (struct ctx *)SvPVX (csv); |
209 | adns_query q; |
|
|
210 | int r = adns_submit (owner, type, flags, (void *)c, &q); |
212 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
211 | |
213 | |
212 | if (r) |
214 | if (r) |
213 | { |
215 | { |
214 | free (c); |
216 | SvREFCNT_dec (csv); |
215 | XSRETURN_EMPTY; |
217 | XSRETURN_EMPTY; |
216 | } |
218 | } |
217 | } |
219 | else |
|
|
220 | { |
|
|
221 | SvPOK_only (csv); |
|
|
222 | SvCUR_set (csv, sizeof (struct ctx)); |
|
|
223 | c->self = csv; |
|
|
224 | c->cb = newSVsv (cb); |
218 | |
225 | |
|
|
226 | if (GIMME_V != G_VOID) |
|
|
227 | { |
|
|
228 | csv = sv_2mortal (newRV_inc (csv)); |
|
|
229 | sv_bless (csv, stash); |
|
|
230 | XPUSHs (csv); |
|
|
231 | } |
|
|
232 | } |
|
|
233 | } |
219 | |
234 | |
|
|
235 | void DESTROY (SV *req) |
|
|
236 | CODE: |
|
|
237 | { |
|
|
238 | struct ctx *c; |
220 | |
239 | |
|
|
240 | if (!(SvROK (req) && SvOBJECT (SvRV (req)) |
|
|
241 | && (SvSTASH (SvRV (req)) == stash))) |
|
|
242 | croak ("object is not of type EV::ADNS"); |
|
|
243 | |
|
|
244 | c = (struct ctx *)SvPVX (SvRV (req)); |
|
|
245 | |
|
|
246 | if (c->cb) |
|
|
247 | { |
|
|
248 | adns_cancel (c->query); |
|
|
249 | SvREFCNT_dec (c->cb); |
|
|
250 | } |
|
|
251 | |
|
|
252 | SvREFCNT_dec (c->self); |
|
|
253 | } |
|
|
254 | |
|
|
255 | |
|
|
256 | |
|
|
257 | |