57d9204074b612ed4de2889e7ede7c92c4dbf8e1
[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 typedef struct _DNSQueryParts {
24         ParseDNSAnswerCb DNS_CB;
25         IO_CallBack PostDNS;
26
27         int DNSStatus;
28         void *VParsedDNSReply;
29         FreeDNSReply DNSReplyFree;
30         void *Data;
31 } DNSQueryParts;
32
33
34 struct AsyncIO {
35         StrBuf *Host;
36         char service[32];
37
38         /* To cycle through several possible services... */
39         struct addrinfo *res;
40         struct addrinfo *curr_ai;
41
42         /* connection related */
43         int IP6;
44         struct sockaddr_in6 Addr;
45
46         int sock;
47         unsigned short dport;
48         eNextState NextState;
49         
50         ev_cleanup abort_by_shutdown;
51
52         ev_timer conn_fail, 
53                 rw_timeout;
54         ev_idle unwind_stack;
55         ev_io recv_event, 
56                 send_event, 
57                 conn_event;
58         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
59
60         /* read/send related... */
61         StrBuf *IOBuf;
62         IOBuffer SendBuf, 
63                 RecvBuf;
64
65         /* Citadel application callbacks... */
66         IO_CallBack ReadDone, /* Theres new data to read... */
67                 SendDone,     /* we may send more data */
68                 Terminate,    /* shutting down... */
69                 Timeout,      /* Timeout handler; may also be connection timeout */
70                 ConnFail,     /* What to do when one connection failed? */
71                 ShutdownAbort;/* we're going down. make your piece. */ 
72
73         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
74
75         ev_io dns_recv_event, 
76                 dns_send_event;
77         struct ares_options DNSOptions;
78         ares_channel DNSChannel;
79         DNSQueryParts *DNSQuery;
80
81         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
82         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
83         void *Data; /* application specific data */
84 };
85
86 typedef struct _IOAddHandler {
87         AsyncIO *IO;
88         IO_CallBack EvAttch;
89 }IOAddHandler; 
90
91 void FreeAsyncIOContents(AsyncIO *IO);
92
93 int QueueEventContext(AsyncIO *IO, IO_CallBack CB);
94 int ShutDownEventQueue(void);
95
96 eNextState InitEventIO(AsyncIO *IO, 
97                        void *pData, 
98                        double conn_timeout, 
99                        double first_rw_timeout,
100                        int ReadFirst);
101 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
102
103 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
104 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
105
106 void QueryCbDone(AsyncIO *IO);
107
108 void StopClient(AsyncIO *IO);
109
110 void SetNextTimeout(AsyncIO *IO, double timeout);
111
112 void InitC_ares_dns(AsyncIO *IO);