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