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 switch (IO->NextState)
382 ev_io_start(event_base, &IO->send_event);
385 StopClientWatchers(IO, 0);
393 ev_io_start(event_base, &IO->recv_event);
395 case eTerminateConnection:
409 eReadState HandleInbound(AsyncIO *IO)
411 const char *Err = NULL;
412 eReadState Finished = eBufferNotEmpty;
414 become_session(IO->CitContext);
416 while ((Finished == eBufferNotEmpty) &&
417 ((IO->NextState == eReadMessage)||
418 (IO->NextState == eReadMore)||
419 (IO->NextState == eReadFile)||
420 (IO->NextState == eReadPayload)))
423 * lex line reply in callback,
424 * or do it ourselves.
425 * i.e. as nnn-blabla means continue reading in SMTP
427 if ((IO->NextState == eReadFile) &&
428 (Finished == eBufferNotEmpty))
430 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
431 if (Finished == eReadSuccess)
433 IO->NextState = eSendReply;
436 else if (IO->LineReader)
437 Finished = IO->LineReader(IO);
439 Finished = StrBufChunkSipLine(IO->IOBuf,
443 case eMustReadMore: /// read new from socket...
445 case eBufferNotEmpty: /* shouldn't happen... */
446 case eReadSuccess: /// done for now...
448 case eReadFail: /// WHUT?
453 if (Finished != eMustReadMore) {
454 assert(IO->ReadDone);
455 ev_io_stop(event_base, &IO->recv_event);
456 IO->NextState = IO->ReadDone(IO);
457 Finished = StrBufCheckBuffer(&IO->RecvBuf);
468 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
471 AsyncIO *IO = watcher->data;
472 const char *errmsg = NULL;
474 IO->Now = ev_now(event_base);
475 become_session(IO->CitContext);
481 const char *pch = ChrPtr(IO->SendBuf.Buf);
482 const char *pchh = IO->SendBuf.ReadWritePointer;
488 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
489 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
490 ((CitContext*)(IO->CitContext))->ServiceName,
493 fd = fopen(fn, "a+");
494 fprintf(fd, "Send: BufSize: %ld BufContent: [",
496 rv = fwrite(pchh, nbytes, 1, fd);
497 if (!rv) printf("failed to write debug to %s!\n", fn);
500 switch (IO->NextState) {
502 rc = FileSendChunked(&IO->IOB, &errmsg);
504 StrBufPlain(IO->ErrMsg, errmsg, -1);
507 rc = StrBuf_write_one_chunk_callback(IO->SendBuf.fd,
513 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
519 ev_io_stop(event_base, &IO->send_event);
520 switch (IO->NextState) {
522 assert(IO->SendDone);
523 IO->NextState = IO->SendDone(IO);
525 if ((IO->NextState == eTerminateConnection) ||
526 (IO->NextState == eAbort) )
529 ev_io_start(event_base, &IO->send_event);
533 if (IO->IOB.ChunkSendRemain > 0) {
534 ev_io_start(event_base, &IO->recv_event);
535 SetNextTimeout(IO, 100.0);
538 assert(IO->ReadDone);
539 IO->NextState = IO->ReadDone(IO);
540 switch(IO->NextState) {
549 ev_io_start(event_base,
557 case eTerminateConnection:
564 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
566 IO->NextState = eReadMore;
571 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
576 ev_io_start(event_base, &IO->recv_event);
582 * we now live in another queue,
583 * so we have to unregister.
585 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
590 case eTerminateConnection:
596 if (errno != EAGAIN) {
597 StopClientWatchers(IO, 1);
599 "IO_send_callback(): Socket Invalid! [%d] [%s] [%d]\n",
600 errno, strerror(errno), IO->SendBuf.fd);
601 StrBufPrintf(IO->ErrMsg,
602 "Socket Invalid! [%s]",
604 SetNextTimeout(IO, 0.01);
607 /* else : must write more. */
610 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
612 ev_timer_stop(event_base, &IO->conn_fail);
613 ev_timer_start(event_base, &IO->rw_timeout);
615 switch(IO->NextState) {
619 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
620 ev_io_start(event_base, &IO->recv_event);
626 become_session(IO->CitContext);
627 IO_send_callback(loop, &IO->send_event, revents);
633 case eTerminateConnection:
641 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
643 AsyncIO *IO = watcher->data;
645 IO->Now = ev_now(event_base);
646 ev_timer_stop (event_base, &IO->rw_timeout);
647 become_session(IO->CitContext);
649 if (IO->SendBuf.fd != 0)
651 ev_io_stop(event_base, &IO->send_event);
652 ev_io_stop(event_base, &IO->recv_event);
653 ev_timer_stop (event_base, &IO->rw_timeout);
654 close(IO->SendBuf.fd);
655 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
659 switch (IO->Timeout(IO))
669 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
671 AsyncIO *IO = watcher->data;
673 IO->Now = ev_now(event_base);
674 ev_timer_stop (event_base, &IO->conn_fail);
676 if (IO->SendBuf.fd != 0)
678 ev_io_stop(loop, &IO->conn_event);
679 ev_io_stop(event_base, &IO->send_event);
680 ev_io_stop(event_base, &IO->recv_event);
681 ev_timer_stop (event_base, &IO->rw_timeout);
682 close(IO->SendBuf.fd);
683 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
685 become_session(IO->CitContext);
687 assert(IO->ConnFail);
688 switch (IO->ConnFail(IO))
699 IO_connfailimmediate_callback(struct ev_loop *loop,
703 AsyncIO *IO = watcher->data;
705 IO->Now = ev_now(event_base);
706 ev_idle_stop (event_base, &IO->conn_fail_immediate);
708 if (IO->SendBuf.fd != 0)
710 close(IO->SendBuf.fd);
711 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
713 become_session(IO->CitContext);
715 assert(IO->ConnFail);
716 switch (IO->ConnFail(IO))
727 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
729 AsyncIO *IO = watcher->data;
731 socklen_t lon = sizeof(so_err);
734 IO->Now = ev_now(event_base);
735 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
737 ev_io_stop(loop, &IO->conn_event);
738 ev_timer_stop(event_base, &IO->conn_fail);
740 err = getsockopt(IO->SendBuf.fd,
746 if ((err == 0) && (so_err != 0))
748 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
751 IO_connfail_callback(loop, &IO->conn_fail, revents);
756 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
757 set_start_callback(loop, IO, revents);
762 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
766 AsyncIO *IO = watcher->data;
768 IO->Now = ev_now(event_base);
769 switch (IO->NextState) {
771 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
773 StrBufPlain(IO->ErrMsg, errmsg, -1);
776 if (IO->IOB.ChunkSendRemain == 0)
778 IO->NextState = eSendReply;
779 assert(IO->ReadDone);
780 ev_io_stop(event_base, &IO->recv_event);
789 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
801 const char *pch = ChrPtr(IO->RecvBuf.Buf);
802 const char *pchh = IO->RecvBuf.ReadWritePointer;
807 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
808 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
809 ((CitContext*)(IO->CitContext))->ServiceName,
812 fd = fopen(fn, "a+");
813 fprintf(fd, "Read: BufSize: %ld BufContent: [",
815 rv = fwrite(pchh, nbytes, 1, fd);
816 if (!rv) printf("failed to write debug to %s!\n", fn);
823 } else if (nbytes == 0) {
824 StopClientWatchers(IO, 1);
825 SetNextTimeout(IO, 0.01);
827 } else if (nbytes == -1) {
828 if (errno != EAGAIN) {
829 // FD is gone. kick it.
830 StopClientWatchers(IO, 1);
832 "IO_recv_callback(): Socket Invalid! [%d] [%s] [%d]\n",
833 errno, strerror(errno), IO->SendBuf.fd);
834 StrBufPrintf(IO->ErrMsg,
835 "Socket Invalid! [%s]",
837 SetNextTimeout(IO, 0.01);
844 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
846 AsyncIO *IO = watcher->data;
847 IO->Now = ev_now(event_base);
848 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
849 become_session(IO->CitContext);
850 assert(IO->DNS.Query->PostDNS);
851 switch (IO->DNS.Query->PostDNS(IO))
854 assert(IO->DNS.Fail);
855 switch (IO->DNS.Fail(IO)) {
857 //// StopClientWatchers(IO);
868 eNextState EvConnectSock(AsyncIO *IO,
870 double first_rw_timeout,
873 struct sockaddr_in egress_sin;
877 become_session(IO->CitContext);
880 IO->NextState = eReadMessage;
883 IO->NextState = eSendReply;
886 IO->SendBuf.fd = IO->RecvBuf.fd =
888 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
892 if (IO->SendBuf.fd < 0) {
894 "EVENT: socket() failed: %s\n",
897 StrBufPrintf(IO->ErrMsg,
898 "Failed to create socket: %s",
900 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
903 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
906 "EVENT: unable to get socket %d flags! %s \n",
909 StrBufPrintf(IO->ErrMsg,
910 "Failed to get socket %d flags: %s",
913 close(IO->SendBuf.fd);
914 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
917 fdflags = fdflags | O_NONBLOCK;
918 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
921 "EVENT: unable to set socket %d nonblocking flags! %s \n",
924 StrBufPrintf(IO->ErrMsg,
925 "Failed to set socket flags: %s",
927 close(IO->SendBuf.fd);
928 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
931 /* TODO: maye we could use offsetof() to calc the position of data...
932 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
934 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
935 IO->recv_event.data = IO;
936 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
937 IO->send_event.data = IO;
939 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
940 IO->conn_fail.data = IO;
941 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
942 IO->rw_timeout.data = IO;
947 /* for debugging you may bypass it like this:
948 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
949 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
950 * inet_addr("127.0.0.1");
952 if (IO->ConnectMe->IPv6) {
953 rc = connect(IO->SendBuf.fd,
954 &IO->ConnectMe->Addr,
955 sizeof(struct sockaddr_in6));
958 /* If citserver is bound to a specific IP address on the host, make
959 * sure we use that address for outbound connections.
962 memset(&egress_sin, 0, sizeof(egress_sin));
963 egress_sin.sin_family = AF_INET;
964 if (!IsEmptyStr(config.c_ip_addr)) {
965 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
966 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
967 egress_sin.sin_addr.s_addr = INADDR_ANY;
970 /* If this bind fails, no problem; we can still use INADDR_ANY */
971 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
973 rc = connect(IO->SendBuf.fd,
974 (struct sockaddr_in *)&IO->ConnectMe->Addr,
975 sizeof(struct sockaddr_in));
979 EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd);
980 set_start_callback(event_base, IO, 0);
981 return IO->NextState;
983 else if (errno == EINPROGRESS) {
984 EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd);
986 ev_io_init(&IO->conn_event,
987 IO_connestd_callback,
991 IO->conn_event.data = IO;
993 ev_io_start(event_base, &IO->conn_event);
994 ev_timer_start(event_base, &IO->conn_fail);
995 return IO->NextState;
998 ev_idle_init(&IO->conn_fail_immediate,
999 IO_connfailimmediate_callback);
1000 IO->conn_fail_immediate.data = IO;
1001 ev_idle_start(event_base, &IO->conn_fail_immediate);
1004 "connect() = %d failed: %s\n",
1008 StrBufPrintf(IO->ErrMsg,
1009 "Failed to connect: %s",
1011 return IO->NextState;
1013 return IO->NextState;
1016 void SetNextTimeout(AsyncIO *IO, double timeout)
1018 IO->rw_timeout.repeat = timeout;
1019 ev_timer_again (event_base, &IO->rw_timeout);
1023 eNextState ReAttachIO(AsyncIO *IO,
1028 become_session(IO->CitContext);
1029 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1031 IO->NextState = eReadMessage;
1034 IO->NextState = eSendReply;
1036 set_start_callback(event_base, IO, 0);
1038 return IO->NextState;
1041 void InitIOStruct(AsyncIO *IO,
1043 eNextState NextState,
1044 IO_LineReaderCallback LineReader,
1045 IO_CallBack DNS_Fail,
1046 IO_CallBack SendDone,
1047 IO_CallBack ReadDone,
1048 IO_CallBack Terminate,
1049 IO_CallBack DBTerminate,
1050 IO_CallBack ConnFail,
1051 IO_CallBack Timeout,
1052 IO_CallBack ShutdownAbort)
1056 IO->CitContext = CloneContext(CC);
1057 IO->CitContext->session_specific_data = Data;
1058 IO->CitContext->IO = IO;
1060 IO->NextState = NextState;
1062 IO->SendDone = SendDone;
1063 IO->ReadDone = ReadDone;
1064 IO->Terminate = Terminate;
1065 IO->DBTerminate = DBTerminate;
1066 IO->LineReader = LineReader;
1067 IO->ConnFail = ConnFail;
1068 IO->Timeout = Timeout;
1069 IO->ShutdownAbort = ShutdownAbort;
1071 IO->DNS.Fail = DNS_Fail;
1073 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1074 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1075 IO->IOBuf = NewStrBuf();
1076 EV_syslog(LOG_DEBUG,
1077 "EVENT: Session lives at %p IO at %p \n",
1082 extern int evcurl_init(AsyncIO *IO);
1084 int InitcURLIOStruct(AsyncIO *IO,
1087 IO_CallBack SendDone,
1088 IO_CallBack Terminate,
1089 IO_CallBack DBTerminate,
1090 IO_CallBack ShutdownAbort)
1094 IO->CitContext = CloneContext(CC);
1095 IO->CitContext->session_specific_data = Data;
1096 IO->CitContext->IO = IO;
1098 IO->SendDone = SendDone;
1099 IO->Terminate = Terminate;
1100 IO->DBTerminate = DBTerminate;
1101 IO->ShutdownAbort = ShutdownAbort;
1103 strcpy(IO->HttpReq.errdesc, Desc);
1106 return evcurl_init(IO);
1111 typedef struct KillOtherSessionContext {
1114 }KillOtherSessionContext;
1116 eNextState KillTerminate(AsyncIO *IO)
1118 KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
1119 EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
1120 FreeAsyncIOContents(IO);
1121 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1127 eNextState KillShutdown(AsyncIO *IO)
1129 return eTerminateConnection;
1132 eNextState KillOtherContextNow(AsyncIO *IO)
1134 KillOtherSessionContext *Ctx = IO->Data;
1136 if (Ctx->OtherOne->ShutdownAbort != NULL)
1137 Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
1138 return eTerminateConnection;
1141 void KillAsyncIOContext(AsyncIO *IO)
1143 KillOtherSessionContext *Ctx;
1145 Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
1146 memset(Ctx, 0, sizeof(KillOtherSessionContext));
1148 InitIOStruct(&Ctx->IO,
1163 switch(IO->NextState) {
1176 QueueEventContext(&Ctx->IO, KillOtherContextNow);
1179 QueueDBOperation(&Ctx->IO, KillOtherContextNow);
1181 case eTerminateConnection:
1183 /*hm, its already dying, dunno which Queue its in... */
1189 extern int DebugEventLoopBacktrace;
1190 void EV_backtrace(AsyncIO *IO)
1192 #ifdef HAVE_BACKTRACE
1193 void *stack_frames[50];
1197 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1199 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1200 strings = backtrace_symbols(stack_frames, size);
1201 for (i = 0; i < size; i++) {
1202 if (strings != NULL) {
1203 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1206 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1214 ev_tstamp ctdl_ev_now (void)
1216 return ev_now(event_base);