don't call curl_multi_add_handle() from outside of the event queue
[citadel.git] / citadel / event_client.h
1 #ifndef __EVENT_CLIENT_H__
2 #define __EVENT_CLIENT_H__
3 #define EV_COMPAT3 0
4 #include <ev.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netdb.h>
8 #include <arpa/nameser.h>
9 #include <ares.h>
10 #include <curl/curl.h>
11
12 typedef struct AsyncIO AsyncIO;
13
14 typedef enum _eNextState {
15         eSendDNSQuery,
16         eReadDNSReply,
17
18         eDBQuery,
19
20         eConnect,
21         eSendReply, 
22         eSendMore,
23         eSendFile,
24
25         eReadMessage, 
26         eReadMore,
27         eReadPayload,
28         eReadFile,
29
30         eTerminateConnection,
31         eAbort
32 }eNextState;
33
34 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
35 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
36 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
37 typedef void (*FreeDNSReply)(void *DNSData);
38
39
40 typedef struct __ReadAsyncMsg {
41         StrBuf *MsgBuf;
42         size_t maxlen;          /* maximum message length */
43
44         const char *terminator; /* token signalling EOT */
45         long tlen;
46         int dodot;
47
48         int flushing;           /* if we read maxlen, read until nothing more arives and ignore this. */
49
50         int crlf;               /* CRLF newlines instead of LF */
51 } ReadAsyncMsg;
52
53
54 typedef struct _DNSQueryParts {
55         ParseDNSAnswerCb DNS_CB;
56         IO_CallBack PostDNS;
57
58         int DNSStatus;
59         void *VParsedDNSReply;
60         FreeDNSReply DNSReplyFree;
61         void *Data;
62 } DNSQueryParts;
63
64 typedef struct _evcurl_request_data 
65 {
66         CURL              *chnd;
67         struct curl_slist *headers;
68         char               errdesc[CURL_ERROR_SIZE];
69
70         int                attached;
71
72         char              *PlainPostData;
73         long               PlainPostDataLen;
74         StrBuf            *PostData;
75
76         StrBuf            *ReplyData;
77         long               httpcode;
78 } evcurl_request_data;
79
80 struct AsyncIO {
81         eNextState NextState;
82
83         /* connection related */
84         ParsedURL *ConnectMe;
85         
86         /* read/send related... */
87         StrBuf *IOBuf;
88         IOBuffer SendBuf, 
89                 RecvBuf;
90
91         FDIOBuffer IOB; /* when sending from / reading into files, this is used. */
92
93         /* our events... */
94         ev_cleanup abort_by_shutdown, /* server wants to go down... */
95                 db_abort_by_shutdown; /* server wants to go down... */
96         ev_timer conn_fail,           /* connection establishing timed out */
97                 rw_timeout;           /* timeout while sending data */
98         ev_idle unwind_stack,         /* get c-ares out of the stack */
99                 db_unwind_stack,      /* wait for next db operation... */
100                 conn_fail_immediate;  /* unwind stack, but fail immediately. */
101         ev_io recv_event,             /* receive data from the client */
102                 send_event,           /* send more data to the client */
103                 conn_event;           /* Connection successfully established */
104
105         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
106
107         /* Citadel application callbacks... */
108         IO_CallBack ReadDone, /* Theres new data to read... */
109                 SendDone,     /* we may send more data */
110                 Terminate,    /* shutting down... */
111                 Timeout,      /* Timeout handler; may also be connection timeout */
112                 ConnFail,     /* What to do when one connection failed? */
113                 DNSFail,      /* the dns lookup didn't work out. */
114                 ShutdownAbort,/* we're going down. make your piece. */ 
115                 NextDBOperation; /* Perform Database IO */
116
117         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
118
119         /* DNS Related */
120         ev_io dns_recv_event, 
121                 dns_send_event;
122         struct ares_options DNSOptions;
123         ares_channel DNSChannel;
124         DNSQueryParts *DNSQuery;
125         
126         evcurl_request_data HttpReq;
127
128         /* Saving / loading a message async from / to disk */
129         ReadAsyncMsg *ReadMsg;
130         struct CtdlMessage *AsyncMsg;
131         struct recptypes *AsyncRcp;
132         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
133         void *Data;        /* application specific data */
134         void *CitContext;  /* Citadel Session context... */
135 };
136
137 typedef struct _IOAddHandler {
138         AsyncIO *IO;
139         IO_CallBack EvAttch;
140 }IOAddHandler; 
141
142 void FreeAsyncIOContents(AsyncIO *IO);
143
144 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
145 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
146 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
147 eNextState QueueCurlContext(AsyncIO *IO);
148 int ShutDownEventQueue(void);
149
150 eNextState InitEventIO(AsyncIO *IO, 
151                        void *pData, 
152                        double conn_timeout, 
153                        double first_rw_timeout,
154                        int ReadFirst);
155 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
156
157 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
158 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
159
160 void QueryCbDone(AsyncIO *IO);
161
162 void StopClient(AsyncIO *IO);
163
164 void StopClientWatchers(AsyncIO *IO);
165
166 void SetNextTimeout(AsyncIO *IO, double timeout);
167
168 void InitC_ares_dns(AsyncIO *IO);
169
170 #include <curl/curl.h>
171
172 #define OPT(s, v) \
173         do { \
174                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
175                 if (sta)  {                                             \
176                         syslog(LOG_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
177         } } while (0)
178
179
180 int evcurl_init(AsyncIO *IO, 
181                 void *CustomData, 
182                 const char* Desc,
183                 IO_CallBack CallBack, 
184                 IO_CallBack Terminate);
185
186 eNextState ReAttachIO(AsyncIO *IO, 
187                       void *pData, 
188                       int ReadFirst);
189
190 #endif /* __EVENT_CLIENT_H__ */