]> code.citadel.org Git - citadel.git/blobdiff - citadel/event_client.c
libev/c-ares migration: unstack out ouf c-ares before querying new requests
[citadel.git] / citadel / event_client.c
index 0dd97d4b95f964c414fc423c5e0822f374937425..b45367d08f71ac784ad9960c2602a34f8fbbe916 100644 (file)
@@ -46,6 +46,8 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <assert.h>
+
 #include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
@@ -71,13 +73,13 @@ extern HashList *InboundEventQueue;
 extern struct ev_loop *event_base;
 
        
-int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
+int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
 {
        IOAddHandler *h;
        int i;
 
        h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
-       h->Ctx = Ctx;
+       h->IO = IO;
        h->EvAttch = CB;
 
        citthread_mutex_lock(&EventQueueMutex);
@@ -117,7 +119,6 @@ void FreeAsyncIOContents(AsyncIO *IO)
        FreeStrBuf(&IO->IOBuf);
        FreeStrBuf(&IO->SendBuf.Buf);
        FreeStrBuf(&IO->RecvBuf.Buf);
-       ares_destroy(IO->DNSChannel);
 }
 
 
@@ -135,7 +136,13 @@ void ShutDownCLient(AsyncIO *IO)
                IO->SendBuf.fd = 0;
                IO->RecvBuf.fd = 0;
        }
-
+       if (IO->DNSChannel != NULL) {
+               ares_destroy(IO->DNSChannel);
+               ev_io_stop(event_base, &IO->dns_recv_event);
+               ev_io_stop(event_base, &IO->dns_send_event);
+               IO->DNSChannel = NULL;
+       }
+       assert(IO->Terminate);
        IO->Terminate(IO);
        
 }
@@ -144,7 +151,7 @@ void ShutDownCLient(AsyncIO *IO)
 eReadState HandleInbound(AsyncIO *IO)
 {
        eReadState Finished = eBufferNotEmpty;
-               
+       
        while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
                if (IO->RecvBuf.nBlobBytesWanted != 0) { 
                                
@@ -171,6 +178,7 @@ eReadState HandleInbound(AsyncIO *IO)
                }
                        
                if (Finished != eMustReadMore) {
+                       assert(IO->ReadDone);
                        ev_io_stop(event_base, &IO->recv_event);
                        IO->NextState = IO->ReadDone(IO);
                        Finished = StrBufCheckBuffer(&IO->RecvBuf);
@@ -181,6 +189,7 @@ eReadState HandleInbound(AsyncIO *IO)
        if ((IO->NextState == eSendReply) ||
            (IO->NextState == eSendMore))
        {
+               assert(IO->SendDone);
                IO->NextState = IO->SendDone(IO);
                ev_io_start(event_base, &IO->send_event);
        }
@@ -231,6 +240,7 @@ IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
                case eSendReply:
                        break;
                case eSendMore:
+                       assert(IO->SendDone);
                        IO->NextState = IO->SendDone(IO);
 
                        if ((IO->NextState == eTerminateConnection) ||
@@ -254,8 +264,10 @@ IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
                        break;
                }
        }
-       else if (rc < 0)
+       else if (rc < 0) {
+               assert(IO->Timeout);
                IO->Timeout(IO);
+       }
        /* else : must write more. */
 }
 static void
@@ -283,6 +295,7 @@ IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
        AsyncIO *IO = watcher->data;
 
        ev_timer_stop (event_base, &IO->rw_timeout);
+       assert(IO->Timeout);
        IO->Timeout(IO);
 }
 static void
@@ -292,6 +305,7 @@ IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
 
        ev_timer_stop (event_base, &IO->conn_fail);
        ev_io_stop(loop, &IO->conn_event);
+       assert(IO->ConnFail);
        IO->ConnFail(IO);
 }
 static void
@@ -313,6 +327,7 @@ IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
        if (nbytes > 0) {
                HandleInbound(IO);
        } else if (nbytes == 0) {
+               assert(IO->Timeout);
                IO->Timeout(IO); /* this is a timeout... */
                return;
        } else if (nbytes == -1) {
@@ -321,26 +336,29 @@ IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
        }
 }
 
+void
+IO_postdns_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
+{
+       AsyncIO *IO = watcher->data;
+       IO->PostDNS(IO);
+}
 
-
-int event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
+eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
 {
-       struct sockaddr_in  saddr;
        int fdflags; 
        int rc = -1;
 
        IO->SendBuf.fd = IO->RecvBuf.fd = 
-               IO->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
-/*
-IO->curr_ai->ai_family, 
-                         IO->curr_ai->ai_socktype, 
-                         IO->curr_ai->ai_protocol);
-*/
+               IO->sock = socket(
+                       (IO->IP6)?PF_INET6:PF_INET, 
+                       SOCK_STREAM, 
+                       IPPROTO_TCP);
+
        if (IO->sock < 0) {
                CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
                StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
 //             freeaddrinfo(res);
-               return -1;
+               return eAbort;
        }
        fdflags = fcntl(IO->sock, F_GETFL);
        if (fdflags < 0) {
@@ -348,7 +366,7 @@ IO->curr_ai->ai_family,
                              "EVENT: unable to get socket flags! %s \n",
                              strerror(errno));
                StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
-               return -1;
+               return eAbort;
        }
        fdflags = fdflags | O_NONBLOCK;
        if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
@@ -357,7 +375,7 @@ IO->curr_ai->ai_family,
                              strerror(errno));
                StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
                close(IO->sock);
-               return -1;
+               return eAbort;
        }
 /* TODO: maye we could use offsetof() to calc the position of data... 
  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
@@ -371,26 +389,17 @@ IO->curr_ai->ai_family,
        IO->conn_fail.data = IO;
        ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
        IO->rw_timeout.data = IO;
-       
-       memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
-
-       memcpy(&saddr.sin_addr, 
-              IO->HEnt->h_addr_list[0],
-              sizeof(struct in_addr));
-       saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-       saddr.sin_family = AF_INET;
-       saddr.sin_port = htons(IO->dport);
-       rc = connect(IO->sock, 
-                    (struct sockaddr *) &saddr,
-/// TODO: ipv6??                    (IO->HEnt->h_addrtype == AF_INET6)?
-                    /*     sizeof(in6_addr):*/
-                    sizeof(struct sockaddr_in));
+
+       if (IO->IP6)
+               rc = connect(IO->sock, &IO->Addr, sizeof(struct sockaddr_in6));
+       else
+               rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
+
        if (rc >= 0){
 ////           freeaddrinfo(res);
                set_start_callback(event_base, IO, 0);
                ev_timer_start(event_base, &IO->rw_timeout);
-               return 0;
+               return IO->NextState;
        }
        else if (errno == EINPROGRESS) {
 
@@ -399,14 +408,16 @@ IO->curr_ai->ai_family,
 
                ev_io_start(event_base, &IO->conn_event);
                ev_timer_start(event_base, &IO->conn_fail);
-               return 0;
+               return IO->NextState;
        }
        else {
                CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
                StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
+               assert(IO->ConnFail);
                IO->ConnFail(IO);
-               return -1;
+               return eAbort;
        }
+       return IO->NextState;
 }
 
 void SetNextTimeout(AsyncIO *IO, double timeout)
@@ -415,25 +426,13 @@ void SetNextTimeout(AsyncIO *IO, double timeout)
        ev_timer_again (event_base,  &IO->rw_timeout);
 }
 
-void InitEventIO(AsyncIO *IO, 
-                void *pData, 
-                IO_CallBack ReadDone, 
-                IO_CallBack SendDone, 
-                IO_CallBack Terminate, 
-                IO_CallBack Timeout, 
-                IO_CallBack ConnFail, 
-                IO_LineReaderCallback LineReader,
-                double conn_timeout, 
-                double first_rw_timeout,
-                int ReadFirst)
+eNextState InitEventIO(AsyncIO *IO, 
+                      void *pData, 
+                      double conn_timeout, 
+                      double first_rw_timeout,
+                      int ReadFirst)
 {
        IO->Data = pData;
-       IO->SendDone = SendDone;
-       IO->ReadDone = ReadDone;
-       IO->Terminate = Terminate;
-       IO->LineReader = LineReader;
-       IO->ConnFail = ConnFail;
-       IO->Timeout = Timeout;
        
        if (ReadFirst) {
                IO->NextState = eReadMessage;
@@ -441,7 +440,5 @@ void InitEventIO(AsyncIO *IO,
        else {
                IO->NextState = eSendReply;
        }
-       IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
-//     IO->res = HEnt->h_addr_list[0];
-       event_connect_socket(IO, conn_timeout, first_rw_timeout);
+       return event_connect_socket(IO, conn_timeout, first_rw_timeout);
 }