3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "openbsd-compat.h"
20 #include <sys/types.h>
21 #include <sys/socket.h>
30 struct asr_query *async;
31 void (*cb)(struct asr_result *, void *);
35 struct event_asr * event_asr_run(struct asr_query *,
36 void (*)(struct asr_result *, void *), void *);
37 void event_asr_abort(struct event_asr *);
40 event_asr_dispatch(int fd __attribute__((__unused__)),
41 short ev __attribute__((__unused__)), void *arg)
43 struct event_asr *eva = arg;
49 if (asr_run(eva->async, &ar)) {
50 eva->cb(&ar, eva->arg);
53 event_set(&eva->ev, ar.ar_fd,
54 ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE,
55 event_asr_dispatch, eva);
56 tv.tv_sec = ar.ar_timeout / 1000;
57 tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
58 event_add(&eva->ev, &tv);
63 event_asr_run(struct asr_query *async, void (*cb)(struct asr_result *, void *),
66 struct event_asr *eva;
69 eva = calloc(1, sizeof *eva);
77 evtimer_set(&eva->ev, event_asr_dispatch, eva);
78 evtimer_add(&eva->ev, &tv);
83 event_asr_abort(struct event_asr *eva)
85 asr_abort(eva->async);