libev/c-ares migration: unstack out ouf c-ares before querying new requests
[citadel.git] / citadel / event_client.h
1 #include <ev.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netdb.h>
5 #include <arpa/nameser.h>
6 #include <ares.h>
7
8 typedef struct AsyncIO AsyncIO;
9
10 typedef enum _eNextState {
11         eSendReply, 
12         eSendMore,
13         eReadMessage, 
14         eTerminateConnection,
15         eAbort
16 }eNextState;
17
18 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
19 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
20 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
21 typedef void (*FreeDNSReply)(void *DNSData);
22
23 struct AsyncIO {
24         StrBuf *Host;
25         char service[32];
26
27         /* To cycle through several possible services... */
28         struct addrinfo *res;
29         struct addrinfo *curr_ai;
30
31         /* connection related */
32         int IP6;
33         struct hostent *HEnt;
34         struct sockaddr_in6 Addr;
35
36         int sock;
37         unsigned short dport;
38         eNextState NextState;
39
40         ev_timer conn_fail, 
41                 rw_timeout,
42                 unwind_stack_timeout;
43         ev_io recv_event, 
44                 send_event, 
45                 conn_event;
46         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
47
48         /* read/send related... */
49         StrBuf *IOBuf;
50         IOBuffer SendBuf, 
51                 RecvBuf;
52
53         /* Citadel application callbacks... */
54         IO_CallBack ReadDone, /* Theres new data to read... */
55                 SendDone,     /* we may send more data */
56                 Terminate,    /* shutting down... */
57                 Timeout,      /* Timeout handler; may also be connection timeout */
58                 ConnFail;     /* What to do when one connection failed? */
59
60         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
61
62
63         int active_dns_event;
64         ev_io dns_recv_event, 
65                 dns_send_event;
66         struct ares_options DNSOptions;
67         ares_channel DNSChannel;
68
69         ParseDNSAnswerCb DNS_CB;
70         IO_CallBack PostDNS;
71
72         int DNSStatus;
73         void *VParsedDNSReply;
74         FreeDNSReply DNSReplyFree;
75
76         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
77         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
78         void *Data; /* application specific data */
79 };
80
81 typedef struct _IOAddHandler {
82         AsyncIO *IO;
83         IO_CallBack EvAttch;
84 }IOAddHandler; 
85
86 void FreeAsyncIOContents(AsyncIO *IO);
87
88 int QueueEventContext(AsyncIO *IO, IO_CallBack CB);
89 int ShutDownEventQueue(void);
90
91 eNextState InitEventIO(AsyncIO *IO, 
92                        void *pData, 
93                        double conn_timeout, 
94                        double first_rw_timeout,
95                        int ReadFirst);
96 void IO_postdns_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
97
98 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS);
99 void QueryCbDone(AsyncIO *IO);
100
101 void StopClient(AsyncIO *IO);
102
103 void SetNextTimeout(AsyncIO *IO, double timeout);
104
105 void InitC_ares_dns(AsyncIO *IO);