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