… | |
… | |
18 | }; |
18 | }; |
19 | |
19 | |
20 | static void |
20 | static void |
21 | process () |
21 | process () |
22 | { |
22 | { |
|
|
23 | dSP; |
|
|
24 | |
23 | for (;;) |
25 | for (;;) |
24 | { |
26 | { |
25 | adns_query q; |
27 | adns_query q = 0; |
26 | adns_answer *a; |
28 | adns_answer *a; |
27 | void *c; |
29 | void *ctx; |
|
|
30 | SV *cb; |
|
|
31 | struct ctx *c; |
28 | int r = adns_check (ads, &q, &a, &c); |
32 | int r = adns_check (ads, &q, &a, &ctx); |
29 | |
33 | |
|
|
34 | printf ("check %d\n", r);//D |
30 | if (r) |
35 | if (r) |
31 | break; |
36 | break; |
|
|
37 | |
|
|
38 | c = (struct ctx *)ctx; |
|
|
39 | cb = c->cb; |
|
|
40 | c->cb = 0; |
|
|
41 | ev_unref (); |
|
|
42 | |
|
|
43 | PUSHMARK (SP); |
|
|
44 | |
|
|
45 | XPUSHs (sv_2mortal (newSViv (a->status))); |
|
|
46 | free (a); |
|
|
47 | |
|
|
48 | PUTBACK; |
|
|
49 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
|
|
50 | SPAGAIN; |
|
|
51 | |
|
|
52 | SvREFCNT_dec (cb); |
|
|
53 | SvREFCNT_dec (c->self); |
32 | } |
54 | } |
33 | } |
55 | } |
34 | |
56 | |
35 | static struct pollfd *fds; |
57 | static struct pollfd *fds; |
36 | static int nfd; |
58 | static int nfd, mfd; |
37 | static ev_io *iow; |
59 | static ev_io *iow; |
38 | static ev_timer tw; |
60 | static ev_timer tw; |
39 | static ev_prepare prepare_ev; |
61 | static ev_prepare prepare_ev; |
40 | static struct timeval tv_now; |
62 | static struct timeval tv_now; |
41 | |
63 | |
… | |
… | |
76 | |
98 | |
77 | process (); |
99 | process (); |
78 | |
100 | |
79 | update_now (EV_A); |
101 | update_now (EV_A); |
80 | |
102 | |
|
|
103 | nfd = mfd; |
|
|
104 | |
81 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
105 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
82 | { |
106 | { |
|
|
107 | mfd = nfd; |
|
|
108 | |
83 | free (iow); iow = malloc (nfd * sizeof (struct pollfd)); |
109 | free (iow); iow = malloc (mfd * sizeof (struct pollfd)); |
84 | free (fds); fds = malloc (nfd * sizeof (ev_io)); |
110 | free (fds); fds = malloc (mfd * sizeof (ev_io)); |
85 | } |
111 | } |
|
|
112 | |
|
|
113 | printf ("to %d\n", timeout);//d |
86 | |
114 | |
87 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
115 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
88 | ev_timer_start (EV_A_ &tw); |
116 | ev_timer_start (EV_A_ &tw); |
89 | |
117 | |
90 | // create on ev_io per pollfd |
118 | // create one ev_io per pollfd |
91 | for (i = 0; i < nfd; ++i) |
119 | for (i = 0; i < nfd; ++i) |
92 | { |
120 | { |
93 | ev_io_init (iow + i, io_cb, fds [i].fd, |
121 | ev_io_init (iow + i, io_cb, fds [i].fd, |
94 | ((fds [i].events & POLLIN ? EV_READ : 0) |
122 | ((fds [i].events & POLLIN ? EV_READ : 0) |
95 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
123 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
… | |
… | |
103 | PROTOTYPES: ENABLE |
131 | PROTOTYPES: ENABLE |
104 | |
132 | |
105 | BOOT: |
133 | BOOT: |
106 | { |
134 | { |
107 | stash = gv_stashpv ("EV::ADNS", 1); |
135 | stash = gv_stashpv ("EV::ADNS", 1); |
108 | #if 0 |
|
|
109 | |
136 | |
110 | static const struct { |
137 | static const struct { |
111 | const char *name; |
138 | const char *name; |
112 | IV iv; |
139 | IV iv; |
113 | } *civ, const_iv[] = { |
140 | } *civ, const_iv[] = { |
114 | # define const_iv(pfx, name) { # name, (IV) adns_ ## name }, |
141 | # define const_iv(name) { # name, (IV) adns_ ## name }, |
115 | const_iv (if_none) |
142 | const_iv (if_none) |
116 | const_iv (if_noenv) |
143 | const_iv (if_noenv) |
117 | const_iv (if_noerrprint) |
144 | const_iv (if_noerrprint) |
118 | const_iv (if_noserverwarn) |
145 | const_iv (if_noserverwarn) |
119 | const_iv (if_debug) |
146 | const_iv (if_debug) |
… | |
… | |
190 | const_iv (s_max_permfail) |
217 | const_iv (s_max_permfail) |
191 | }; |
218 | }; |
192 | |
219 | |
193 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
220 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
194 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
221 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
195 | #endif |
|
|
196 | |
222 | |
197 | I_EV_API ("EV::ADNS"); |
223 | I_EV_API ("EV::ADNS"); |
198 | |
224 | |
199 | ev_prepare_init (&prepare_ev, prepare_cb); ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
225 | ev_prepare_init (&prepare_ev, prepare_cb); ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
200 | ev_unref (); |
226 | ev_unref (); |
… | |
… | |
202 | ev_init (&tw, timer_cb); |
228 | ev_init (&tw, timer_cb); |
203 | |
229 | |
204 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
230 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
205 | } |
231 | } |
206 | |
232 | |
207 | void adns_submit (char *owner, int type, int flags, SV *cb) |
233 | void submit (char *owner, int type, int flags, SV *cb) |
208 | PPCODE: |
234 | PPCODE: |
209 | { |
235 | { |
210 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
236 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
211 | struct ctx *c = (struct ctx *)SvPVX (csv); |
237 | struct ctx *c = (struct ctx *)SvPVX (csv); |
212 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
238 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
… | |
… | |
216 | SvREFCNT_dec (csv); |
242 | SvREFCNT_dec (csv); |
217 | XSRETURN_EMPTY; |
243 | XSRETURN_EMPTY; |
218 | } |
244 | } |
219 | else |
245 | else |
220 | { |
246 | { |
|
|
247 | ev_ref (); |
221 | SvPOK_only (csv); |
248 | SvPOK_only (csv); |
222 | SvCUR_set (csv, sizeof (struct ctx)); |
249 | SvCUR_set (csv, sizeof (struct ctx)); |
223 | c->self = csv; |
250 | c->self = csv; |
224 | c->cb = newSVsv (cb); |
251 | c->cb = newSVsv (cb); |
225 | |
252 | |
… | |
… | |
243 | |
270 | |
244 | c = (struct ctx *)SvPVX (SvRV (req)); |
271 | c = (struct ctx *)SvPVX (SvRV (req)); |
245 | |
272 | |
246 | if (c->cb) |
273 | if (c->cb) |
247 | { |
274 | { |
|
|
275 | ev_unref (); |
248 | adns_cancel (c->query); |
276 | adns_cancel (c->query); |
249 | SvREFCNT_dec (c->cb); |
277 | SvREFCNT_dec (c->cb); |
250 | } |
278 | } |
251 | |
|
|
252 | SvREFCNT_dec (c->self); |
|
|
253 | } |
279 | } |
254 | |
280 | |
255 | |
281 | |
256 | |
282 | |
257 | |
283 | |