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