1 #ifndef __EVENT_CLIENT_H__
2 #define __EVENT_CLIENT_H__
6 #include <sys/socket.h>
8 #include <arpa/nameser.h>
10 #include <curl/curl.h>
12 typedef struct AsyncIO AsyncIO;
14 typedef enum _eNextState {
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);
40 typedef struct __ReadAsyncMsg {
42 size_t maxlen; /* maximum message length */
44 const char *terminator; /* token signalling EOT */
48 int flushing; /* if we read maxlen, read until nothing more arives and ignore this. */
50 int crlf; /* CRLF newlines instead of LF */
54 typedef struct _DNSQueryParts {
55 ParseDNSAnswerCb DNS_CB;
59 void *VParsedDNSReply;
60 FreeDNSReply DNSReplyFree;
64 typedef struct _evcurl_request_data
67 struct curl_slist *headers;
68 char errdesc[CURL_ERROR_SIZE];
73 long PlainPostDataLen;
78 } evcurl_request_data;
84 /* connection related */
87 /* read/send related... */
92 FDIOBuffer IOB; /* when sending from / reading into files, this is used. */
95 ev_cleanup abort_by_shutdown, /* server wants to go down... */
96 db_abort_by_shutdown; /* server wants to go down... */
97 ev_timer conn_fail, /* connection establishing timed out */
98 rw_timeout; /* timeout while sending data */
99 ev_idle unwind_stack, /* get c-ares out of the stack */
100 db_unwind_stack, /* wait for next db operation... */
101 conn_fail_immediate; /* unwind stack, but fail immediately. */
102 ev_io recv_event, /* receive data from the client */
103 send_event, /* send more data to the client */
104 conn_event; /* Connection successfully established */
106 StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
108 /* Citadel application callbacks... */
109 IO_CallBack ReadDone, /* Theres new data to read... */
110 SendDone, /* we may send more data */
111 Terminate, /* shutting down... */
112 Timeout, /* Timeout handler; may also be connection timeout */
113 ConnFail, /* What to do when one connection failed? */
114 DNSFail, /* the dns lookup didn't work out. */
115 ShutdownAbort,/* we're going down. make your piece. */
116 NextDBOperation; /* Perform Database IO */
118 IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
121 ev_io dns_recv_event,
124 short int DnsSourcePort;
126 struct ares_options DNSOptions;
127 ares_channel DNSChannel;
128 DNSQueryParts *DNSQuery;
130 evcurl_request_data HttpReq;
132 /* Saving / loading a message async from / to disk */
133 ReadAsyncMsg *ReadMsg;
134 struct CtdlMessage *AsyncMsg;
135 struct recptypes *AsyncRcp;
136 /* Custom data; its expected to contain AsyncIO so we can save malloc()s... */
137 void *Data; /* application specific data */
138 void *CitContext; /* Citadel Session context... */
141 typedef struct _IOAddHandler {
146 #define CCID ((CitContext*)IO->CitContext)->cs_pid
147 #define EV_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
148 #define EVM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
150 #define EVNC_syslog(LEVEL, FORMAT, ...) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
151 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
153 void FreeAsyncIOContents(AsyncIO *IO);
155 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
156 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
157 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
158 eNextState QueueCurlContext(AsyncIO *IO);
159 int ShutDownEventQueue(void);
161 eNextState InitEventIO(AsyncIO *IO,
164 double first_rw_timeout,
166 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
168 int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
169 void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS);
171 void QueryCbDone(AsyncIO *IO);
173 void StopClient(AsyncIO *IO);
175 void StopClientWatchers(AsyncIO *IO);
177 void SetNextTimeout(AsyncIO *IO, double timeout);
179 void InitC_ares_dns(AsyncIO *IO);
181 #include <curl/curl.h>
185 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
187 syslog(LOG_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \
190 int evcurl_init(AsyncIO *IO,
193 IO_CallBack CallBack,
194 IO_CallBack Terminate,
195 IO_CallBack ShutdownAbort);
197 eNextState ReAttachIO(AsyncIO *IO,
201 #endif /* __EVENT_CLIENT_H__ */