… | |
… | |
4 | |
4 | |
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 | |
|
|
10 | static HV *stash; |
|
|
11 | static adns_state ads; |
|
|
12 | |
9 | |
13 | struct ctx |
10 | struct ctx |
14 | { |
11 | { |
15 | SV *self; |
12 | SV *self; |
16 | adns_query query; |
13 | adns_query query; |
… | |
… | |
26 | |
23 | |
27 | return newRV_noinc ((SV *)av); |
24 | return newRV_noinc ((SV *)av); |
28 | } |
25 | } |
29 | |
26 | |
30 | static void |
27 | static void |
31 | process () |
28 | process (adns_state ads) |
32 | { |
29 | { |
33 | dSP; |
30 | dSP; |
34 | |
31 | |
35 | for (;;) |
32 | for (;;) |
36 | { |
33 | { |
… | |
… | |
216 | static struct pollfd *fds; |
213 | static struct pollfd *fds; |
217 | static int nfd, mfd; |
214 | static int nfd, mfd; |
218 | static ev_io *iow; |
215 | static ev_io *iow; |
219 | static ev_timer tw; |
216 | static ev_timer tw; |
220 | static ev_idle iw; |
217 | static ev_idle iw; |
221 | static ev_prepare prepare_ev; |
218 | static ev_prepare pw; |
222 | static struct timeval tv_now; |
219 | static struct timeval tv_now; |
223 | |
220 | |
224 | static void |
221 | static void |
225 | update_now (EV_P) |
222 | update_now (EV_P) |
226 | { |
223 | { |
227 | ev_tstamp t = ev_now (); |
224 | ev_tstamp t = ev_now (); |
228 | |
225 | |
229 | tv_now.tv_sec = (long)t; |
226 | tv_now.tv_sec = (long)t; |
230 | tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e-6); |
227 | tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e6); |
231 | } |
228 | } |
232 | |
229 | |
233 | static void |
230 | static void |
234 | idle_cb (EV_P_ ev_idle *w, int revents) |
231 | idle_cb (EV_P_ ev_idle *w, int revents) |
235 | { |
232 | { |
… | |
… | |
237 | } |
234 | } |
238 | |
235 | |
239 | static void |
236 | static void |
240 | timer_cb (EV_P_ ev_timer *w, int revents) |
237 | timer_cb (EV_P_ ev_timer *w, int revents) |
241 | { |
238 | { |
|
|
239 | adns_state ads = (adns_state)w->data; |
|
|
240 | update_now (); |
|
|
241 | |
|
|
242 | adns_processtimeouts (ads, &tv_now); |
242 | } |
243 | } |
243 | |
244 | |
244 | static void |
245 | static void |
245 | io_cb (EV_P_ ev_io *w, int revents) |
246 | io_cb (EV_P_ ev_io *w, int revents) |
246 | { |
247 | { |
|
|
248 | adns_state ads = (adns_state)w->data; |
247 | update_now (EV_A); |
249 | update_now (EV_A); |
248 | |
250 | |
249 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
251 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
250 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
252 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
251 | } |
253 | } |
… | |
… | |
254 | static void |
256 | static void |
255 | prepare_cb (EV_P_ ev_prepare *w, int revents) |
257 | prepare_cb (EV_P_ ev_prepare *w, int revents) |
256 | { |
258 | { |
257 | int i; |
259 | int i; |
258 | int timeout = 3600000; |
260 | int timeout = 3600000; |
|
|
261 | adns_state ads = (adns_state)w->data; |
259 | |
262 | |
260 | if (ev_is_active (&tw)) |
263 | if (ev_is_active (&tw)) |
261 | { |
264 | { |
262 | ev_ref (); |
265 | ev_ref (); |
263 | ev_timer_stop (EV_A_ &tw); |
266 | ev_timer_stop (EV_A_ &tw); |
… | |
… | |
270 | { |
273 | { |
271 | ev_ref (); |
274 | ev_ref (); |
272 | ev_io_stop (EV_A_ iow + i); |
275 | ev_io_stop (EV_A_ iow + i); |
273 | } |
276 | } |
274 | |
277 | |
275 | process (); |
278 | process (ads); |
276 | |
|
|
277 | update_now (EV_A); |
279 | update_now (EV_A); |
278 | |
280 | |
279 | nfd = mfd; |
281 | nfd = mfd; |
280 | |
282 | |
281 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
283 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
… | |
… | |
299 | |
301 | |
300 | ev_io_start (EV_A_ iow + i); |
302 | ev_io_start (EV_A_ iow + i); |
301 | ev_unref (); |
303 | ev_unref (); |
302 | } |
304 | } |
303 | } |
305 | } |
|
|
306 | |
|
|
307 | static HV *stash; |
|
|
308 | static adns_state ads; |
304 | |
309 | |
305 | MODULE = EV::ADNS PACKAGE = EV::ADNS |
310 | MODULE = EV::ADNS PACKAGE = EV::ADNS |
306 | |
311 | |
307 | PROTOTYPES: ENABLE |
312 | PROTOTYPES: ENABLE |
308 | |
313 | |
… | |
… | |
396 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
401 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
397 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
402 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
398 | |
403 | |
399 | I_EV_API ("EV::ADNS"); |
404 | I_EV_API ("EV::ADNS"); |
400 | |
405 | |
|
|
406 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
|
|
407 | |
401 | ev_prepare_init (&prepare_ev, prepare_cb); |
408 | ev_prepare_init (&pw, prepare_cb); |
|
|
409 | pw.data = (void *)ads; |
402 | ev_prepare_start (EV_DEFAULT_ &prepare_ev); |
410 | ev_prepare_start (EV_DEFAULT_ &pw); |
403 | ev_unref (); |
411 | ev_unref (); |
404 | |
412 | |
405 | ev_init (&iw, idle_cb); |
413 | ev_init (&iw, idle_cb); ev_set_priority (&iw, EV_MINPRI); |
|
|
414 | iw.data = (void *)ads; |
406 | ev_init (&tw, timer_cb); |
415 | ev_init (&tw, timer_cb); |
407 | |
416 | tw.data = (void *)ads; |
408 | adns_init (&ads, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn | adns_if_noautosys, 0); |
|
|
409 | } |
417 | } |
410 | |
418 | |
411 | void submit (char *owner, int type, int flags, SV *cb) |
419 | void submit (char *owner, int type, int flags, SV *cb) |
412 | PPCODE: |
420 | PPCODE: |
413 | { |
421 | { |