X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fevent_client.h;h=de36f29ed126b31c3c82d4ec0ffb6bf2984961ad;hb=f1ee61891901850ebbdee1e9440b363dc6df540a;hp=c5761ffae482c27ceee0cc73e04f09f329b1b981;hpb=ddb2f5803b76a4a6120df8e845bcf165b3726f86;p=citadel.git diff --git a/citadel/event_client.h b/citadel/event_client.h index c5761ffae..de36f29ed 100644 --- a/citadel/event_client.h +++ b/citadel/event_client.h @@ -1,103 +1,182 @@ +#ifndef __EVENT_CLIENT_H__ +#define __EVENT_CLIENT_H__ +#define EV_COMPAT3 0 #include #include #include #include #include #include +#include typedef struct AsyncIO AsyncIO; typedef enum _eNextState { + eSendDNSQuery, + eReadDNSReply, + eDBQuery, + eConnect, eSendReply, eSendMore, eReadMessage, + eReadMore, + eReadPayload, eTerminateConnection, eAbort }eNextState; -typedef int (*EventContextAttach)(void *Data); typedef eNextState (*IO_CallBack)(AsyncIO *IO); typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO); typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int); typedef void (*FreeDNSReply)(void *DNSData); -struct AsyncIO { - StrBuf *Host; - char service[32]; - /* To cycle through several possible services... */ - struct addrinfo *res; - struct addrinfo *curr_ai; +typedef struct __ReadAsyncMsg { + StrBuf *MsgBuf; + size_t maxlen; /* maximum message length */ - /* connection related */ - int IP6; - struct hostent *HEnt; - int sock; - unsigned short dport; - eNextState NextState; + const char *terminator; /* token signalling EOT */ + long tlen; + int dodot; - ev_timer conn_fail, - conn_timeout; - ev_io recv_event, - send_event; - StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */ + int flushing; /* if we read maxlen, read until nothing more arives and ignore this. */ + + int crlf; /* CRLF newlines instead of LF */ +} ReadAsyncMsg; + + +typedef struct _DNSQueryParts { + ParseDNSAnswerCb DNS_CB; + IO_CallBack PostDNS; + + int DNSStatus; + void *VParsedDNSReply; + FreeDNSReply DNSReplyFree; + void *Data; +} DNSQueryParts; + +typedef struct _evcurl_request_data +{ + CURL *chnd; + struct curl_slist *headers; + char errdesc[CURL_ERROR_SIZE]; + + int attached; + + char *PlainPostData; + long PlainPostDataLen; + StrBuf *PostData; + + StrBuf *ReplyData; + long httpcode; +} evcurl_request_data; +struct AsyncIO { + eNextState NextState; + + /* connection related */ + ParsedURL *ConnectMe; + /* read/send related... */ StrBuf *IOBuf; IOBuffer SendBuf, RecvBuf; + /* our events... */ + ev_cleanup abort_by_shutdown, /* server wants to go down... */ + db_abort_by_shutdown; /* server wants to go down... */ + ev_timer conn_fail, /* connection establishing timed out */ + rw_timeout; /* timeout while sending data */ + ev_idle unwind_stack, /* get c-ares out of the stack */ + db_unwind_stack, /* wait for next db operation... */ + conn_fail_immediate; /* unwind stack, but fail immediately. */ + ev_io recv_event, /* receive data from the client */ + send_event, /* send more data to the client */ + conn_event; /* Connection successfully established */ + + StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */ + /* Citadel application callbacks... */ IO_CallBack ReadDone, /* Theres new data to read... */ SendDone, /* we may send more data */ Terminate, /* shutting down... */ Timeout, /* Timeout handler; may also be connection timeout */ - ConnFail; /* What to do when one connection failed? */ + ConnFail, /* What to do when one connection failed? */ + ShutdownAbort,/* we're going down. make your piece. */ + NextDBOperation; /* Perform Database IO */ IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */ - - int active_dns_event; + /* DNS Related */ ev_io dns_recv_event, dns_send_event; struct ares_options DNSOptions; ares_channel DNSChannel; - - ParseDNSAnswerCb DNS_CB; - IO_CallBack PostDNS; - - int DNSStatus; - void *VParsedDNSReply; - FreeDNSReply DNSReplyFree; - + DNSQueryParts *DNSQuery; + + evcurl_request_data HttpReq; + + /* Saving / loading a message async from / to disk */ + ReadAsyncMsg *ReadMsg; + struct CtdlMessage *AsyncMsg; + struct recptypes *AsyncRcp; /* Custom data; its expected to contain AsyncIO so we can save malloc()s... */ - DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */ - void *Data; /* application specific data */ + void *Data; /* application specific data */ + void *CitContext; /* Citadel Session context... */ }; typedef struct _IOAddHandler { - void *Ctx; - EventContextAttach EvAttch; + AsyncIO *IO; + IO_CallBack EvAttch; }IOAddHandler; void FreeAsyncIOContents(AsyncIO *IO); -int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB); +eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB); +eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB); +eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB); int ShutDownEventQueue(void); -void InitEventIO(AsyncIO *IO, - void *pData, - IO_CallBack ReadDone, - IO_CallBack SendDone, - IO_CallBack Terminate, - IO_CallBack Timeout, - IO_CallBack ConnFail, - IO_LineReaderCallback LineReader, - int conn_timeout, int first_rw_timeout, - int ReadFirst); +eNextState InitEventIO(AsyncIO *IO, + void *pData, + double conn_timeout, + double first_rw_timeout, + int ReadFirst); +void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents); -int QueueQuery(ns_type Type, char *name, AsyncIO *IO, IO_CallBack PostDNS); +int QueueQuery(ns_type Type, const char *name, AsyncIO *IO, DNSQueryParts *QueryParts, IO_CallBack PostDNS); +void QueueGetHostByName(AsyncIO *IO, const char *Hostname, DNSQueryParts *QueryParts, IO_CallBack PostDNS); + +void QueryCbDone(AsyncIO *IO); void StopClient(AsyncIO *IO); -void SetNextTimeout(AsyncIO *IO, int timeout); +void StopClientWatchers(AsyncIO *IO); + +void SetNextTimeout(AsyncIO *IO, double timeout); + +void InitC_ares_dns(AsyncIO *IO); + +#include + +#define OPT(s, v) \ + do { \ + sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v)); \ + if (sta) { \ + syslog(LOG_ERR, "error setting option " #s " on curl handle: %s", curl_easy_strerror(sta)); \ + } } while (0) + + +int evcurl_init(AsyncIO *IO, + void *CustomData, + const char* Desc, + IO_CallBack CallBack, + IO_CallBack Terminate); + +eNextState ReAttachIO(AsyncIO *IO, + void *pData, + int ReadFirst); + +void evcurl_handle_start(AsyncIO *IO); + +#endif /* __EVENT_CLIENT_H__ */