libev migration
[citadel.git] / citadel / event_client.h
1 #include <ev.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netdb.h>
5
6 typedef struct AsyncIO AsyncIO;
7
8 typedef enum _eNextState {
9         eSendReply, 
10         eSendMore,
11         eReadMessage, 
12         eTerminateConnection,
13         eAbort
14 }eNextState;
15
16 typedef int (*EventContextAttach)(void *Data);
17 typedef eNextState (*IO_CallBack)(void *Data);
18 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
19
20 struct AsyncIO {
21         StrBuf *Host;
22         char service[32];
23
24         /* To cycle through several possible services... */
25         struct addrinfo *res;
26         struct addrinfo *curr_ai;
27
28         /* connection related */
29         int sock;
30         int active_event;
31         eNextState NextState;
32         ev_io recv_event, 
33                 send_event, 
34                 conn_event;
35         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
36
37         /* read/send related... */
38         StrBuf *IOBuf;
39         IOBuffer SendBuf, 
40                 RecvBuf;
41
42         /* Citadel application callbacks... */
43         IO_CallBack ReadDone, /* Theres new data to read... */
44                 SendDone,     /* we may send more data */
45                 Terminate,    /* shutting down... */
46                 Timeout,      /* Timeout handler; may also be connection timeout */
47                 ConnFail,     /* What to do when one connection failed? */
48                 CustomDNS;    /* If the application wants to do custom dns functionality like cycle through different MX-Records */
49
50         IO_LineReaderCallback LineReader; /* if we have linereaders, maybe we want to read more lines before the real application logic is called? */
51
52         /* Custom data; its expected to contain  AsyncIO so we can save malloc()s... */
53         DeleteHashDataFunc DeleteData; /* so if we have to destroy you, what to do... */
54         void *Data; /* application specific data */
55 };
56
57 typedef struct _IOAddHandler {
58         void *Ctx;
59         EventContextAttach EvAttch;
60 }IOAddHandler; 
61
62 void FreeAsyncIOContents(AsyncIO *IO);
63
64 int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB);
65 int ShutDownEventQueue(void);
66
67 void InitEventIO(AsyncIO *IO, 
68                  void *pData, 
69                  IO_CallBack ReadDone, 
70                  IO_CallBack SendDone, 
71                  IO_CallBack Terminate, 
72                  IO_CallBack Timeout, 
73                  IO_CallBack ConnFail, 
74                  IO_CallBack CustomDNS,
75                  IO_LineReaderCallback LineReader,
76                  int ReadFirst);