… | |
… | |
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 = 0; |
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 | |
|
|
34 | printf ("check %d\n", r);//D |
28 | if (r) |
35 | if (r) |
29 | 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); |
30 | } |
54 | } |
31 | } |
55 | } |
32 | |
56 | |
33 | static struct pollfd *fds; |
57 | static struct pollfd *fds; |
34 | static int nfd; |
58 | static int nfd, mfd; |
35 | static ev_io *iow; |
59 | static ev_io *iow; |
36 | static ev_timer tw; |
60 | static ev_timer tw; |
37 | static ev_prepare prepare_ev; |
61 | static ev_prepare prepare_ev; |
38 | static struct timeval *tv_now; |
62 | static struct timeval tv_now; |
39 | |
63 | |
40 | static void |
64 | static void |
41 | update_now (EV_P) |
65 | update_now (EV_P) |
42 | { |
66 | { |
43 | ev_tstamp t = ev_now (EV_P); |
67 | ev_tstamp t = ev_now (); |
44 | |
68 | |
45 | tv.tv_sec = (long)t; |
69 | tv_now.tv_sec = (long)t; |
46 | tv.tv_usec = (long)((t - (ev_tstamp)tv.tv_sec) * 1e-6); |
70 | tv_now.tv_usec = (long)((t - (ev_tstamp)tv_now.tv_sec) * 1e-6); |
47 | } |
71 | } |
48 | |
72 | |
49 | static void |
73 | static void |
50 | timer_cb (EV_P_ ev_timer *w, int revents) |
74 | timer_cb (EV_P_ ev_timer *w, int revents) |
51 | { |
75 | { |
… | |
… | |
74 | |
98 | |
75 | process (); |
99 | process (); |
76 | |
100 | |
77 | update_now (EV_A); |
101 | update_now (EV_A); |
78 | |
102 | |
|
|
103 | nfd = mfd; |
|
|
104 | |
79 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
105 | while (adns_beforepoll (ads, fds, &nfd, &timeout, &tv_now)) |
80 | { |
106 | { |
|
|
107 | mfd = nfd; |
|
|
108 | |
81 | free (iow); iow = malloc (nfd * sizeof (struct pollfd)); |
109 | free (iow); iow = malloc (mfd * sizeof (struct pollfd)); |
82 | free (fds); fds = malloc (nfd * sizeof (ev_io)); |
110 | free (fds); fds = malloc (mfd * sizeof (ev_io)); |
83 | } |
111 | } |
|
|
112 | |
|
|
113 | printf ("to %d\n", timeout);//d |
84 | |
114 | |
85 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
115 | ev_timer_set (&tw, timeout * 1e-3, 0.); |
86 | ev_timer_start (EV_A_ &tw); |
116 | ev_timer_start (EV_A_ &tw); |
87 | |
117 | |
88 | // create on ev_io per pollfd |
118 | // create one ev_io per pollfd |
89 | for (i = 0; i < nfd; ++i) |
119 | for (i = 0; i < nfd; ++i) |
90 | { |
120 | { |
91 | ev_io_init (iow + i, io_cb, fds [i].fd, |
121 | ev_io_init (iow + i, io_cb, fds [i].fd, |
92 | ((fds [i].events & POLLIN ? EV_READ : 0) |
122 | ((fds [i].events & POLLIN ? EV_READ : 0) |
93 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
123 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
… | |
… | |
100 | |
130 | |
101 | PROTOTYPES: ENABLE |
131 | PROTOTYPES: ENABLE |
102 | |
132 | |
103 | BOOT: |
133 | BOOT: |
104 | { |
134 | { |
105 | #if 0 |
|
|
106 | HV *stash = gv_stashpv ("EV::ADNS", 1); |
135 | stash = gv_stashpv ("EV::ADNS", 1); |
107 | |
136 | |
108 | static const struct { |
137 | static const struct { |
109 | const char *name; |
138 | const char *name; |
110 | IV iv; |
139 | IV iv; |
111 | } *civ, const_iv[] = { |
140 | } *civ, const_iv[] = { |
112 | # define const_iv(pfx, name) { # name, (IV) adns_ ## name }, |
141 | # define const_iv(name) { # name, (IV) adns_ ## name }, |
113 | const_iv (if_none) |
142 | const_iv (if_none) |
114 | const_iv (if_noenv) |
143 | const_iv (if_noenv) |
115 | const_iv (if_noerrprint) |
144 | const_iv (if_noerrprint) |
116 | const_iv (if_noserverwarn) |
145 | const_iv (if_noserverwarn) |
117 | const_iv (if_debug) |
146 | const_iv (if_debug) |
… | |
… | |
188 | const_iv (s_max_permfail) |
217 | const_iv (s_max_permfail) |
189 | }; |
218 | }; |
190 | |
219 | |
191 | 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; ) |
192 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
221 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
193 | #endif |
|
|
194 | |
222 | |
195 | I_EV_API ("EV::ADNS"); |
223 | I_EV_API ("EV::ADNS"); |
196 | |
224 | |
197 | 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); |
198 | ev_unref (); |
226 | ev_unref (); |
… | |
… | |
200 | ev_init (&tw, timer_cb); |
228 | ev_init (&tw, timer_cb); |
201 | |
229 | |
202 | 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); |
203 | } |
231 | } |
204 | |
232 | |
205 | int adns_submit (char *owner, int type, int flags, SV *cb) |
233 | void submit (char *owner, int type, int flags, SV *cb) |
206 | CODE: |
234 | PPCODE: |
207 | { |
235 | { |
|
|
236 | SV *csv = NEWSV (0, sizeof (struct ctx)); |
208 | struct ctx *c = (struct ctx *)malloc (sizeof (ctx)); |
237 | struct ctx *c = (struct ctx *)SvPVX (csv); |
209 | adns_query q; |
|
|
210 | int r = adns_submit (owner, type, flags, (void *)c, &q); |
238 | int r = adns_submit (ads, owner, type, flags, (void *)c, &c->query); |
211 | |
239 | |
212 | if (r) |
240 | if (r) |
213 | { |
241 | { |
214 | free (c); |
242 | SvREFCNT_dec (csv); |
215 | XSRETURN_EMPTY; |
243 | XSRETURN_EMPTY; |
216 | } |
244 | } |
217 | } |
245 | else |
|
|
246 | { |
|
|
247 | ev_ref (); |
|
|
248 | SvPOK_only (csv); |
|
|
249 | SvCUR_set (csv, sizeof (struct ctx)); |
|
|
250 | c->self = csv; |
|
|
251 | c->cb = newSVsv (cb); |
218 | |
252 | |
|
|
253 | if (GIMME_V != G_VOID) |
|
|
254 | { |
|
|
255 | csv = sv_2mortal (newRV_inc (csv)); |
|
|
256 | sv_bless (csv, stash); |
|
|
257 | XPUSHs (csv); |
|
|
258 | } |
|
|
259 | } |
|
|
260 | } |
219 | |
261 | |
|
|
262 | void DESTROY (SV *req) |
|
|
263 | CODE: |
|
|
264 | { |
|
|
265 | struct ctx *c; |
220 | |
266 | |
|
|
267 | if (!(SvROK (req) && SvOBJECT (SvRV (req)) |
|
|
268 | && (SvSTASH (SvRV (req)) == stash))) |
|
|
269 | croak ("object is not of type EV::ADNS"); |
|
|
270 | |
|
|
271 | c = (struct ctx *)SvPVX (SvRV (req)); |
|
|
272 | |
|
|
273 | if (c->cb) |
|
|
274 | { |
|
|
275 | ev_unref (); |
|
|
276 | adns_cancel (c->query); |
|
|
277 | SvREFCNT_dec (c->cb); |
|
|
278 | } |
|
|
279 | } |
|
|
280 | |
|
|
281 | |
|
|
282 | |
|
|
283 | |