f9cf037b5967fe907b08ef3449724a4d950dbe88
[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)(void *Data);
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         int active_event;
38         eNextState NextState;
39         ev_io recv_event, 
40                 send_event, 
41                 dns_recv_event, 
42                 dns_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         struct ares_options DNSOptions;
60         ares_channel DNSChannel;
61         ParseDNSAnswerCb DNS_CB;
62         IO_CallBack PostDNS;
63         int DNSStatus;
64         void *VParsedDNSReply;
65         FreeDNSReply DNSReplyFree;
66
67         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
68         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
69         void *Data; /* application specific data */
70 };
71
72 typedef struct _IOAddHandler {
73         void *Ctx;
74         EventContextAttach EvAttch;
75 }IOAddHandler; 
76
77 void FreeAsyncIOContents(AsyncIO *IO);
78
79 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB);
80 int ShutDownEventQueue(void);
81
82 void InitEventIO(AsyncIO *IO, 
83                  void *pData, 
84                  IO_CallBack ReadDone, 
85                  IO_CallBack SendDone, 
86                  IO_CallBack Terminate, 
87                  IO_CallBack Timeout, 
88                  IO_CallBack ConnFail, 
89                  IO_LineReaderCallback LineReader,
90                  int ReadFirst);
91
92 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS);