#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)
{
+ IOAddHandler *h;
+ int i;
+
+ h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
+ h->IO = IO;
+ h->EvAttch = CB;
+
citthread_mutex_lock(&EventQueueMutex);
if (event_add_pipe[1] == -1) {
citthread_mutex_unlock(&EventQueueMutex);
-
+ free (h);
return -1;
}
-
- QueueEventAddPtr = Ctx;
- EventContextAttachPtr = CB;
- QueueThisIO = IO;
+ 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);
- citthread_mutex_unlock(&EventQueueMutex);
+ CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
return 0;
}
FreeStrBuf(&IO->IOBuf);
FreeStrBuf(&IO->SendBuf.Buf);
FreeStrBuf(&IO->RecvBuf.Buf);
-
+ ares_destroy(IO->DNSChannel);
}
-/*
-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);
-
- event_set(&instance->sigquit_event, SIGQUIT, EV_SIGNAL|EV_PERSIST,
- exit_event_callback, instance);
- event_add(&instance->sigquit_event, NULL);
-}
-*/
void ShutDownCLient(AsyncIO *IO)
{
- event_del(&IO->send_event);
- event_del(&IO->recv_event);
- IO->Terminate(IO->Data);
-
-// citthread_mutex_lock(&EventQueueMutex);
+ CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
-///QueueEvents /// todo remove from hash.
+ 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;
+ }
-// citthread_mutex_unlock(&EventQueueMutex);
+ IO->Terminate(IO);
+
}
+
eReadState HandleInbound(AsyncIO *IO)
{
eReadState Finished = eBufferNotEmpty;
}
if (Finished != eMustReadMore) {
- event_del(&IO->recv_event);
- IO->NextState = IO->ReadDone(IO->Data);
+ ev_io_stop(event_base, &IO->recv_event);
+ IO->NextState = IO->ReadDone(IO);
Finished = StrBufCheckBuffer(&IO->RecvBuf);
}
}
if ((IO->NextState == eSendReply) ||
(IO->NextState == eSendMore))
{
- IO->NextState = IO->SendDone(IO->Data);
- event_add(&IO->send_event, NULL);
-
+ IO->NextState = IO->SendDone(IO);
+ ev_io_start(event_base, &IO->send_event);
}
else if ((IO->NextState == eTerminateConnection) ||
(IO->NextState == eAbort) )
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);
-
- if ((IO->NextState == eTerminateConnection) ||
- (IO->NextState == eAbort) )
- ShutDownCLient(IO);
- else
- 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:
+ 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)
+ 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);
+ 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);
+ IO->ConnFail(IO);
+}
+static void
+IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
+{
+ AsyncIO *IO = watcher->data;
- nbytes = StrBuf_read_one_chunk_callback(fd, event, &IO->RecvBuf);
+ 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(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);
+ IO->Timeout(IO); /* this is a timeout... */
return;
} else if (nbytes == -1) {
/// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
}
}
-void IOReadNextLine(AsyncIO *IO, int timeout)
+
+
+int 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 -1;
+ }
+ 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 -1;
+ }
+ 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 -1;
+ }
+/* 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;
+
+ rc = connect(IO->sock,
+ (struct sockaddr *) &IO->Addr,
+ (IO->IP6)? ///HEnt->h_addrtype == AF_INET6)?
+ sizeof(struct in6_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;
+ }
+ 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 0;
+ }
+ else {
+ CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
+ StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
+ IO->ConnFail(IO);
+ return -1;
+ }
}
-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_CallBack Terminate,
- IO_LineReaderCallback LineReader,
+ double conn_timeout,
+ double first_rw_timeout,
int ReadFirst)
{
IO->Data = pData;
- IO->SendDone = SendDone;
- IO->ReadDone = ReadDone;
- IO->Terminate = Terminate;
- 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;
}
+ IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
+// IO->res = HEnt->h_addr_list[0];
+ event_connect_socket(IO, conn_timeout, first_rw_timeout);
}
-
-
-#endif