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