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;
85 /* connection related */
88 /* read/send related... */
93 FDIOBuffer IOB; /* when sending from / reading into files, this is used. */
96 ev_cleanup abort_by_shutdown, /* server wants to go down... */
97 db_abort_by_shutdown; /* server wants to go down... */
98 ev_timer conn_fail, /* connection establishing timed out */
99 rw_timeout; /* timeout while sending data */
100 ev_idle unwind_stack, /* get c-ares out of the stack */
101 db_unwind_stack, /* wait for next db operation... */
102 conn_fail_immediate; /* unwind stack, but fail immediately. */
103 ev_io recv_event, /* receive data from the client */
104 send_event, /* send more data to the client */
105 conn_event; /* Connection successfully established */
107 StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
109 /* Citadel application callbacks... */
110 IO_CallBack ReadDone, /* Theres new data to read... */
111 SendDone, /* we may send more data */
112 Terminate, /* shutting down... */
113 Timeout, /* Timeout handler; may also be connection timeout */
114 ConnFail, /* What to do when one connection failed? */
115 DNSFail, /* the dns lookup didn't work out. */
116 ShutdownAbort,/* we're going down. make your piece. */
117 NextDBOperation; /* Perform Database IO */
119 IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
122 ev_io dns_recv_event,
124 ev_timer dns_timeout; /* timeout while requesting ips */
126 short int DnsSourcePort;
128 struct ares_options DNSOptions;
129 ares_channel DNSChannel;
130 DNSQueryParts *DNSQuery;
132 evcurl_request_data HttpReq;
134 /* Saving / loading a message async from / to disk */
135 ReadAsyncMsg *ReadMsg;
136 struct CtdlMessage *AsyncMsg;
137 struct recptypes *AsyncRcp;
139 /* Custom data; its expected to contain AsyncIO so we can save malloc()s... */
140 void *Data; /* application specific data */
141 void *CitContext; /* Citadel Session context... */
144 typedef struct _IOAddHandler {
149 #define CCID ((CitContext*)IO->CitContext)->cs_pid
150 #define EV_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
151 #define EVM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
153 #define EVNC_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
154 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
156 void FreeAsyncIOContents(AsyncIO *IO);
158 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
159 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
160 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
161 eNextState QueueCurlContext(AsyncIO *IO);
162 int ShutDownEventQueue(void);
164 eNextState InitEventIO(AsyncIO *IO,
167 double first_rw_timeout,
169 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
171 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
172 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
174 void QueryCbDone(AsyncIO *IO);
176 void StopClient(AsyncIO *IO);
178 void StopClientWatchers(AsyncIO *IO);
180 void SetNextTimeout(AsyncIO *IO, double timeout);
182 void InitC_ares_dns(AsyncIO *IO);
184 #include <curl/curl.h>
188 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
190 syslog(LOG_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
193 int evcurl_init(AsyncIO *IO,
196 IO_CallBack CallBack,
197 IO_CallBack Terminate,
198 IO_CallBack ShutdownAbort);
200 eNextState ReAttachIO(AsyncIO *IO,
204 #endif /* __EVENT_CLIENT_H__ */