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