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