3 * Copyright (c) 1998-2012 by the citadel.org team
5 * This program is open source software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifndef __EVENT_CLIENT_H__
21 #define __EVENT_CLIENT_H__
23 #include "sysconfig.h"
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <arpa/nameser.h>
30 #include <curl/curl.h>
32 typedef struct AsyncIO AsyncIO;
34 typedef enum _eNextState {
54 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
55 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
56 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
57 typedef void (*FreeDNSReply)(void *DNSData);
60 typedef struct __ReadAsyncMsg {
62 size_t maxlen; /* maximum message length */
64 const char *terminator; /* token signalling EOT */
69 /* if we read maxlen, read until nothing more arives and ignore this. */
71 int crlf; /* CRLF newlines instead of LF */
75 typedef struct _DNSQueryParts {
76 ParseDNSAnswerCb DNS_CB;
80 void *VParsedDNSReply;
81 FreeDNSReply DNSReplyFree;
85 typedef struct _evcurl_request_data
88 struct curl_slist *headers;
89 char errdesc[CURL_ERROR_SIZE];
94 long PlainPostDataLen;
99 } evcurl_request_data;
102 typedef struct __evcares_data {
105 ev_timer timeout; /* timeout while requesting ips */
107 short int SourcePort;
109 struct ares_options Options;
110 ares_channel Channel;
111 DNSQueryParts *Query;
113 IO_CallBack Fail; /* the dns lookup didn't work out. */
118 eNextState NextState;
120 /* connection related */
121 ParsedURL *ConnectMe;
123 /* read/send related... */
129 /* when sending from / reading into files, this is used. */
132 ev_cleanup abort_by_shutdown, /* server wants to go down... */
133 db_abort_by_shutdown; /* server wants to go down... */
134 ev_timer conn_fail, /* connection establishing timed out */
135 rw_timeout; /* timeout while sending data */
136 ev_idle unwind_stack, /* get c-ares out of the stack */
137 db_unwind_stack, /* wait for next db operation... */
138 conn_fail_immediate; /* unwind stack, but fail immediately. */
139 ev_io recv_event, /* receive data from the client */
140 send_event, /* send more data to the client */
141 conn_event; /* Connection successfully established */
143 StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
145 /* Citadel application callbacks... */
146 IO_CallBack ReadDone, /* Theres new data to read... */
147 SendDone, /* we may send more data */
148 Terminate, /* shutting down... */
149 Timeout, /* Timeout handler;may also be conn. timeout */
150 ConnFail, /* What to do when one connection failed? */
151 ShutdownAbort,/* we're going down. make your piece. */
152 NextDBOperation; /* Perform Database IO */
154 /* if we have linereaders, maybe we want to read more lines before
155 * the real application logic is called? */
156 IO_LineReaderCallback LineReader;
160 evcurl_request_data HttpReq;
162 /* Saving / loading a message async from / to disk */
163 ReadAsyncMsg *ReadMsg;
164 struct CtdlMessage *AsyncMsg;
165 struct recptypes *AsyncRcp;
167 /* Context specific data; Hint: put AsyncIO in there */
168 void *Data; /* application specific data */
169 void *CitContext; /* Citadel Session context... */
172 typedef struct _IOAddHandler {
177 #define CCID ((CitContext*)IO->CitContext)->cs_pid
179 #define EV_syslog(LEVEL, FORMAT, ...) \
180 syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID, __VA_ARGS__)
182 #define EVM_syslog(LEVEL, FORMAT) \
183 syslog(LEVEL, "IO[%ld]CC[%d]" FORMAT, IO->ID, CCID)
185 #define EVNC_syslog(LEVEL, FORMAT, ...) \
186 syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID, __VA_ARGS__)
188 #define EVNCM_syslog(LEVEL, FORMAT) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
190 void FreeAsyncIOContents(AsyncIO *IO);
192 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
193 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
194 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
195 eNextState QueueCurlContext(AsyncIO *IO);
197 eNextState EvConnectSock(AsyncIO *IO,
199 double first_rw_timeout,
201 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
203 int QueueQuery(ns_type Type,
206 DNSQueryParts *QueryParts,
207 IO_CallBack PostDNS);
209 void QueueGetHostByName(AsyncIO *IO,
210 const char *Hostname,
211 DNSQueryParts *QueryParts,
212 IO_CallBack PostDNS);
214 void QueryCbDone(AsyncIO *IO);
216 void StopClient(AsyncIO *IO);
218 void StopClientWatchers(AsyncIO *IO);
220 void SetNextTimeout(AsyncIO *IO, double timeout);
222 void InitC_ares_dns(AsyncIO *IO);
224 #include <curl/curl.h>
228 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \
231 "error setting option " #s \
232 " on curl handle: %s", \
233 curl_easy_strerror(sta)); \
236 void InitIOStruct(AsyncIO *IO,
238 eNextState NextState,
239 IO_LineReaderCallback LineReader,
240 IO_CallBack DNS_Fail,
241 IO_CallBack SendDone,
242 IO_CallBack ReadDone,
243 IO_CallBack Terminate,
244 IO_CallBack ConnFail,
246 IO_CallBack ShutdownAbort);
248 int InitcURLIOStruct(AsyncIO *IO,
251 IO_CallBack SendDone,
252 IO_CallBack Terminate,
253 IO_CallBack ShutdownAbort);
255 eNextState ReAttachIO(AsyncIO *IO,
259 #endif /* __EVENT_CLIENT_H__ */