2 * Copyright (c) 1998-2012 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
22 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
47 #include <libcitadel.h>
50 #include "citserver.h"
57 #include "internet_addressing.h"
60 #include "clientsocket.h"
61 #include "locate_host.h"
62 #include "citadel_dirs.h"
64 #include "event_client.h"
65 #include "ctdl_module.h"
67 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
68 static void IO_abort_shutdown_callback(struct ev_loop *loop,
73 /*------------------------------------------------------------------------------
75 *----------------------------------------------------------------------------*/
76 extern int evdb_count;
77 extern pthread_mutex_t DBEventQueueMutex;
78 extern pthread_mutex_t DBEventExitQueueMutex;
79 extern HashList *DBInboundEventQueue;
80 extern struct ev_loop *event_db;
81 extern ev_async DBAddJob;
82 extern ev_async DBExitEventLoop;
84 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
89 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
92 ev_cleanup_init(&IO->db_abort_by_shutdown,
93 IO_abort_shutdown_callback);
94 IO->db_abort_by_shutdown.data = IO;
96 pthread_mutex_lock(&DBEventQueueMutex);
97 if (DBInboundEventQueue == NULL)
99 /* shutting down... */
101 EVM_syslog(LOG_DEBUG, "DBEVENT Q exiting.\n");
102 pthread_mutex_unlock(&DBEventQueueMutex);
105 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
107 Put(DBInboundEventQueue, IKEY(i), h, NULL);
108 pthread_mutex_unlock(&DBEventQueueMutex);
110 pthread_mutex_lock(&DBEventExitQueueMutex);
111 if (event_db == NULL)
113 pthread_mutex_unlock(&DBEventExitQueueMutex);
116 ev_async_send (event_db, &DBAddJob);
117 pthread_mutex_unlock(&DBEventExitQueueMutex);
119 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
123 void StopDBWatchers(AsyncIO *IO)
125 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
126 ev_idle_stop(event_db, &IO->db_unwind_stack);
129 void ShutDownDBCLient(AsyncIO *IO)
131 CitContext *Ctx =IO->CitContext;
134 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
137 assert(IO->DBTerminate);
142 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
144 AsyncIO *IO = watcher->data;
145 IO->Now = ev_now(event_db);
146 EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
147 become_session(IO->CitContext);
149 ev_idle_stop(event_db, &IO->db_unwind_stack);
151 assert(IO->NextDBOperation);
152 switch (IO->NextDBOperation(IO))
166 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
168 case eTerminateConnection:
170 ev_idle_stop(event_db, &IO->db_unwind_stack);
171 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
172 ShutDownDBCLient(IO);
176 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
178 IO->NextDBOperation = CB;
179 ev_idle_init(&IO->db_unwind_stack,
181 IO->db_unwind_stack.data = IO;
182 ev_idle_start(event_db, &IO->db_unwind_stack);
186 /*------------------------------------------------------------------------------
188 *----------------------------------------------------------------------------*/
189 extern int evbase_count;
190 extern pthread_mutex_t EventQueueMutex;
191 extern pthread_mutex_t EventExitQueueMutex;
192 extern HashList *InboundEventQueue;
193 extern struct ev_loop *event_base;
194 extern ev_async AddJob;
195 extern ev_async ExitEventLoop;
197 static void IO_abort_shutdown_callback(struct ev_loop *loop,
201 AsyncIO *IO = watcher->data;
202 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
203 IO->Now = ev_now(event_base);
204 assert(IO->ShutdownAbort);
205 IO->ShutdownAbort(IO);
209 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
214 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
217 ev_cleanup_init(&IO->abort_by_shutdown,
218 IO_abort_shutdown_callback);
219 IO->abort_by_shutdown.data = IO;
221 pthread_mutex_lock(&EventQueueMutex);
222 if (InboundEventQueue == NULL)
225 /* shutting down... */
226 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
227 pthread_mutex_unlock(&EventQueueMutex);
230 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
232 Put(InboundEventQueue, IKEY(i), h, NULL);
233 pthread_mutex_unlock(&EventQueueMutex);
235 pthread_mutex_lock(&EventExitQueueMutex);
236 if (event_base == NULL) {
237 pthread_mutex_unlock(&EventExitQueueMutex);
240 ev_async_send (event_base, &AddJob);
241 pthread_mutex_unlock(&EventExitQueueMutex);
242 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
246 extern eNextState evcurl_handle_start(AsyncIO *IO);
248 eNextState QueueCurlContext(AsyncIO *IO)
253 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
255 h->EvAttch = evcurl_handle_start;
257 pthread_mutex_lock(&EventQueueMutex);
258 if (InboundEventQueue == NULL)
260 /* shutting down... */
262 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
263 pthread_mutex_unlock(&EventQueueMutex);
267 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
269 Put(InboundEventQueue, IKEY(i), h, NULL);
270 pthread_mutex_unlock(&EventQueueMutex);
272 pthread_mutex_lock(&EventExitQueueMutex);
273 if (event_base == NULL) {
274 pthread_mutex_unlock(&EventExitQueueMutex);
277 ev_async_send (event_base, &AddJob);
278 pthread_mutex_unlock(&EventExitQueueMutex);
280 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
284 void DestructCAres(AsyncIO *IO);
285 void FreeAsyncIOContents(AsyncIO *IO)
287 CitContext *Ctx = IO->CitContext;
289 FreeStrBuf(&IO->IOBuf);
290 FreeStrBuf(&IO->SendBuf.Buf);
291 FreeStrBuf(&IO->RecvBuf.Buf);
295 FreeURL(&IO->ConnectMe);
296 FreeStrBuf(&IO->HttpReq.ReplyData);
299 Ctx->state = CON_IDLE;
305 void StopClientWatchers(AsyncIO *IO, int CloseFD)
307 ev_timer_stop (event_base, &IO->rw_timeout);
308 ev_timer_stop(event_base, &IO->conn_fail);
309 ev_idle_stop(event_base, &IO->unwind_stack);
310 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
312 ev_io_stop(event_base, &IO->conn_event);
313 ev_io_stop(event_base, &IO->send_event);
314 ev_io_stop(event_base, &IO->recv_event);
316 if (CloseFD && (IO->SendBuf.fd > 0)) {
317 close(IO->SendBuf.fd);
323 void StopCurlWatchers(AsyncIO *IO)
325 ev_timer_stop (event_base, &IO->rw_timeout);
326 ev_timer_stop(event_base, &IO->conn_fail);
327 ev_idle_stop(event_base, &IO->unwind_stack);
328 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
330 ev_io_stop(event_base, &IO->conn_event);
331 ev_io_stop(event_base, &IO->send_event);
332 ev_io_stop(event_base, &IO->recv_event);
334 if (IO->SendBuf.fd != 0) {
335 close(IO->SendBuf.fd);
341 void ShutDownCLient(AsyncIO *IO)
343 CitContext *Ctx =IO->CitContext;
346 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
348 StopClientWatchers(IO, 1);
350 if (IO->DNS.Channel != NULL) {
351 ares_destroy(IO->DNS.Channel);
352 EV_DNS_LOG_STOP(DNS.recv_event);
353 EV_DNS_LOG_STOP(DNS.send_event);
354 ev_io_stop(event_base, &IO->DNS.recv_event);
355 ev_io_stop(event_base, &IO->DNS.send_event);
356 IO->DNS.Channel = NULL;
358 assert(IO->Terminate);
362 void PostInbound(AsyncIO *IO)
364 switch (IO->NextState) {
366 ev_io_start(event_base, &IO->send_event);
370 assert(IO->SendDone);
371 IO->NextState = IO->SendDone(IO);
372 switch (IO->NextState)
381 ev_io_start(event_base, &IO->send_event);
384 StopClientWatchers(IO, 0);
392 ev_io_start(event_base, &IO->recv_event);
394 case eTerminateConnection:
408 eReadState HandleInbound(AsyncIO *IO)
410 const char *Err = NULL;
411 eReadState Finished = eBufferNotEmpty;
413 become_session(IO->CitContext);
415 while ((Finished == eBufferNotEmpty) &&
416 ((IO->NextState == eReadMessage)||
417 (IO->NextState == eReadMore)||
418 (IO->NextState == eReadFile)||
419 (IO->NextState == eReadPayload)))
422 * lex line reply in callback,
423 * or do it ourselves.
424 * i.e. as nnn-blabla means continue reading in SMTP
426 if ((IO->NextState == eReadFile) &&
427 (Finished == eBufferNotEmpty))
429 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
430 if (Finished == eReadSuccess)
432 IO->NextState = eSendReply;
435 else if (IO->LineReader)
436 Finished = IO->LineReader(IO);
438 Finished = StrBufChunkSipLine(IO->IOBuf,
442 case eMustReadMore: /// read new from socket...
444 case eBufferNotEmpty: /* shouldn't happen... */
445 case eReadSuccess: /// done for now...
447 case eReadFail: /// WHUT?
452 if (Finished != eMustReadMore) {
453 assert(IO->ReadDone);
454 ev_io_stop(event_base, &IO->recv_event);
455 IO->NextState = IO->ReadDone(IO);
456 Finished = StrBufCheckBuffer(&IO->RecvBuf);
467 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
470 AsyncIO *IO = watcher->data;
471 const char *errmsg = NULL;
473 IO->Now = ev_now(event_base);
474 become_session(IO->CitContext);
480 const char *pch = ChrPtr(IO->SendBuf.Buf);
481 const char *pchh = IO->SendBuf.ReadWritePointer;
487 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
488 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
489 ((CitContext*)(IO->CitContext))->ServiceName,
492 fd = fopen(fn, "a+");
493 fprintf(fd, "Send: BufSize: %ld BufContent: [",
495 rv = fwrite(pchh, nbytes, 1, fd);
496 if (!rv) printf("failed to write debug to %s!\n", fn);
499 switch (IO->NextState) {
501 rc = FileSendChunked(&IO->IOB, &errmsg);
503 StrBufPlain(IO->ErrMsg, errmsg, -1);
506 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
512 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
518 ev_io_stop(event_base, &IO->send_event);
519 switch (IO->NextState) {
521 assert(IO->SendDone);
522 IO->NextState = IO->SendDone(IO);
524 if ((IO->NextState == eTerminateConnection) ||
525 (IO->NextState == eAbort) )
528 ev_io_start(event_base, &IO->send_event);
532 if (IO->IOB.ChunkSendRemain > 0) {
533 ev_io_start(event_base, &IO->recv_event);
534 SetNextTimeout(IO, 100.0);
537 assert(IO->ReadDone);
538 IO->NextState = IO->ReadDone(IO);
539 switch(IO->NextState) {
548 ev_io_start(event_base,
556 case eTerminateConnection:
563 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
565 IO->NextState = eReadMore;
570 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
575 ev_io_start(event_base, &IO->recv_event);
581 * we now live in another queue,
582 * so we have to unregister.
584 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
589 case eTerminateConnection:
595 if (errno != EAGAIN) {
596 StopClientWatchers(IO, 1);
598 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
599 errno, strerror(errno), IO->SendBuf.fd);
600 StrBufPrintf(IO->ErrMsg,
601 "Socket Invalid! [%s]",
603 SetNextTimeout(IO, 0.0);
606 /* else : must write more. */
609 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
611 ev_timer_stop(event_base, &IO->conn_fail);
612 ev_timer_start(event_base, &IO->rw_timeout);
614 switch(IO->NextState) {
618 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
619 ev_io_start(event_base, &IO->recv_event);
625 become_session(IO->CitContext);
626 IO_send_callback(loop, &IO->send_event, revents);
632 case eTerminateConnection:
640 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
642 AsyncIO *IO = watcher->data;
644 IO->Now = ev_now(event_base);
645 ev_timer_stop (event_base, &IO->rw_timeout);
646 become_session(IO->CitContext);
648 if (IO->SendBuf.fd != 0)
650 ev_io_stop(event_base, &IO->send_event);
651 ev_io_stop(event_base, &IO->recv_event);
652 ev_timer_stop (event_base, &IO->rw_timeout);
653 close(IO->SendBuf.fd);
654 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
658 switch (IO->Timeout(IO))
668 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
670 AsyncIO *IO = watcher->data;
672 IO->Now = ev_now(event_base);
673 ev_timer_stop (event_base, &IO->conn_fail);
675 if (IO->SendBuf.fd != 0)
677 ev_io_stop(loop, &IO->conn_event);
678 ev_io_stop(event_base, &IO->send_event);
679 ev_io_stop(event_base, &IO->recv_event);
680 ev_timer_stop (event_base, &IO->rw_timeout);
681 close(IO->SendBuf.fd);
682 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
684 become_session(IO->CitContext);
686 assert(IO->ConnFail);
687 switch (IO->ConnFail(IO))
698 IO_connfailimmediate_callback(struct ev_loop *loop,
702 AsyncIO *IO = watcher->data;
704 IO->Now = ev_now(event_base);
705 ev_idle_stop (event_base, &IO->conn_fail_immediate);
707 if (IO->SendBuf.fd != 0)
709 close(IO->SendBuf.fd);
710 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
712 become_session(IO->CitContext);
714 assert(IO->ConnFail);
715 switch (IO->ConnFail(IO))
726 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
728 AsyncIO *IO = watcher->data;
730 socklen_t lon = sizeof(so_err);
733 IO->Now = ev_now(event_base);
734 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
736 ev_io_stop(loop, &IO->conn_event);
737 ev_timer_stop(event_base, &IO->conn_fail);
739 err = getsockopt(IO->SendBuf.fd,
745 if ((err == 0) && (so_err != 0))
747 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
750 IO_connfail_callback(loop, &IO->conn_fail, revents);
755 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
756 set_start_callback(loop, IO, revents);
761 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
765 AsyncIO *IO = watcher->data;
767 IO->Now = ev_now(event_base);
768 switch (IO->NextState) {
770 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
772 StrBufPlain(IO->ErrMsg, errmsg, -1);
775 if (IO->IOB.ChunkSendRemain == 0)
777 IO->NextState = eSendReply;
778 assert(IO->ReadDone);
779 ev_io_stop(event_base, &IO->recv_event);
788 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
800 const char *pch = ChrPtr(IO->RecvBuf.Buf);
801 const char *pchh = IO->RecvBuf.ReadWritePointer;
806 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
807 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
808 ((CitContext*)(IO->CitContext))->ServiceName,
811 fd = fopen(fn, "a+");
812 fprintf(fd, "Read: BufSize: %ld BufContent: [",
814 rv = fwrite(pchh, nbytes, 1, fd);
815 if (!rv) printf("failed to write debug to %s!\n", fn);
822 } else if (nbytes == 0) {
823 StopClientWatchers(IO, 1);
824 SetNextTimeout(IO, 0.01);
826 } else if (nbytes == -1) {
827 if (errno != EAGAIN) {
828 // FD is gone. kick it.
829 StopClientWatchers(IO, 1);
831 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
832 errno, strerror(errno), IO->SendBuf.fd);
833 StrBufPrintf(IO->ErrMsg,
834 "Socket Invalid! [%s]",
836 SetNextTimeout(IO, 0.0);
843 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
845 AsyncIO *IO = watcher->data;
846 IO->Now = ev_now(event_base);
847 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
848 become_session(IO->CitContext);
849 assert(IO->DNS.Query->PostDNS);
850 switch (IO->DNS.Query->PostDNS(IO))
853 assert(IO->DNS.Fail);
854 switch (IO->DNS.Fail(IO)) {
856 //// StopClientWatchers(IO);
867 eNextState EvConnectSock(AsyncIO *IO,
869 double first_rw_timeout,
872 struct sockaddr_in egress_sin;
876 become_session(IO->CitContext);
879 IO->NextState = eReadMessage;
882 IO->NextState = eSendReply;
885 IO->SendBuf.fd = IO->RecvBuf.fd =
887 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
891 if (IO->SendBuf.fd < 0) {
893 "EVENT: socket() failed: %s\n",
896 StrBufPrintf(IO->ErrMsg,
897 "Failed to create socket: %s",
899 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
902 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
905 "EVENT: unable to get socket flags! %s \n",
907 StrBufPrintf(IO->ErrMsg,
908 "Failed to get socket flags: %s",
910 close(IO->SendBuf.fd);
911 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
914 fdflags = fdflags | O_NONBLOCK;
915 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
918 "EVENT: unable to set socket nonblocking flags! %s \n",
920 StrBufPrintf(IO->ErrMsg,
921 "Failed to set socket flags: %s",
923 close(IO->SendBuf.fd);
924 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
927 /* TODO: maye we could use offsetof() to calc the position of data...
928 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
930 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
931 IO->recv_event.data = IO;
932 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
933 IO->send_event.data = IO;
935 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
936 IO->conn_fail.data = IO;
937 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
938 IO->rw_timeout.data = IO;
943 /* for debugging you may bypass it like this:
944 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
945 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
946 * inet_addr("127.0.0.1");
948 if (IO->ConnectMe->IPv6) {
949 rc = connect(IO->SendBuf.fd,
950 &IO->ConnectMe->Addr,
951 sizeof(struct sockaddr_in6));
954 /* If citserver is bound to a specific IP address on the host, make
955 * sure we use that address for outbound connections.
958 memset(&egress_sin, 0, sizeof(egress_sin));
959 egress_sin.sin_family = AF_INET;
960 if (!IsEmptyStr(config.c_ip_addr)) {
961 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
962 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
963 egress_sin.sin_addr.s_addr = INADDR_ANY;
966 /* If this bind fails, no problem; we can still use INADDR_ANY */
967 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
969 rc = connect(IO->SendBuf.fd,
970 (struct sockaddr_in *)&IO->ConnectMe->Addr,
971 sizeof(struct sockaddr_in));
975 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
976 set_start_callback(event_base, IO, 0);
977 return IO->NextState;
979 else if (errno == EINPROGRESS) {
980 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
982 ev_io_init(&IO->conn_event,
983 IO_connestd_callback,
987 IO->conn_event.data = IO;
989 ev_io_start(event_base, &IO->conn_event);
990 ev_timer_start(event_base, &IO->conn_fail);
991 return IO->NextState;
994 ev_idle_init(&IO->conn_fail_immediate,
995 IO_connfailimmediate_callback);
996 IO->conn_fail_immediate.data = IO;
997 ev_idle_start(event_base, &IO->conn_fail_immediate);
999 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
1000 StrBufPrintf(IO->ErrMsg,
1001 "Failed to connect: %s",
1003 return IO->NextState;
1005 return IO->NextState;
1008 void SetNextTimeout(AsyncIO *IO, double timeout)
1010 IO->rw_timeout.repeat = timeout;
1011 ev_timer_again (event_base, &IO->rw_timeout);
1015 eNextState ReAttachIO(AsyncIO *IO,
1020 become_session(IO->CitContext);
1021 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1023 IO->NextState = eReadMessage;
1026 IO->NextState = eSendReply;
1028 set_start_callback(event_base, IO, 0);
1030 return IO->NextState;
1033 void InitIOStruct(AsyncIO *IO,
1035 eNextState NextState,
1036 IO_LineReaderCallback LineReader,
1037 IO_CallBack DNS_Fail,
1038 IO_CallBack SendDone,
1039 IO_CallBack ReadDone,
1040 IO_CallBack Terminate,
1041 IO_CallBack DBTerminate,
1042 IO_CallBack ConnFail,
1043 IO_CallBack Timeout,
1044 IO_CallBack ShutdownAbort)
1048 IO->CitContext = CloneContext(CC);
1049 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1051 IO->NextState = NextState;
1053 IO->SendDone = SendDone;
1054 IO->ReadDone = ReadDone;
1055 IO->Terminate = Terminate;
1056 IO->DBTerminate = DBTerminate;
1057 IO->LineReader = LineReader;
1058 IO->ConnFail = ConnFail;
1059 IO->Timeout = Timeout;
1060 IO->ShutdownAbort = ShutdownAbort;
1062 IO->DNS.Fail = DNS_Fail;
1064 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1065 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1066 IO->IOBuf = NewStrBuf();
1067 EV_syslog(LOG_DEBUG,
1068 "EVENT: Session lives at %p IO at %p \n",
1073 extern int evcurl_init(AsyncIO *IO);
1075 int InitcURLIOStruct(AsyncIO *IO,
1078 IO_CallBack SendDone,
1079 IO_CallBack Terminate,
1080 IO_CallBack DBTerminate,
1081 IO_CallBack ShutdownAbort)
1085 IO->CitContext = CloneContext(CC);
1086 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1088 IO->SendDone = SendDone;
1089 IO->Terminate = Terminate;
1090 IO->DBTerminate = DBTerminate;
1091 IO->ShutdownAbort = ShutdownAbort;
1093 strcpy(IO->HttpReq.errdesc, Desc);
1096 return evcurl_init(IO);
1100 extern int DebugEventLoopBacktrace;
1101 void EV_backtrace(AsyncIO *IO)
1103 #ifdef HAVE_BACKTRACE
1104 void *stack_frames[50];
1108 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1110 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1111 strings = backtrace_symbols(stack_frames, size);
1112 for (i = 0; i < size; i++) {
1113 if (strings != NULL) {
1114 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1117 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1125 ev_tstamp ctdl_ev_now (void)
1127 return ev_now(event_base);