… | |
… | |
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 struct pollfd *fds; |
|
|
11 | static int nfd, mfd; |
|
|
12 | static ev_io *iow; |
|
|
13 | static ev_timer tw; |
|
|
14 | static ev_idle iw; |
|
|
15 | static ev_prepare pw; |
|
|
16 | static struct timeval tv_now; |
|
|
17 | static int outstanding; |
|
|
18 | |
|
|
19 | static void |
|
|
20 | outstanding_inc (adns_state ads) |
|
|
21 | { |
|
|
22 | if (!outstanding++) |
|
|
23 | ev_prepare_start (EV_DEFAULT, &pw); |
|
|
24 | } |
|
|
25 | |
|
|
26 | static void |
|
|
27 | outstanding_dec (adns_state ads) |
|
|
28 | { |
|
|
29 | --outstanding; |
|
|
30 | } |
|
|
31 | |
10 | struct ctx |
32 | struct ctx |
11 | { |
33 | { |
12 | SV *self; |
34 | SV *self; |
|
|
35 | adns_state ads; |
13 | adns_query query; |
36 | adns_query query; |
14 | SV *cb; |
37 | SV *cb; |
15 | }; |
38 | }; |
16 | |
39 | |
17 | static SV * |
40 | static SV * |
… | |
… | |
42 | if (r) |
65 | if (r) |
43 | break; |
66 | break; |
44 | |
67 | |
45 | c = (struct ctx *)ctx; |
68 | c = (struct ctx *)ctx; |
46 | cb = c->cb; |
69 | cb = c->cb; |
47 | c->cb = 0; |
70 | c->cb = 0; outstanding_dec (ads); |
48 | ev_unref (EV_A); |
|
|
49 | SvREFCNT_dec (c->self); |
71 | SvREFCNT_dec (c->self); |
|
|
72 | |
|
|
73 | assert (cb); |
50 | |
74 | |
51 | PUSHMARK (SP); |
75 | PUSHMARK (SP); |
52 | |
76 | |
53 | EXTEND (SP, a->nrrs + 2); |
77 | EXTEND (SP, a->nrrs + 2); |
54 | PUSHs (sv_2mortal (newSViv (a->status))); |
78 | PUSHs (sv_2mortal (newSViv (a->status))); |
… | |
… | |
204 | |
228 | |
205 | PUTBACK; |
229 | PUTBACK; |
206 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
230 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
207 | SPAGAIN; |
231 | SPAGAIN; |
208 | |
232 | |
|
|
233 | if (SvTRUE (ERRSV)) |
|
|
234 | warn ("%s", SvPV_nolen (ERRSV)); |
|
|
235 | |
209 | SvREFCNT_dec (cb); |
236 | SvREFCNT_dec (cb); |
210 | } |
237 | } |
211 | } |
238 | } |
212 | |
239 | |
213 | static struct pollfd *fds; |
|
|
214 | static int nfd, mfd; |
|
|
215 | static ev_io *iow; |
|
|
216 | static ev_timer tw; |
|
|
217 | static ev_idle iw; |
|
|
218 | static ev_prepare pw; |
|
|
219 | static struct timeval tv_now; |
|
|
220 | |
|
|
221 | static void |
240 | static void |
222 | update_now (EV_P) |
241 | update_now (EV_P) |
223 | { |
242 | { |
224 | ev_tstamp t = ev_now (EV_A); |
243 | ev_tstamp t = ev_now (EV_A); |
225 | |
244 | |
… | |
… | |
228 | } |
247 | } |
229 | |
248 | |
230 | static void |
249 | static void |
231 | idle_cb (EV_P_ ev_idle *w, int revents) |
250 | idle_cb (EV_P_ ev_idle *w, int revents) |
232 | { |
251 | { |
233 | ev_idle_stop (EV_A_ w); |
252 | ev_idle_stop (EV_A, w); |
234 | } |
253 | } |
235 | |
254 | |
236 | static void |
255 | static void |
237 | timer_cb (EV_P_ ev_timer *w, int revents) |
256 | timer_cb (EV_P_ ev_timer *w, int revents) |
238 | { |
257 | { |
… | |
… | |
259 | int i; |
278 | int i; |
260 | int timeout = 3600000; |
279 | int timeout = 3600000; |
261 | adns_state ads = (adns_state)w->data; |
280 | adns_state ads = (adns_state)w->data; |
262 | |
281 | |
263 | if (ev_is_active (&tw)) |
282 | if (ev_is_active (&tw)) |
|
|
283 | ev_timer_stop (EV_A, &tw); |
|
|
284 | |
|
|
285 | if (ev_is_active (&iw)) |
|
|
286 | ev_idle_stop (EV_A, &iw); |
|
|
287 | |
|
|
288 | for (i = 0; i < nfd; ++i) |
|
|
289 | ev_io_stop (EV_A, iow + i); |
|
|
290 | |
|
|
291 | process (ads); |
|
|
292 | |
|
|
293 | if (!outstanding) |
264 | { |
294 | { |
265 | ev_ref (EV_A); |
|
|
266 | ev_timer_stop (EV_A_ &tw); |
295 | ev_prepare_stop (EV_A, w); |
|
|
296 | return; |
267 | } |
297 | } |
268 | |
298 | |
269 | if (ev_is_active (&iw)) |
|
|
270 | ev_idle_stop (EV_A_ &iw); |
|
|
271 | |
|
|
272 | for (i = 0; i < nfd; ++i) |
|
|
273 | { |
|
|
274 | ev_ref (EV_A); |
|
|
275 | ev_io_stop (EV_A_ iow + i); |
|
|
276 | } |
|
|
277 | |
|
|
278 | process (ads); |
|
|
279 | update_now (EV_A); |
299 | update_now (EV_A); |
280 | |
300 | |
281 | nfd = mfd; |
301 | nfd = mfd; |
282 | |
302 | |
283 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
303 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
… | |
… | |
287 | free (iow); iow = malloc (mfd * sizeof (ev_io)); |
307 | free (iow); iow = malloc (mfd * sizeof (ev_io)); |
288 | free (fds); fds = malloc (mfd * sizeof (struct pollfd)); |
308 | free (fds); fds = malloc (mfd * sizeof (struct pollfd)); |
289 | } |
309 | } |
290 | |
310 | |
291 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
311 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
292 | ev_timer_start (EV_A_ &tw); |
312 | ev_timer_start (EV_A, &tw); |
293 | ev_unref (EV_A); |
|
|
294 | |
313 | |
295 | // create one ev_io per pollfd |
314 | // create one ev_io per pollfd |
296 | for (i = 0; i < nfd; ++i) |
315 | for (i = 0; i < nfd; ++i) |
297 | { |
316 | { |
|
|
317 | ev_io *w = iow + i; |
|
|
318 | |
298 | ev_io_init (iow + i, io_cb, fds [i].fd, |
319 | ev_io_init (w, io_cb, fds [i].fd, |
299 | ((fds [i].events & POLLIN ? EV_READ : 0) |
320 | ((fds [i].events & POLLIN ? EV_READ : 0) |
300 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
321 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
301 | |
322 | |
|
|
323 | w->data = (void *)ads; |
302 | ev_io_start (EV_A_ iow + i); |
324 | ev_io_start (EV_A, w); |
303 | ev_unref (EV_A); |
|
|
304 | } |
325 | } |
305 | } |
326 | } |
306 | |
327 | |
307 | static HV *stash; |
328 | static HV *stash; |
308 | static adns_state ads; |
329 | static adns_state ads; |
… | |
… | |
405 | |
426 | |
406 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
427 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
407 | |
428 | |
408 | ev_prepare_init (&pw, prepare_cb); |
429 | ev_prepare_init (&pw, prepare_cb); |
409 | pw.data = (void *)ads; |
430 | pw.data = (void *)ads; |
410 | ev_prepare_start (EV_DEFAULT_ &pw); |
|
|
411 | ev_unref (EV_A); |
|
|
412 | |
431 | |
413 | ev_init (&iw, idle_cb); ev_set_priority (&iw, EV_MINPRI); |
432 | ev_init (&iw, idle_cb); ev_set_priority (&iw, EV_MINPRI); |
414 | iw.data = (void *)ads; |
433 | iw.data = (void *)ads; |
415 | ev_init (&tw, timer_cb); |
434 | ev_init (&tw, timer_cb); |
416 | tw.data = (void *)ads; |
435 | tw.data = (void *)ads; |
… | |
… | |
420 | PPCODE: |
439 | PPCODE: |
421 | { |
440 | { |
422 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
441 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
423 | struct ctx *c = (struct ctx *)SvPVX (csv); |
442 | struct ctx *c = (struct ctx *)SvPVX (csv); |
424 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
443 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
|
|
444 | |
|
|
445 | outstanding_inc (ads); |
425 | |
446 | |
426 | if (r) |
447 | if (r) |
427 | { |
448 | { |
428 | SvREFCNT_dec (csv); |
449 | SvREFCNT_dec (csv); |
429 | errno = r; |
450 | errno = r; |
430 | XSRETURN_EMPTY; |
451 | XSRETURN_EMPTY; |
431 | } |
452 | } |
432 | else |
453 | else |
433 | { |
454 | { |
434 | ev_ref (EV_A); |
|
|
435 | SvPOK_only (csv); |
455 | SvPOK_only (csv); |
436 | SvCUR_set (csv, sizeof (struct ctx)); |
456 | SvCUR_set (csv, sizeof (struct ctx)); |
437 | |
457 | |
438 | c->self = csv; |
458 | c->self = csv; |
439 | c->cb = newSVsv (cb); |
459 | c->cb = newSVsv (cb); |
|
|
460 | c->ads = ads; |
440 | |
461 | |
441 | if (!ev_is_active (&iw)) |
462 | if (!ev_is_active (&iw)) |
442 | ev_idle_start (EV_A_ &iw); |
463 | ev_idle_start (EV_DEFAULT, &iw); |
443 | |
464 | |
444 | if (GIMME_V != G_VOID) |
465 | if (GIMME_V != G_VOID) |
445 | { |
466 | { |
446 | csv = sv_2mortal (newRV_inc (csv)); |
467 | csv = sv_2mortal (newRV_inc (csv)); |
447 | sv_bless (csv, stash); |
468 | sv_bless (csv, stash); |
… | |
… | |
463 | |
484 | |
464 | c = (struct ctx *)SvPVX (SvRV (req)); |
485 | c = (struct ctx *)SvPVX (SvRV (req)); |
465 | |
486 | |
466 | if (c->cb) |
487 | if (c->cb) |
467 | { |
488 | { |
468 | ev_unref (EV_A); |
|
|
469 | SvREFCNT_dec (c->cb); |
489 | SvREFCNT_dec (c->cb); |
470 | c->cb = 0; |
490 | c->cb = 0; outstanding_dec (c->ads); |
471 | adns_cancel (c->query); |
491 | adns_cancel (c->query); |
472 | SvREFCNT_dec (c->self); |
492 | SvREFCNT_dec (c->self); |
473 | } |
493 | } |
474 | } |
494 | } |
475 | |
495 | |