Libev / libc-ares Migration
[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 int (*EventContextAttach)(void *Data);
19 typedef eNextState (*IO_CallBack)(void *Data);
20 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
21 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
22 typedef void (*FreeDNSReply)(void *DNSData);
23
24 struct AsyncIO {
25         StrBuf *Host;
26         char service[32];
27
28         /* To cycle through several possible services... */
29         struct addrinfo *res;
30         struct addrinfo *curr_ai;
31
32         /* connection related */
33         int IP6;
34         struct hostent *HEnt;
35         int sock;
36         unsigned short dport;
37         int active_event;
38         eNextState NextState;
39
40         ev_timer conn_fail, 
41                 conn_timeout;
42         ev_io recv_event, 
43                 send_event;
44         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
45
46         /* read/send related... */
47         StrBuf *IOBuf;
48         IOBuffer SendBuf, 
49                 RecvBuf;
50
51         /* Citadel application callbacks... */
52         IO_CallBack ReadDone, /* Theres new data to read... */
53                 SendDone,     /* we may send more data */
54                 Terminate,    /* shutting down... */
55                 Timeout,      /* Timeout handler; may also be connection timeout */
56                 ConnFail;     /* What to do when one connection failed? */
57
58         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
59
60
61         int active_dns_event;
62         ev_io dns_recv_event, 
63                 dns_send_event;
64         struct ares_options DNSOptions;
65         ares_channel DNSChannel;
66
67         ParseDNSAnswerCb DNS_CB;
68         IO_CallBack PostDNS;
69
70         int DNSStatus;
71         void *VParsedDNSReply;
72         FreeDNSReply DNSReplyFree;
73
74         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
75         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
76         void *Data; /* application specific data */
77 };
78
79 typedef struct _IOAddHandler {
80         void *Ctx;
81         EventContextAttach EvAttch;
82 }IOAddHandler; 
83
84 void FreeAsyncIOContents(AsyncIO *IO);
85
86 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB);
87 int ShutDownEventQueue(void);
88
89 void InitEventIO(AsyncIO *IO, 
90                  void *pData, 
91                  IO_CallBack ReadDone, 
92                  IO_CallBack SendDone, 
93                  IO_CallBack Terminate, 
94                  IO_CallBack Timeout, 
95                  IO_CallBack ConnFail, 
96                  IO_LineReaderCallback LineReader,
97                  int conn_timeout, int first_rw_timeout,
98                  int ReadFirst);
99
100 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS);