Libev/libc-ares migration: cleanup
[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         ev_io recv_event, 
40                 send_event, 
41                 dns_io_event;
42         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
43
44         /* read/send related... */
45         StrBuf *IOBuf;
46         IOBuffer SendBuf, 
47                 RecvBuf;
48
49         /* Citadel application callbacks... */
50         IO_CallBack ReadDone, /* Theres new data to read... */
51                 SendDone,     /* we may send more data */
52                 Terminate,    /* shutting down... */
53                 Timeout,      /* Timeout handler; may also be connection timeout */
54                 ConnFail;     /* What to do when one connection failed? */
55
56         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
57
58         struct ares_options DNSOptions;
59         ares_channel DNSChannel;
60         ParseDNSAnswerCb DNS_CB;
61         IO_CallBack PostDNS;
62         int DNSStatus;
63         void *VParsedDNSReply;
64         FreeDNSReply DNSReplyFree;
65
66         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
67         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
68         void *Data; /* application specific data */
69 };
70
71 typedef struct _IOAddHandler {
72         void *Ctx;
73         EventContextAttach EvAttch;
74 }IOAddHandler; 
75
76 void FreeAsyncIOContents(AsyncIO *IO);
77
78 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB);
79 int ShutDownEventQueue(void);
80
81 void InitEventIO(AsyncIO *IO, 
82                  void *pData, 
83                  IO_CallBack ReadDone, 
84                  IO_CallBack SendDone, 
85                  IO_CallBack Terminate, 
86                  IO_CallBack Timeout, 
87                  IO_CallBack ConnFail, 
88                  IO_LineReaderCallback LineReader,
89                  int ReadFirst);
90
91 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS);