6032a3c17167e0ce11b35eb3fe520d562bf250a4
[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         int active_event;
37         eNextState NextState;
38         ev_io recv_event, 
39                 send_event, 
40                 dns_io_event;
41         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
42
43         /* read/send related... */
44         StrBuf *IOBuf;
45         IOBuffer SendBuf, 
46                 RecvBuf;
47
48         /* Citadel application callbacks... */
49         IO_CallBack ReadDone, /* Theres new data to read... */
50                 SendDone,     /* we may send more data */
51                 Terminate,    /* shutting down... */
52                 Timeout,      /* Timeout handler; may also be connection timeout */
53                 ConnFail;     /* What to do when one connection failed? */
54
55         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
56
57         struct ares_options DNSOptions;
58         ares_channel DNSChannel;
59         ParseDNSAnswerCb DNS_CB;
60         IO_CallBack PostDNS;
61         int DNSStatus;
62         void *VParsedDNSReply;
63         FreeDNSReply DNSReplyFree;
64
65         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
66         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
67         void *Data; /* application specific data */
68 };
69
70 typedef struct _IOAddHandler {
71         void *Ctx;
72         EventContextAttach EvAttch;
73 }IOAddHandler; 
74
75 void FreeAsyncIOContents(AsyncIO *IO);
76
77 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB);
78 int ShutDownEventQueue(void);
79
80 void InitEventIO(AsyncIO *IO, 
81                  void *pData, 
82                  IO_CallBack ReadDone, 
83                  IO_CallBack SendDone, 
84                  IO_CallBack Terminate, 
85                  IO_CallBack Timeout, 
86                  IO_CallBack ConnFail, 
87                  IO_LineReaderCallback LineReader,
88                  int ReadFirst);
89
90 int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS);