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 ev_io_start(event_base, &IO->send_event);
377 ev_io_start(event_base, &IO->recv_event);
379 case eTerminateConnection:
393 eReadState HandleInbound(AsyncIO *IO)
395 const char *Err = NULL;
396 eReadState Finished = eBufferNotEmpty;
398 become_session(IO->CitContext);
400 while ((Finished == eBufferNotEmpty) &&
401 ((IO->NextState == eReadMessage)||
402 (IO->NextState == eReadMore)||
403 (IO->NextState == eReadFile)||
404 (IO->NextState == eReadPayload)))
407 * lex line reply in callback,
408 * or do it ourselves.
409 * i.e. as nnn-blabla means continue reading in SMTP
411 if ((IO->NextState == eReadFile) &&
412 (Finished == eBufferNotEmpty))
414 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
415 if (Finished == eReadSuccess)
417 IO->NextState = eSendReply;
420 else if (IO->LineReader)
421 Finished = IO->LineReader(IO);
423 Finished = StrBufChunkSipLine(IO->IOBuf,
427 case eMustReadMore: /// read new from socket...
429 case eBufferNotEmpty: /* shouldn't happen... */
430 case eReadSuccess: /// done for now...
432 case eReadFail: /// WHUT?
437 if (Finished != eMustReadMore) {
438 assert(IO->ReadDone);
439 ev_io_stop(event_base, &IO->recv_event);
440 IO->NextState = IO->ReadDone(IO);
441 Finished = StrBufCheckBuffer(&IO->RecvBuf);
452 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
455 AsyncIO *IO = watcher->data;
456 const char *errmsg = NULL;
458 IO->Now = ev_now(event_base);
459 become_session(IO->CitContext);
465 const char *pch = ChrPtr(IO->SendBuf.Buf);
466 const char *pchh = IO->SendBuf.ReadWritePointer;
472 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
473 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
474 ((CitContext*)(IO->CitContext))->ServiceName,
477 fd = fopen(fn, "a+");
478 fprintf(fd, "Send: BufSize: %ld BufContent: [",
480 rv = fwrite(pchh, nbytes, 1, fd);
481 if (!rv) printf("failed to write debug to %s!\n", fn);
484 switch (IO->NextState) {
486 rc = FileSendChunked(&IO->IOB, &errmsg);
488 StrBufPlain(IO->ErrMsg, errmsg, -1);
491 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
497 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
503 ev_io_stop(event_base, &IO->send_event);
504 switch (IO->NextState) {
506 assert(IO->SendDone);
507 IO->NextState = IO->SendDone(IO);
509 if ((IO->NextState == eTerminateConnection) ||
510 (IO->NextState == eAbort) )
513 ev_io_start(event_base, &IO->send_event);
517 if (IO->IOB.ChunkSendRemain > 0) {
518 ev_io_start(event_base, &IO->recv_event);
519 SetNextTimeout(IO, 100.0);
522 assert(IO->ReadDone);
523 IO->NextState = IO->ReadDone(IO);
524 switch(IO->NextState) {
533 ev_io_start(event_base,
541 case eTerminateConnection:
548 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
550 IO->NextState = eReadMore;
555 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
560 ev_io_start(event_base, &IO->recv_event);
566 * we now live in another queue,
567 * so we have to unregister.
569 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
574 case eTerminateConnection:
580 if (errno != EAGAIN) {
581 StopClientWatchers(IO, 1);
583 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
584 errno, strerror(errno), IO->SendBuf.fd);
585 StrBufPrintf(IO->ErrMsg,
586 "Socket Invalid! [%s]",
588 SetNextTimeout(IO, 0.0);
591 /* else : must write more. */
594 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
596 ev_timer_stop(event_base, &IO->conn_fail);
597 ev_timer_start(event_base, &IO->rw_timeout);
599 switch(IO->NextState) {
603 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
604 ev_io_start(event_base, &IO->recv_event);
610 become_session(IO->CitContext);
611 IO_send_callback(loop, &IO->send_event, revents);
617 case eTerminateConnection:
625 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
627 AsyncIO *IO = watcher->data;
629 IO->Now = ev_now(event_base);
630 ev_timer_stop (event_base, &IO->rw_timeout);
631 become_session(IO->CitContext);
633 if (IO->SendBuf.fd != 0)
635 ev_io_stop(event_base, &IO->send_event);
636 ev_io_stop(event_base, &IO->recv_event);
637 ev_timer_stop (event_base, &IO->rw_timeout);
638 close(IO->SendBuf.fd);
639 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
643 switch (IO->Timeout(IO))
653 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
655 AsyncIO *IO = watcher->data;
657 IO->Now = ev_now(event_base);
658 ev_timer_stop (event_base, &IO->conn_fail);
660 if (IO->SendBuf.fd != 0)
662 ev_io_stop(loop, &IO->conn_event);
663 ev_io_stop(event_base, &IO->send_event);
664 ev_io_stop(event_base, &IO->recv_event);
665 ev_timer_stop (event_base, &IO->rw_timeout);
666 close(IO->SendBuf.fd);
667 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
669 become_session(IO->CitContext);
671 assert(IO->ConnFail);
672 switch (IO->ConnFail(IO))
683 IO_connfailimmediate_callback(struct ev_loop *loop,
687 AsyncIO *IO = watcher->data;
689 IO->Now = ev_now(event_base);
690 ev_idle_stop (event_base, &IO->conn_fail_immediate);
692 if (IO->SendBuf.fd != 0)
694 close(IO->SendBuf.fd);
695 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
697 become_session(IO->CitContext);
699 assert(IO->ConnFail);
700 switch (IO->ConnFail(IO))
711 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
713 AsyncIO *IO = watcher->data;
715 socklen_t lon = sizeof(so_err);
718 IO->Now = ev_now(event_base);
719 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
721 ev_io_stop(loop, &IO->conn_event);
722 ev_timer_stop(event_base, &IO->conn_fail);
724 err = getsockopt(IO->SendBuf.fd,
730 if ((err == 0) && (so_err != 0))
732 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
735 IO_connfail_callback(loop, &IO->conn_fail, revents);
740 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
741 set_start_callback(loop, IO, revents);
746 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
750 AsyncIO *IO = watcher->data;
752 IO->Now = ev_now(event_base);
753 switch (IO->NextState) {
755 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
757 StrBufPlain(IO->ErrMsg, errmsg, -1);
760 if (IO->IOB.ChunkSendRemain == 0)
762 IO->NextState = eSendReply;
763 assert(IO->ReadDone);
764 ev_io_stop(event_base, &IO->recv_event);
773 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
785 const char *pch = ChrPtr(IO->RecvBuf.Buf);
786 const char *pchh = IO->RecvBuf.ReadWritePointer;
791 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
792 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
793 ((CitContext*)(IO->CitContext))->ServiceName,
796 fd = fopen(fn, "a+");
797 fprintf(fd, "Read: BufSize: %ld BufContent: [",
799 rv = fwrite(pchh, nbytes, 1, fd);
800 if (!rv) printf("failed to write debug to %s!\n", fn);
807 } else if (nbytes == 0) {
808 StopClientWatchers(IO, 1);
809 SetNextTimeout(IO, 0.01);
811 } else if (nbytes == -1) {
812 if (errno != EAGAIN) {
813 // FD is gone. kick it.
814 StopClientWatchers(IO, 1);
816 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
817 errno, strerror(errno), IO->SendBuf.fd);
818 StrBufPrintf(IO->ErrMsg,
819 "Socket Invalid! [%s]",
821 SetNextTimeout(IO, 0.0);
828 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
830 AsyncIO *IO = watcher->data;
831 IO->Now = ev_now(event_base);
832 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
833 become_session(IO->CitContext);
834 assert(IO->DNS.Query->PostDNS);
835 switch (IO->DNS.Query->PostDNS(IO))
838 assert(IO->DNS.Fail);
839 switch (IO->DNS.Fail(IO)) {
841 //// StopClientWatchers(IO);
852 eNextState EvConnectSock(AsyncIO *IO,
854 double first_rw_timeout,
857 struct sockaddr_in egress_sin;
861 become_session(IO->CitContext);
864 IO->NextState = eReadMessage;
867 IO->NextState = eSendReply;
870 IO->SendBuf.fd = IO->RecvBuf.fd =
872 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
876 if (IO->SendBuf.fd < 0) {
878 "EVENT: socket() failed: %s\n",
881 StrBufPrintf(IO->ErrMsg,
882 "Failed to create socket: %s",
884 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
887 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
890 "EVENT: unable to get socket flags! %s \n",
892 StrBufPrintf(IO->ErrMsg,
893 "Failed to get socket flags: %s",
895 close(IO->SendBuf.fd);
896 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
899 fdflags = fdflags | O_NONBLOCK;
900 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
903 "EVENT: unable to set socket nonblocking flags! %s \n",
905 StrBufPrintf(IO->ErrMsg,
906 "Failed to set socket flags: %s",
908 close(IO->SendBuf.fd);
909 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
912 /* TODO: maye we could use offsetof() to calc the position of data...
913 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
915 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
916 IO->recv_event.data = IO;
917 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
918 IO->send_event.data = IO;
920 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
921 IO->conn_fail.data = IO;
922 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
923 IO->rw_timeout.data = IO;
928 /* for debugging you may bypass it like this:
929 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
930 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
931 * inet_addr("127.0.0.1");
933 if (IO->ConnectMe->IPv6) {
934 rc = connect(IO->SendBuf.fd,
935 &IO->ConnectMe->Addr,
936 sizeof(struct sockaddr_in6));
939 /* If citserver is bound to a specific IP address on the host, make
940 * sure we use that address for outbound connections.
943 memset(&egress_sin, 0, sizeof(egress_sin));
944 egress_sin.sin_family = AF_INET;
945 if (!IsEmptyStr(config.c_ip_addr)) {
946 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
947 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
948 egress_sin.sin_addr.s_addr = INADDR_ANY;
951 /* If this bind fails, no problem; we can still use INADDR_ANY */
952 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
954 rc = connect(IO->SendBuf.fd,
955 (struct sockaddr_in *)&IO->ConnectMe->Addr,
956 sizeof(struct sockaddr_in));
960 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
961 set_start_callback(event_base, IO, 0);
962 return IO->NextState;
964 else if (errno == EINPROGRESS) {
965 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
967 ev_io_init(&IO->conn_event,
968 IO_connestd_callback,
972 IO->conn_event.data = IO;
974 ev_io_start(event_base, &IO->conn_event);
975 ev_timer_start(event_base, &IO->conn_fail);
976 return IO->NextState;
979 ev_idle_init(&IO->conn_fail_immediate,
980 IO_connfailimmediate_callback);
981 IO->conn_fail_immediate.data = IO;
982 ev_idle_start(event_base, &IO->conn_fail_immediate);
984 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
985 StrBufPrintf(IO->ErrMsg,
986 "Failed to connect: %s",
988 return IO->NextState;
990 return IO->NextState;
993 void SetNextTimeout(AsyncIO *IO, double timeout)
995 IO->rw_timeout.repeat = timeout;
996 ev_timer_again (event_base, &IO->rw_timeout);
1000 eNextState ReAttachIO(AsyncIO *IO,
1005 become_session(IO->CitContext);
1006 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1008 IO->NextState = eReadMessage;
1011 IO->NextState = eSendReply;
1013 set_start_callback(event_base, IO, 0);
1015 return IO->NextState;
1018 void InitIOStruct(AsyncIO *IO,
1020 eNextState NextState,
1021 IO_LineReaderCallback LineReader,
1022 IO_CallBack DNS_Fail,
1023 IO_CallBack SendDone,
1024 IO_CallBack ReadDone,
1025 IO_CallBack Terminate,
1026 IO_CallBack DBTerminate,
1027 IO_CallBack ConnFail,
1028 IO_CallBack Timeout,
1029 IO_CallBack ShutdownAbort)
1033 IO->CitContext = CloneContext(CC);
1034 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1036 IO->NextState = NextState;
1038 IO->SendDone = SendDone;
1039 IO->ReadDone = ReadDone;
1040 IO->Terminate = Terminate;
1041 IO->DBTerminate = DBTerminate;
1042 IO->LineReader = LineReader;
1043 IO->ConnFail = ConnFail;
1044 IO->Timeout = Timeout;
1045 IO->ShutdownAbort = ShutdownAbort;
1047 IO->DNS.Fail = DNS_Fail;
1049 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1050 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1051 IO->IOBuf = NewStrBuf();
1052 EV_syslog(LOG_DEBUG,
1053 "EVENT: Session lives at %p IO at %p \n",
1058 extern int evcurl_init(AsyncIO *IO);
1060 int InitcURLIOStruct(AsyncIO *IO,
1063 IO_CallBack SendDone,
1064 IO_CallBack Terminate,
1065 IO_CallBack DBTerminate,
1066 IO_CallBack ShutdownAbort)
1070 IO->CitContext = CloneContext(CC);
1071 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1073 IO->SendDone = SendDone;
1074 IO->Terminate = Terminate;
1075 IO->DBTerminate = DBTerminate;
1076 IO->ShutdownAbort = ShutdownAbort;
1078 strcpy(IO->HttpReq.errdesc, Desc);
1081 return evcurl_init(IO);
1085 extern int DebugEventLoopBacktrace;
1086 void EV_backtrace(AsyncIO *IO)
1088 #ifdef HAVE_BACKTRACE
1089 void *stack_frames[50];
1093 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1095 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1096 strings = backtrace_symbols(stack_frames, size);
1097 for (i = 0; i < size; i++) {
1098 if (strings != NULL) {
1099 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1102 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1110 ev_tstamp ctdl_ev_now (void)
1112 return ev_now(event_base);