+
+void InitIOStruct(AsyncIO *IO,
+ void *Data,
+ eNextState NextState,
+ IO_LineReaderCallback LineReader,
+ IO_CallBack DNS_Fail,
+ IO_CallBack SendDone,
+ IO_CallBack ReadDone,
+ IO_CallBack Terminate,
+ IO_CallBack DBTerminate,
+ IO_CallBack ConnFail,
+ IO_CallBack Timeout,
+ IO_CallBack ShutdownAbort)
+{
+ IO->Data = Data;
+
+ IO->CitContext = CloneContext(CC);
+ IO->CitContext->session_specific_data = Data;
+ IO->CitContext->IO = IO;
+
+ IO->NextState = NextState;
+
+ IO->SendDone = SendDone;
+ IO->ReadDone = ReadDone;
+ IO->Terminate = Terminate;
+ IO->DBTerminate = DBTerminate;
+ IO->LineReader = LineReader;
+ IO->ConnFail = ConnFail;
+ IO->Timeout = Timeout;
+ IO->ShutdownAbort = ShutdownAbort;
+
+ IO->DNS.Fail = DNS_Fail;
+
+ IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
+ IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
+ IO->IOBuf = NewStrBuf();
+ EV_syslog(LOG_DEBUG,
+ "EVENT: Session lives at %p IO at %p \n",
+ Data, IO);
+
+}
+
+extern int evcurl_init(AsyncIO *IO);
+
+int InitcURLIOStruct(AsyncIO *IO,
+ void *Data,
+ const char* Desc,
+ IO_CallBack SendDone,
+ IO_CallBack Terminate,
+ IO_CallBack DBTerminate,
+ IO_CallBack ShutdownAbort)
+{
+ IO->Data = Data;
+
+ IO->CitContext = CloneContext(CC);
+ IO->CitContext->session_specific_data = Data;
+ IO->CitContext->IO = IO;
+
+ IO->SendDone = SendDone;
+ IO->Terminate = Terminate;
+ IO->DBTerminate = DBTerminate;
+ IO->ShutdownAbort = ShutdownAbort;
+
+ strcpy(IO->HttpReq.errdesc, Desc);
+
+
+ return evcurl_init(IO);
+
+}
+
+
+typedef struct KillOtherSessionContext {
+ AsyncIO IO;
+ AsyncIO *OtherOne;
+}KillOtherSessionContext;
+
+eNextState KillTerminate(AsyncIO *IO)
+{
+ long id;
+ KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
+ EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
+ id = IO->ID;
+ FreeAsyncIOContents(IO);
+ memset(Ctx, 0, sizeof(KillOtherSessionContext));
+ IO->ID = id; /* just for the case we want to analyze it in a coredump */
+ free(Ctx);
+ return eAbort;
+
+}
+
+eNextState KillShutdown(AsyncIO *IO)
+{
+ return eTerminateConnection;
+}
+
+eNextState KillOtherContextNow(AsyncIO *IO)
+{
+ KillOtherSessionContext *Ctx = IO->Data;
+
+ SetEVState(IO, eKill);
+
+ if (Ctx->OtherOne->ShutdownAbort != NULL)
+ Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
+ return eTerminateConnection;
+}
+
+void KillAsyncIOContext(AsyncIO *IO)
+{
+ KillOtherSessionContext *Ctx;
+
+ Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
+ memset(Ctx, 0, sizeof(KillOtherSessionContext));
+
+ InitIOStruct(&Ctx->IO,
+ Ctx,
+ eReadMessage,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ KillTerminate,
+ NULL,
+ NULL,
+ NULL,
+ KillShutdown);
+
+ Ctx->OtherOne = IO;
+
+ switch(IO->NextState) {
+ case eSendDNSQuery:
+ case eReadDNSReply:
+
+ case eConnect:
+ case eSendReply:
+ case eSendMore:
+ case eSendFile:
+
+ case eReadMessage:
+ case eReadMore:
+ case eReadPayload:
+ case eReadFile:
+ QueueEventContext(&Ctx->IO, KillOtherContextNow);
+ break;
+ case eDBQuery:
+ QueueDBOperation(&Ctx->IO, KillOtherContextNow);
+ break;
+ case eTerminateConnection:
+ case eAbort:
+ /*hm, its already dying, dunno which Queue its in... */
+ free(Ctx);
+ }
+
+}
+
+extern int DebugEventLoopBacktrace;
+void EV_backtrace(AsyncIO *IO)
+{
+#ifdef HAVE_BACKTRACE
+ void *stack_frames[50];
+ size_t size, i;
+ char **strings;
+
+ if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
+ return;
+ size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
+ strings = backtrace_symbols(stack_frames, size);
+ for (i = 0; i < size; i++) {
+ if (strings != NULL) {
+ EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
+ }
+ else {
+ EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
+ }
+ }
+ free(strings);
+#endif
+}
+
+
+ev_tstamp ctdl_ev_now (void)
+{
+ return ev_now(event_base);
+}