Migrate http context struct into the AsyncIO struct; streamline other structs.
[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 #include <curl/curl.h>
8
9 typedef struct AsyncIO AsyncIO;
10
11 typedef enum _eNextState {
12         eSendDNSQuery,
13         eReadDNSReply,
14         eConnect,
15         eSendReply, 
16         eSendMore,
17         eReadMessage, 
18         eTerminateConnection,
19         eAbort
20 }eNextState;
21
22 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
23 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
24 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
25 typedef void (*FreeDNSReply)(void *DNSData);
26
27 typedef struct _DNSQueryParts {
28         ParseDNSAnswerCb DNS_CB;
29         IO_CallBack PostDNS;
30
31         int DNSStatus;
32         void *VParsedDNSReply;
33         FreeDNSReply DNSReplyFree;
34         void *Data;
35 } DNSQueryParts;
36
37 typedef struct _evcurl_request_data 
38 {
39         CURL *chnd;
40         char errdesc[CURL_ERROR_SIZE];
41         int attached;
42         char* PlainPostData;
43         long PlainPostDataLen;
44         StrBuf *PostData;
45         StrBuf *ReplyData;
46 ///     ParsedURL *URL; /// take from AsyncIO->ConnectMe
47         struct curl_slist * headers;
48 } evcurl_request_data;
49
50 struct AsyncIO {
51         eNextState NextState;
52
53         /* connection related */
54         ParsedURL *ConnectMe;
55         
56         /* read/send related... */
57         StrBuf *IOBuf;
58         IOBuffer SendBuf, 
59                 RecvBuf;
60
61         /* our events... */
62         ev_cleanup abort_by_shutdown; /* server wants to go down... */
63         ev_timer conn_fail,           /* connection establishing timed out */
64                 rw_timeout;           /* timeout while sending data */
65         ev_idle unwind_stack;         /* get c-ares out of the stack */
66         ev_io recv_event,             /* receive data from the client */
67                 send_event,           /* send more data to the client */
68                 conn_event;           /* Connection successfully established */
69
70         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
71
72         /* Citadel application callbacks... */
73         IO_CallBack ReadDone, /* Theres new data to read... */
74                 SendDone,     /* we may send more data */
75                 Terminate,    /* shutting down... */
76                 Timeout,      /* Timeout handler; may also be connection timeout */
77                 ConnFail,     /* What to do when one connection failed? */
78                 ShutdownAbort;/* we're going down. make your piece. */ 
79
80         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
81
82         /* DNS Related */
83         ev_io dns_recv_event, 
84                 dns_send_event;
85         struct ares_options DNSOptions;
86         ares_channel DNSChannel;
87         DNSQueryParts *DNSQuery;
88         
89         evcurl_request_data HttpReq;
90
91         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
92         void *Data;        /* application specific data */
93         void *CitContext;  /* Citadel Session context... */
94 };
95
96 typedef struct _IOAddHandler {
97         AsyncIO *IO;
98         IO_CallBack EvAttch;
99 }IOAddHandler; 
100
101 void FreeAsyncIOContents(AsyncIO *IO);
102
103 int QueueEventContext(AsyncIO *IO, IO_CallBack CB);
104 int ShutDownEventQueue(void);
105
106 eNextState InitEventIO(AsyncIO *IO, 
107                        void *pData, 
108                        double conn_timeout, 
109                        double first_rw_timeout,
110                        int ReadFirst);
111 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
112
113 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
114 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
115
116 void QueryCbDone(AsyncIO *IO);
117
118 void StopClient(AsyncIO *IO);
119
120 void StopClientWatchers(AsyncIO *IO);
121
122 void SetNextTimeout(AsyncIO *IO, double timeout);
123
124 void InitC_ares_dns(AsyncIO *IO);
125
126 #include <curl/curl.h>
127
128 #define OPT(s, v) \
129         do { \
130                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
131                 if (sta)  {                                             \
132                         CtdlLogPrintf(CTDL_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
133         } } while (0)
134
135
136 int evcurl_init(AsyncIO *IO, 
137                 void *CustomData, 
138                 const char* Desc,
139                 int CallBack);
140
141 void evcurl_handle_start(AsyncIO *IO);