1 #ifndef __EVENT_CLIENT_H__
2 #define __EVENT_CLIENT_H__
7 #include <sys/socket.h>
9 #include <arpa/nameser.h>
11 #include <curl/curl.h>
13 typedef struct AsyncIO AsyncIO;
15 typedef enum _eNextState {
35 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
36 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
37 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
38 typedef void (*FreeDNSReply)(void *DNSData);
41 typedef struct __ReadAsyncMsg {
43 size_t maxlen; /* maximum message length */
45 const char *terminator; /* token signalling EOT */
49 int flushing; /* if we read maxlen, read until nothing more arives and ignore this. */
51 int crlf; /* CRLF newlines instead of LF */
55 typedef struct _DNSQueryParts {
56 ParseDNSAnswerCb DNS_CB;
60 void *VParsedDNSReply;
61 FreeDNSReply DNSReplyFree;
65 typedef struct _evcurl_request_data
68 struct curl_slist *headers;
69 char errdesc[CURL_ERROR_SIZE];
74 long PlainPostDataLen;
79 } evcurl_request_data;
82 typedef struct __evcares_data {
85 ev_timer timeout; /* timeout while requesting ips */
89 struct ares_options Options;
93 IO_CallBack Fail; /* the dns lookup didn't work out. */
100 /* connection related */
101 ParsedURL *ConnectMe;
103 /* read/send related... */
108 FDIOBuffer IOB; /* when sending from / reading into files, this is used. */
111 ev_cleanup abort_by_shutdown, /* server wants to go down... */
112 db_abort_by_shutdown; /* server wants to go down... */
113 ev_timer conn_fail, /* connection establishing timed out */
114 rw_timeout; /* timeout while sending data */
115 ev_idle unwind_stack, /* get c-ares out of the stack */
116 db_unwind_stack, /* wait for next db operation... */
117 conn_fail_immediate; /* unwind stack, but fail immediately. */
118 ev_io recv_event, /* receive data from the client */
119 send_event, /* send more data to the client */
120 conn_event; /* Connection successfully established */
122 StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
124 /* Citadel application callbacks... */
125 IO_CallBack ReadDone, /* Theres new data to read... */
126 SendDone, /* we may send more data */
127 Terminate, /* shutting down... */
128 Timeout, /* Timeout handler; may also be connection timeout */
129 ConnFail, /* What to do when one connection failed? */
130 ShutdownAbort,/* we're going down. make your piece. */
131 NextDBOperation; /* Perform Database IO */
133 IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
137 evcurl_request_data HttpReq;
139 /* Saving / loading a message async from / to disk */
140 ReadAsyncMsg *ReadMsg;
141 struct CtdlMessage *AsyncMsg;
142 struct recptypes *AsyncRcp;
144 /* Custom data; its expected to contain AsyncIO so we can save malloc()s... */
145 void *Data; /* application specific data */
146 void *CitContext; /* Citadel Session context... */
149 typedef struct _IOAddHandler {
154 #define CCID ((CitContext*)IO->CitContext)->cs_pid
155 #define EV_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
156 #define EVM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
158 #define EVNC_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
159 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
161 void FreeAsyncIOContents(AsyncIO *IO);
163 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
164 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
165 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
166 eNextState QueueCurlContext(AsyncIO *IO);
168 eNextState EvConnectSock(AsyncIO *IO,
170 double first_rw_timeout,
172 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
174 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
175 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
177 void QueryCbDone(AsyncIO *IO);
179 void StopClient(AsyncIO *IO);
181 void StopClientWatchers(AsyncIO *IO);
183 void SetNextTimeout(AsyncIO *IO, double timeout);
185 void InitC_ares_dns(AsyncIO *IO);
187 #include <curl/curl.h>
191 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
193 syslog(LOG_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
196 void InitIOStruct(AsyncIO *IO,
198 eNextState NextState,
199 IO_LineReaderCallback LineReader,
200 IO_CallBack DNS_Fail,
201 IO_CallBack SendDone,
202 IO_CallBack ReadDone,
203 IO_CallBack Terminate,
204 IO_CallBack ConnFail,
206 IO_CallBack ShutdownAbort);
208 int InitcURLIOStruct(AsyncIO *IO,
211 IO_CallBack SendDone,
212 IO_CallBack Terminate,
213 IO_CallBack ShutdownAbort);
215 eNextState ReAttachIO(AsyncIO *IO,
219 #endif /* __EVENT_CLIENT_H__ */