libev migration / c-ares migration
[citadel.git] / citadel / event_client.c
index 0f04757eaaafae7a251b521e7363be9e17be225a..60f74282df813cee217614d2ba6af44c1b8e4403 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"
 #include "locate_host.h"
 #include "citadel_dirs.h"
 
-#ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
-
-#include <event.h>
 #include "event_client.h"
 
 extern int event_add_pipe[2];
 extern citthread_mutex_t EventQueueMutex;
-extern void *QueueEventAddPtr;
-extern AsyncIO *QueueThisIO;
-extern EventContextAttach EventContextAttachPtr;
+extern HashList *InboundEventQueue;
+extern struct ev_loop *event_base;
 
-int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
+       
+int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
 {
-       citthread_mutex_lock(&EventQueueMutex);
+       IOAddHandler *h;
+       int i;
 
-       QueueEventAddPtr = Ctx;
-       EventContextAttachPtr = CB;
-       QueueThisIO = IO;
+       h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
+       h->IO = IO;
+       h->EvAttch = CB;
 
-       write(event_add_pipe[1], "+_", 1);
+       citthread_mutex_lock(&EventQueueMutex);
+       if (event_add_pipe[1] == -1) {
+               citthread_mutex_unlock(&EventQueueMutex);
+               free (h);
+               return -1;
+       }
+       CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
+       i = GetCount(InboundEventQueue);
+       Put(InboundEventQueue, IKEY(i), h, NULL);
        citthread_mutex_unlock(&EventQueueMutex);
+
+       write(event_add_pipe[1], "+_", 1);
+       CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
        return 0;
 }
 
 
 int ShutDownEventQueue(void)
 {
+       citthread_mutex_lock(&EventQueueMutex);
+       if (event_add_pipe[1] == -1) {
+               citthread_mutex_unlock(&EventQueueMutex);
+
+               return -1;
+       }
        write(event_add_pipe[1], "x_", 1);
        close(event_add_pipe[1]);
+       event_add_pipe[1] = -1;
+       citthread_mutex_unlock(&EventQueueMutex);
        return 0;
 }
 
@@ -100,33 +119,39 @@ void FreeAsyncIOContents(AsyncIO *IO)
        FreeStrBuf(&IO->IOBuf);
        FreeStrBuf(&IO->SendBuf.Buf);
        FreeStrBuf(&IO->RecvBuf.Buf);
-
 }
 
-/*
-static void
-setup_signal_handlers(struct instance *instance)
-{
-    signal(SIGPIPE, SIG_IGN);
-
-    event_set(&instance->sigterm_event, SIGTERM, EV_SIGNAL|EV_PERSIST,
-              exit_event_callback, instance);
-    event_add(&instance->sigterm_event, NULL);
 
-    event_set(&instance->sigint_event, SIGINT, EV_SIGNAL|EV_PERSIST,
-              exit_event_callback, instance);
-    event_add(&instance->sigint_event, NULL);
+void ShutDownCLient(AsyncIO *IO)
+{
+       CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
 
-    event_set(&instance->sigquit_event, SIGQUIT, EV_SIGNAL|EV_PERSIST,
-              exit_event_callback, instance);
-    event_add(&instance->sigquit_event, NULL);
+       if (IO->sock != 0)
+       {
+               ev_io_stop(event_base, &IO->send_event);
+               ev_io_stop(event_base, &IO->recv_event);
+               ev_timer_stop (event_base, &IO->rw_timeout);
+               close(IO->sock);
+               IO->sock = 0;
+               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);
+       
 }
-*/
+
 
 eReadState HandleInbound(AsyncIO *IO)
 {
        eReadState Finished = eBufferNotEmpty;
-               
+       
        while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
                if (IO->RecvBuf.nBlobBytesWanted != 0) { 
                                
@@ -153,8 +178,9 @@ eReadState HandleInbound(AsyncIO *IO)
                }
                        
                if (Finished != eMustReadMore) {
-                       event_del(&IO->recv_event);
-                       IO->NextState = IO->ReadDone(IO->Data);
+                       assert(IO->ReadDone);
+                       ev_io_stop(event_base, &IO->recv_event);
+                       IO->NextState = IO->ReadDone(IO);
                        Finished = StrBufCheckBuffer(&IO->RecvBuf);
                }
        }
@@ -163,78 +189,146 @@ eReadState HandleInbound(AsyncIO *IO)
        if ((IO->NextState == eSendReply) ||
            (IO->NextState == eSendMore))
        {
-               IO->NextState = IO->SendDone(IO->Data);
-               event_add(&IO->send_event, NULL);
-                       
+               assert(IO->SendDone);
+               IO->NextState = IO->SendDone(IO);
+               ev_io_start(event_base, &IO->send_event);
        }
        else if ((IO->NextState == eTerminateConnection) ||
                 (IO->NextState == eAbort) )
-{
-
-
-       }
+               ShutDownCLient(IO);
        return Finished;
 }
 
 
 static void
-IO_send_callback(int fd, short event, void *ctx)
+IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
 {
        int rc;
-       AsyncIO *IO = ctx;
+       AsyncIO *IO = watcher->data;
 
-       (void)fd;
-       (void)event;
-       
-///    assert(fd == IO->sock);
-       
-       rc = StrBuf_write_one_chunk_callback(fd, event, &IO->SendBuf);
+       rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
 
        if (rc == 0)
-       {
-           event_del(&IO->send_event);
-           switch (IO->NextState) {
-           case eSendReply:
-                   break;
-           case eSendMore:
-                   IO->NextState = IO->SendDone(IO->Data);
-                   event_add(&IO->send_event, NULL);
-                   break;
-           case eReadMessage:
-                   if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
-                           HandleInbound(IO);
-                   }
-                   else {
-                           event_add(&IO->recv_event, NULL);
-                   }
-
-                   break;
-           case eAbort:
-                   break;
-           }
+       {               
+#ifdef BIGBAD_IODBG
+               {
+                       int rv = 0;
+                       char fn [SIZ];
+                       FILE *fd;
+                       const char *pch = ChrPtr(IO->SendBuf.Buf);
+                       const char *pchh = IO->SendBuf.ReadWritePointer;
+                       long nbytes;
+
+                       if (pchh == NULL)
+                               pchh = pch;
+                       
+                       nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
+                       snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->sock);
+               
+                       fd = fopen(fn, "a+");
+                       fprintf(fd, "Read: BufSize: %ld BufContent: [",
+                               nbytes);
+                       rv = fwrite(pchh, nbytes, 1, fd);
+                       fprintf(fd, "]\n");
+               
+                       
+                       fclose(fd);
+               }
+#endif
+               ev_io_stop(event_base, &IO->send_event);
+               switch (IO->NextState) {
+               case eSendReply:
+                       break;
+               case eSendMore:
+                       assert(IO->SendDone);
+                       IO->NextState = IO->SendDone(IO);
+
+                       if ((IO->NextState == eTerminateConnection) ||
+                           (IO->NextState == eAbort) )
+                               ShutDownCLient(IO);
+                       else {
+                               ev_io_start(event_base, &IO->send_event);
+                       }
+                       break;
+               case eReadMessage:
+                       if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
+                               HandleInbound(IO);
+                       }
+                       else {
+                               ev_io_start(event_base, &IO->recv_event);
+                       }
+
+                       break;
+               case eTerminateConnection:
+               case eAbort:
+                       break;
+               }
+       }
+       else if (rc < 0) {
+               assert(IO->Timeout);
+               IO->Timeout(IO);
+       }
+       /* else : must write more. */
+}
+static void
+set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
+{
+       
+       switch(IO->NextState) {
+       case eReadMessage:
+               ev_io_start(event_base, &IO->recv_event);
+               break;
+       case eSendReply:
+       case eSendMore:
+               IO_send_callback(loop, &IO->send_event, revents);
+               break;
+       case eTerminateConnection:
+       case eAbort:
+               /// TODO: WHUT?
+               break;
        }
-       else if (rc > 0)
-               return;
-//     else 
-               ///abort!
 }
 
+static void
+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
-IO_recv_callback(int fd, short event, void *ctx)
+IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
 {
-       ssize_t nbytes;
-       AsyncIO *IO = ctx;
+       AsyncIO *IO = watcher->data;
 
-//    assert(fd == IO->sock);
-       
-//    assert(fd == sb->fd);
+       ev_timer_stop (event_base, &IO->conn_fail);
+       ev_io_stop(loop, &IO->conn_event);
+       assert(IO->ConnFail);
+       IO->ConnFail(IO);
+}
+static void
+IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
+{
+       AsyncIO *IO = watcher->data;
+
+       ev_io_stop(loop, &IO->conn_event);
+       ev_timer_stop (event_base, &IO->conn_fail);
+       set_start_callback(loop, IO, revents);
+}
+static void
+IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
+{
+       ssize_t nbytes;
+       AsyncIO *IO = watcher->data;
 
-       nbytes = StrBuf_read_one_chunk_callback(fd, event, &IO->RecvBuf);
+       nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
        if (nbytes > 0) {
                HandleInbound(IO);
        } else if (nbytes == 0) {
-               ///  TODO: this is a timeout???  sock_buff_invoke_free(sb, 0);
+               assert(IO->Timeout);
+               IO->Timeout(IO); /* this is a timeout... */
                return;
        } else if (nbytes == -1) {
 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
@@ -242,47 +336,104 @@ IO_recv_callback(int fd, short event, void *ctx)
        }
 }
 
-void IOReadNextLine(AsyncIO *IO, int timeout)
+
+
+eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
 {
+       int fdflags; 
+       int rc = -1;
+
+       IO->SendBuf.fd = IO->RecvBuf.fd = 
+               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 eAbort;
+       }
+       fdflags = fcntl(IO->sock, F_GETFL);
+       if (fdflags < 0) {
+               CtdlLogPrintf(CTDL_DEBUG,
+                             "EVENT: unable to get socket flags! %s \n",
+                             strerror(errno));
+               StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
+               return eAbort;
+       }
+       fdflags = fdflags | O_NONBLOCK;
+       if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
+               CtdlLogPrintf(CTDL_DEBUG,
+                             "EVENT: unable to set socket nonblocking flags! %s \n",
+                             strerror(errno));
+               StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
+               close(IO->sock);
+               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
+ */
+       ev_io_init(&IO->recv_event, IO_recv_callback, IO->sock, EV_READ);
+       IO->recv_event.data = IO;
+       ev_io_init(&IO->send_event, IO_send_callback, IO->sock, EV_WRITE);
+       IO->send_event.data = IO;
+
+       ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
+       IO->conn_fail.data = IO;
+       ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
+       IO->rw_timeout.data = IO;
+       ///struct sockaddr_in *addr = &IO->Addr;
+       if (IO->IP6)
+               rc = connect(IO->sock, &IO->Addr, sizeof(struct in6_addr));
+       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 IO->NextState;
+       }
+       else if (errno == EINPROGRESS) {
+
+               ev_io_init(&IO->conn_event, IO_connestd_callback, IO->sock, EV_READ|EV_WRITE);
+               IO->conn_event.data = IO;
 
+               ev_io_start(event_base, &IO->conn_event);
+               ev_timer_start(event_base, &IO->conn_fail);
+               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 eAbort;
+       }
+       return IO->NextState;
 }
 
-void IOReadNextBLOB(AsyncIO *IO, int timeout, long size)
+void SetNextTimeout(AsyncIO *IO, double timeout)
 {
+       IO->rw_timeout.repeat = timeout;
+       ev_timer_again (event_base,  &IO->rw_timeout);
 }
 
-void InitEventIO(AsyncIO *IO, 
-                void *pData, 
-                IO_CallBack ReadDone, 
-                IO_CallBack SendDone, 
-                IO_LineReaderCallback LineReader,
-                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->LineReader = LineReader;
-
-       event_set(&IO->recv_event, 
-                 IO->sock, 
-                 EV_READ|EV_PERSIST,
-                 IO_recv_callback, 
-                 IO);
-
-       event_set(&IO->send_event, 
-                 IO->sock,
-                 EV_WRITE|EV_PERSIST,
-                 IO_send_callback, 
-                 IO);
-
+       
        if (ReadFirst) {
                IO->NextState = eReadMessage;
-               event_add(&IO->recv_event, NULL);
        }
        else {
                IO->NextState = eSendReply;
        }
+       return event_connect_socket(IO, conn_timeout, first_rw_timeout);
 }
-
-
-#endif