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;
301 IO->CitContext = NULL;
306 void StopClientWatchers(AsyncIO *IO, int CloseFD)
308 ev_timer_stop (event_base, &IO->rw_timeout);
309 ev_timer_stop(event_base, &IO->conn_fail);
310 ev_idle_stop(event_base, &IO->unwind_stack);
311 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
313 ev_io_stop(event_base, &IO->conn_event);
314 ev_io_stop(event_base, &IO->send_event);
315 ev_io_stop(event_base, &IO->recv_event);
317 if (CloseFD && (IO->SendBuf.fd > 0)) {
318 close(IO->SendBuf.fd);
324 void StopCurlWatchers(AsyncIO *IO)
326 ev_timer_stop (event_base, &IO->rw_timeout);
327 ev_timer_stop(event_base, &IO->conn_fail);
328 ev_idle_stop(event_base, &IO->unwind_stack);
329 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
331 ev_io_stop(event_base, &IO->conn_event);
332 ev_io_stop(event_base, &IO->send_event);
333 ev_io_stop(event_base, &IO->recv_event);
335 if (IO->SendBuf.fd != 0) {
336 close(IO->SendBuf.fd);
342 void ShutDownCLient(AsyncIO *IO)
344 CitContext *Ctx =IO->CitContext;
347 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
349 StopClientWatchers(IO, 1);
351 if (IO->DNS.Channel != NULL) {
352 ares_destroy(IO->DNS.Channel);
353 EV_DNS_LOG_STOP(DNS.recv_event);
354 EV_DNS_LOG_STOP(DNS.send_event);
355 ev_io_stop(event_base, &IO->DNS.recv_event);
356 ev_io_stop(event_base, &IO->DNS.send_event);
357 IO->DNS.Channel = NULL;
359 assert(IO->Terminate);
363 void PostInbound(AsyncIO *IO)
365 switch (IO->NextState) {
367 ev_io_start(event_base, &IO->send_event);
371 assert(IO->SendDone);
372 IO->NextState = IO->SendDone(IO);
373 ev_io_start(event_base, &IO->send_event);
378 ev_io_start(event_base, &IO->recv_event);
380 case eTerminateConnection:
394 eReadState HandleInbound(AsyncIO *IO)
396 const char *Err = NULL;
397 eReadState Finished = eBufferNotEmpty;
399 become_session(IO->CitContext);
401 while ((Finished == eBufferNotEmpty) &&
402 ((IO->NextState == eReadMessage)||
403 (IO->NextState == eReadMore)||
404 (IO->NextState == eReadFile)||
405 (IO->NextState == eReadPayload)))
408 * lex line reply in callback,
409 * or do it ourselves.
410 * i.e. as nnn-blabla means continue reading in SMTP
412 if ((IO->NextState == eReadFile) &&
413 (Finished == eBufferNotEmpty))
415 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
416 if (Finished == eReadSuccess)
418 IO->NextState = eSendReply;
421 else if (IO->LineReader)
422 Finished = IO->LineReader(IO);
424 Finished = StrBufChunkSipLine(IO->IOBuf,
428 case eMustReadMore: /// read new from socket...
430 case eBufferNotEmpty: /* shouldn't happen... */
431 case eReadSuccess: /// done for now...
433 case eReadFail: /// WHUT?
438 if (Finished != eMustReadMore) {
439 assert(IO->ReadDone);
440 ev_io_stop(event_base, &IO->recv_event);
441 IO->NextState = IO->ReadDone(IO);
442 Finished = StrBufCheckBuffer(&IO->RecvBuf);
453 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
456 AsyncIO *IO = watcher->data;
457 const char *errmsg = NULL;
459 IO->Now = ev_now(event_base);
460 become_session(IO->CitContext);
466 const char *pch = ChrPtr(IO->SendBuf.Buf);
467 const char *pchh = IO->SendBuf.ReadWritePointer;
473 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
474 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
475 ((CitContext*)(IO->CitContext))->ServiceName,
478 fd = fopen(fn, "a+");
479 fprintf(fd, "Send: BufSize: %ld BufContent: [",
481 rv = fwrite(pchh, nbytes, 1, fd);
482 if (!rv) printf("failed to write debug to %s!\n", fn);
485 switch (IO->NextState) {
487 rc = FileSendChunked(&IO->IOB, &errmsg);
489 StrBufPlain(IO->ErrMsg, errmsg, -1);
492 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
498 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
504 ev_io_stop(event_base, &IO->send_event);
505 switch (IO->NextState) {
507 assert(IO->SendDone);
508 IO->NextState = IO->SendDone(IO);
510 if ((IO->NextState == eTerminateConnection) ||
511 (IO->NextState == eAbort) )
514 ev_io_start(event_base, &IO->send_event);
518 if (IO->IOB.ChunkSendRemain > 0) {
519 ev_io_start(event_base, &IO->recv_event);
520 SetNextTimeout(IO, 100.0);
523 assert(IO->ReadDone);
524 IO->NextState = IO->ReadDone(IO);
525 switch(IO->NextState) {
534 ev_io_start(event_base,
542 case eTerminateConnection:
549 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
551 IO->NextState = eReadMore;
556 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
561 ev_io_start(event_base, &IO->recv_event);
567 * we now live in another queue,
568 * so we have to unregister.
570 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
575 case eTerminateConnection:
581 if (errno != EAGAIN) {
582 StopClientWatchers(IO, 1);
584 "IO_send_callback(): Socket Invalid! [%d] [%s] [%d]\n",
585 errno, strerror(errno), IO->SendBuf.fd);
586 StrBufPrintf(IO->ErrMsg,
587 "Socket Invalid! [%s]",
589 SetNextTimeout(IO, 0.01);
592 /* else : must write more. */
595 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
597 ev_timer_stop(event_base, &IO->conn_fail);
598 ev_timer_start(event_base, &IO->rw_timeout);
600 switch(IO->NextState) {
604 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
605 ev_io_start(event_base, &IO->recv_event);
611 become_session(IO->CitContext);
612 IO_send_callback(loop, &IO->send_event, revents);
618 case eTerminateConnection:
626 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
628 AsyncIO *IO = watcher->data;
630 IO->Now = ev_now(event_base);
631 ev_timer_stop (event_base, &IO->rw_timeout);
632 become_session(IO->CitContext);
634 if (IO->SendBuf.fd != 0)
636 ev_io_stop(event_base, &IO->send_event);
637 ev_io_stop(event_base, &IO->recv_event);
638 ev_timer_stop (event_base, &IO->rw_timeout);
639 close(IO->SendBuf.fd);
640 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
644 switch (IO->Timeout(IO))
654 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
656 AsyncIO *IO = watcher->data;
658 IO->Now = ev_now(event_base);
659 ev_timer_stop (event_base, &IO->conn_fail);
661 if (IO->SendBuf.fd != 0)
663 ev_io_stop(loop, &IO->conn_event);
664 ev_io_stop(event_base, &IO->send_event);
665 ev_io_stop(event_base, &IO->recv_event);
666 ev_timer_stop (event_base, &IO->rw_timeout);
667 close(IO->SendBuf.fd);
668 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
670 become_session(IO->CitContext);
672 assert(IO->ConnFail);
673 switch (IO->ConnFail(IO))
684 IO_connfailimmediate_callback(struct ev_loop *loop,
688 AsyncIO *IO = watcher->data;
690 IO->Now = ev_now(event_base);
691 ev_idle_stop (event_base, &IO->conn_fail_immediate);
693 if (IO->SendBuf.fd != 0)
695 close(IO->SendBuf.fd);
696 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
698 become_session(IO->CitContext);
700 assert(IO->ConnFail);
701 switch (IO->ConnFail(IO))
712 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
714 AsyncIO *IO = watcher->data;
716 socklen_t lon = sizeof(so_err);
719 IO->Now = ev_now(event_base);
720 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
722 ev_io_stop(loop, &IO->conn_event);
723 ev_timer_stop(event_base, &IO->conn_fail);
725 err = getsockopt(IO->SendBuf.fd,
731 if ((err == 0) && (so_err != 0))
733 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
736 IO_connfail_callback(loop, &IO->conn_fail, revents);
741 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
742 set_start_callback(loop, IO, revents);
747 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
751 AsyncIO *IO = watcher->data;
753 IO->Now = ev_now(event_base);
754 switch (IO->NextState) {
756 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
758 StrBufPlain(IO->ErrMsg, errmsg, -1);
761 if (IO->IOB.ChunkSendRemain == 0)
763 IO->NextState = eSendReply;
764 assert(IO->ReadDone);
765 ev_io_stop(event_base, &IO->recv_event);
774 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
786 const char *pch = ChrPtr(IO->RecvBuf.Buf);
787 const char *pchh = IO->RecvBuf.ReadWritePointer;
792 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
793 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
794 ((CitContext*)(IO->CitContext))->ServiceName,
797 fd = fopen(fn, "a+");
798 fprintf(fd, "Read: BufSize: %ld BufContent: [",
800 rv = fwrite(pchh, nbytes, 1, fd);
801 if (!rv) printf("failed to write debug to %s!\n", fn);
808 } else if (nbytes == 0) {
809 StopClientWatchers(IO, 1);
810 SetNextTimeout(IO, 0.01);
812 } else if (nbytes == -1) {
813 if (errno != EAGAIN) {
814 // FD is gone. kick it.
815 StopClientWatchers(IO, 1);
817 "IO_recv_callback(): Socket Invalid! [%d] [%s] [%d]\n",
818 errno, strerror(errno), IO->SendBuf.fd);
819 StrBufPrintf(IO->ErrMsg,
820 "Socket Invalid! [%s]",
822 SetNextTimeout(IO, 0.01);
829 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
831 AsyncIO *IO = watcher->data;
832 IO->Now = ev_now(event_base);
833 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
834 become_session(IO->CitContext);
835 assert(IO->DNS.Query->PostDNS);
836 switch (IO->DNS.Query->PostDNS(IO))
839 assert(IO->DNS.Fail);
840 switch (IO->DNS.Fail(IO)) {
842 //// StopClientWatchers(IO);
853 eNextState EvConnectSock(AsyncIO *IO,
855 double first_rw_timeout,
858 struct sockaddr_in egress_sin;
862 become_session(IO->CitContext);
865 IO->NextState = eReadMessage;
868 IO->NextState = eSendReply;
871 IO->SendBuf.fd = IO->RecvBuf.fd =
873 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
877 if (IO->SendBuf.fd < 0) {
879 "EVENT: socket() failed: %s\n",
882 StrBufPrintf(IO->ErrMsg,
883 "Failed to create socket: %s",
885 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
888 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
891 "EVENT: unable to get socket %d flags! %s \n",
894 StrBufPrintf(IO->ErrMsg,
895 "Failed to get socket %d flags: %s",
898 close(IO->SendBuf.fd);
899 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
902 fdflags = fdflags | O_NONBLOCK;
903 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
906 "EVENT: unable to set socket %d nonblocking flags! %s \n",
909 StrBufPrintf(IO->ErrMsg,
910 "Failed to set socket flags: %s",
912 close(IO->SendBuf.fd);
913 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
916 /* TODO: maye we could use offsetof() to calc the position of data...
917 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
919 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
920 IO->recv_event.data = IO;
921 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
922 IO->send_event.data = IO;
924 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
925 IO->conn_fail.data = IO;
926 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
927 IO->rw_timeout.data = IO;
932 /* for debugging you may bypass it like this:
933 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
934 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
935 * inet_addr("127.0.0.1");
937 if (IO->ConnectMe->IPv6) {
938 rc = connect(IO->SendBuf.fd,
939 &IO->ConnectMe->Addr,
940 sizeof(struct sockaddr_in6));
943 /* If citserver is bound to a specific IP address on the host, make
944 * sure we use that address for outbound connections.
947 memset(&egress_sin, 0, sizeof(egress_sin));
948 egress_sin.sin_family = AF_INET;
949 if (!IsEmptyStr(config.c_ip_addr)) {
950 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
951 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
952 egress_sin.sin_addr.s_addr = INADDR_ANY;
955 /* If this bind fails, no problem; we can still use INADDR_ANY */
956 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
958 rc = connect(IO->SendBuf.fd,
959 (struct sockaddr_in *)&IO->ConnectMe->Addr,
960 sizeof(struct sockaddr_in));
964 EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd);
965 set_start_callback(event_base, IO, 0);
966 return IO->NextState;
968 else if (errno == EINPROGRESS) {
969 EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd);
971 ev_io_init(&IO->conn_event,
972 IO_connestd_callback,
976 IO->conn_event.data = IO;
978 ev_io_start(event_base, &IO->conn_event);
979 ev_timer_start(event_base, &IO->conn_fail);
980 return IO->NextState;
983 ev_idle_init(&IO->conn_fail_immediate,
984 IO_connfailimmediate_callback);
985 IO->conn_fail_immediate.data = IO;
986 ev_idle_start(event_base, &IO->conn_fail_immediate);
989 "connect() = %d failed: %s\n",
993 StrBufPrintf(IO->ErrMsg,
994 "Failed to connect: %s",
996 return IO->NextState;
998 return IO->NextState;
1001 void SetNextTimeout(AsyncIO *IO, double timeout)
1003 IO->rw_timeout.repeat = timeout;
1004 ev_timer_again (event_base, &IO->rw_timeout);
1008 eNextState ReAttachIO(AsyncIO *IO,
1013 become_session(IO->CitContext);
1014 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1016 IO->NextState = eReadMessage;
1019 IO->NextState = eSendReply;
1021 set_start_callback(event_base, IO, 0);
1023 return IO->NextState;
1026 void InitIOStruct(AsyncIO *IO,
1028 eNextState NextState,
1029 IO_LineReaderCallback LineReader,
1030 IO_CallBack DNS_Fail,
1031 IO_CallBack SendDone,
1032 IO_CallBack ReadDone,
1033 IO_CallBack Terminate,
1034 IO_CallBack DBTerminate,
1035 IO_CallBack ConnFail,
1036 IO_CallBack Timeout,
1037 IO_CallBack ShutdownAbort)
1041 IO->CitContext = CloneContext(CC);
1042 IO->CitContext->session_specific_data = (char*) Data;
1043 IO->CitContext->IO = IO;
1045 IO->NextState = NextState;
1047 IO->SendDone = SendDone;
1048 IO->ReadDone = ReadDone;
1049 IO->Terminate = Terminate;
1050 IO->DBTerminate = DBTerminate;
1051 IO->LineReader = LineReader;
1052 IO->ConnFail = ConnFail;
1053 IO->Timeout = Timeout;
1054 IO->ShutdownAbort = ShutdownAbort;
1056 IO->DNS.Fail = DNS_Fail;
1058 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1059 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1060 IO->IOBuf = NewStrBuf();
1061 EV_syslog(LOG_DEBUG,
1062 "EVENT: Session lives at %p IO at %p \n",
1067 extern int evcurl_init(AsyncIO *IO);
1069 int InitcURLIOStruct(AsyncIO *IO,
1072 IO_CallBack SendDone,
1073 IO_CallBack Terminate,
1074 IO_CallBack DBTerminate,
1075 IO_CallBack ShutdownAbort)
1079 IO->CitContext = CloneContext(CC);
1080 IO->CitContext->session_specific_data = (char*) Data;
1081 IO->CitContext->IO = IO;
1083 IO->SendDone = SendDone;
1084 IO->Terminate = Terminate;
1085 IO->DBTerminate = DBTerminate;
1086 IO->ShutdownAbort = ShutdownAbort;
1088 strcpy(IO->HttpReq.errdesc, Desc);
1091 return evcurl_init(IO);
1096 typedef struct KillOtherSessionContext {
1099 }KillOtherSessionContext;
1101 eNextState KillTerminate(AsyncIO *IO)
1103 KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
1104 EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
1105 FreeAsyncIOContents(IO);
1106 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1112 eNextState KillShutdown(AsyncIO *IO)
1114 return eTerminateConnection;
1117 eNextState KillOtherContextNow(AsyncIO *IO)
1119 KillOtherSessionContext *Ctx = IO->Data;
1121 if (Ctx->OtherOne->ShutdownAbort != NULL)
1122 Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
1123 return eTerminateConnection;
1126 void KillAsyncIOContext(AsyncIO *IO)
1128 KillOtherSessionContext *Ctx;
1130 Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
1131 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1133 InitIOStruct(&Ctx->IO,
1148 switch(IO->NextState) {
1161 QueueEventContext(&Ctx->IO, KillOtherContextNow);
1164 QueueDBOperation(&Ctx->IO, KillOtherContextNow);
1166 case eTerminateConnection:
1168 /*hm, its already dying, dunno which Queue its in... */
1174 extern int DebugEventLoopBacktrace;
1175 void EV_backtrace(AsyncIO *IO)
1177 #ifdef HAVE_BACKTRACE
1178 void *stack_frames[50];
1182 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1184 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1185 strings = backtrace_symbols(stack_frames, size);
1186 for (i = 0; i < size; i++) {
1187 if (strings != NULL) {
1188 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1191 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1199 ev_tstamp ctdl_ev_now (void)
1201 return ev_now(event_base);