… | |
… | |
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 () |
20 | { |
22 | { |
|
|
23 | dSP; |
|
|
24 | |
21 | for (;;) |
25 | for (;;) |
22 | { |
26 | { |
23 | adns_query q; |
27 | adns_query q; |
24 | adns_answer *a; |
28 | adns_answer *a; |
25 | void *c; |
29 | void *ctx; |
|
|
30 | SV *cb; |
|
|
31 | struct ctx *c; |
26 | int r = adns_check (ads, &q, &a, &c); |
32 | int r = adns_check (ads, &q, &a, &ctx); |
27 | |
33 | |
28 | if (r) |
34 | if (r) |
29 | break; |
35 | break; |
|
|
36 | |
|
|
37 | c = (struct ctx *)ctx; |
|
|
38 | cb = c->cb; |
|
|
39 | c->cb = 0; |
|
|
40 | ev_unref (); |
|
|
41 | |
|
|
42 | PUSHMARK (SP); |
|
|
43 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
|
|
44 | |
|
|
45 | SvREFCNT_dec (cb); |
30 | } |
46 | } |
31 | } |
47 | } |
32 | |
48 | |
33 | static struct pollfd *fds; |
49 | static struct pollfd *fds; |
34 | static int nfd; |
50 | static int nfd; |
35 | static ev_io *iow; |
51 | static ev_io *iow; |
36 | static ev_timer tw; |
52 | static ev_timer tw; |
37 | static ev_prepare prepare_ev; |
53 | static ev_prepare prepare_ev; |
38 | static struct timeval *tv_now; |
54 | static struct timeval tv_now; |
39 | |
55 | |
40 | static void |
56 | static void |
41 | update_now (EV_P) |
57 | update_now (EV_P) |
42 | { |
58 | { |
43 | ev_tstamp t = ev_now (EV_P); |
59 | ev_tstamp t = ev_now (); |
44 | |
60 | |
45 | tv.tv_sec = (long)t; |
61 | tv_now.tv_sec = (long)t; |
46 | tv.tv_usec = (long)((t - (ev_tstamp)tv.tv_sec) * 1e-6); |
62 | tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e-6); |
47 | } |
63 | } |
48 | |
64 | |
49 | static void |
65 | static void |
50 | timer_cb (EV_P_ ev_timer *w, int revents) |
66 | timer_cb (EV_P_ ev_timer *w, int revents) |
51 | { |
67 | { |
… | |
… | |
100 | |
116 | |
101 | PROTOTYPES: ENABLE |
117 | PROTOTYPES: ENABLE |
102 | |
118 | |
103 | BOOT: |
119 | BOOT: |
104 | { |
120 | { |
105 | #if 0 |
|
|
106 | HV *stash = gv_stashpv ("EV::ADNS", 1); |
121 | stash = gv_stashpv ("EV::ADNS", 1); |
107 | |
122 | |
108 | static const struct { |
123 | static const struct { |
109 | const char *name; |
124 | const char *name; |
110 | IV iv; |
125 | IV iv; |
111 | } *civ, const_iv[] = { |
126 | } *civ, const_iv[] = { |
112 | # define const_iv(pfx, name) { # name, (IV) adns_ ## name }, |
127 | # define const_iv(name) { # name, (IV) adns_ ## name }, |
113 | const_iv (if_none) |
128 | const_iv (if_none) |
114 | const_iv (if_noenv) |
129 | const_iv (if_noenv) |
115 | const_iv (if_noerrprint) |
130 | const_iv (if_noerrprint) |
116 | const_iv (if_noserverwarn) |
131 | const_iv (if_noserverwarn) |
117 | const_iv (if_debug) |
132 | const_iv (if_debug) |
… | |
… | |
188 | const_iv (s_max_permfail) |
203 | const_iv (s_max_permfail) |
189 | }; |
204 | }; |
190 | |
205 | |
191 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
206 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
192 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
207 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
193 | #endif |
|
|
194 | |
208 | |
195 | I_EV_API ("EV::ADNS"); |
209 | I_EV_API ("EV::ADNS"); |
196 | |
210 | |
197 | ev_prepare_init (&prepare_ev, prepare_cb); ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
211 | ev_prepare_init (&prepare_ev, prepare_cb); ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
198 | ev_unref (); |
212 | ev_unref (); |
… | |
… | |
200 | ev_init (&tw, timer_cb); |
214 | ev_init (&tw, timer_cb); |
201 | |
215 | |
202 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
216 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
203 | } |
217 | } |
204 | |
218 | |
205 | int adns_submit (char *owner, int type, int flags, SV *cb) |
219 | void submit (char *owner, int type, int flags, SV *cb) |
206 | CODE: |
220 | PPCODE: |
207 | { |
221 | { |
|
|
222 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
208 | struct ctx *c = (struct ctx *)malloc (sizeof (ctx)); |
223 | struct ctx *c = (struct ctx *)SvPVX (csv); |
209 | adns_query q; |
|
|
210 | int r = adns_submit (owner, type, flags, (void *)c, &q); |
224 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
211 | |
225 | |
212 | if (r) |
226 | if (r) |
213 | { |
227 | { |
214 | free (c); |
228 | printf ("errn %d\n", r);//D |
|
|
229 | SvREFCNT_dec (csv); |
215 | XSRETURN_EMPTY; |
230 | XSRETURN_EMPTY; |
216 | } |
231 | } |
217 | } |
232 | else |
|
|
233 | { |
|
|
234 | ev_ref (); |
|
|
235 | SvPOK_only (csv); |
|
|
236 | SvCUR_set (csv, sizeof (struct ctx)); |
|
|
237 | c->self = csv; |
|
|
238 | c->cb = newSVsv (cb); |
218 | |
239 | |
|
|
240 | if (GIMME_V != G_VOID) |
|
|
241 | { |
|
|
242 | csv = sv_2mortal (newRV_inc (csv)); |
|
|
243 | sv_bless (csv, stash); |
|
|
244 | XPUSHs (csv); |
|
|
245 | } |
|
|
246 | } |
|
|
247 | } |
219 | |
248 | |
|
|
249 | void DESTROY (SV *req) |
|
|
250 | CODE: |
|
|
251 | { |
|
|
252 | struct ctx *c; |
220 | |
253 | |
|
|
254 | if (!(SvROK (req) && SvOBJECT (SvRV (req)) |
|
|
255 | && (SvSTASH (SvRV (req)) == stash))) |
|
|
256 | croak ("object is not of type EV::ADNS"); |
|
|
257 | |
|
|
258 | c = (struct ctx *)SvPVX (SvRV (req)); |
|
|
259 | |
|
|
260 | if (c->cb) |
|
|
261 | { |
|
|
262 | ev_unref (); |
|
|
263 | adns_cancel (c->query); |
|
|
264 | SvREFCNT_dec (c->cb); |
|
|
265 | } |
|
|
266 | |
|
|
267 | SvREFCNT_dec (c->self); |
|
|
268 | } |
|
|
269 | |
|
|
270 | |
|
|
271 | |
|
|
272 | |