#ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
-#include <event.h>
#include "event_client.h"
extern int event_add_pipe[2];
extern citthread_mutex_t EventQueueMutex;
extern HashList *InboundEventQueue;
+extern struct ev_loop *event_base;
-#define SEND_EVENT 1
-#define RECV_EVENT 2
int QueueEventContext(void *Ctx, AsyncIO *IO, EventContextAttach CB)
{
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)
{
CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->sock);
- switch (IO->active_event) {
- case SEND_EVENT:
- event_del(&IO->send_event);
- break;
- case RECV_EVENT:
- event_del(&IO->recv_event);
- break;
- case 0:
- // no event active here; just bail out.
- break;
- }
- IO->active_event = 0;
- IO->Terminate(IO->Data);
-
-// citthread_mutex_lock(&EventQueueMutex);
-///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->conn_fail);
+ ev_timer_stop (event_base, &IO->conn_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)
}
if (Finished != eMustReadMore) {
- event_del(&IO->recv_event);
- IO->active_event = 0;
- 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->active_event = SEND_EVENT;
-
+ 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;
-
- (void)fd;
- (void)event;
- CtdlLogPrintf(CTDL_DEBUG, "EVENT -> %d : [%s%s%s%s]\n",
- (int) fd,
- (event&EV_TIMEOUT) ? " timeout" : "",
- (event&EV_READ) ? " read" : "",
- (event&EV_WRITE) ? " write" : "",
- (event&EV_SIGNAL) ? " signal" : "");
-
-/// assert(fd == IO->sock);
-
- rc = StrBuf_write_one_chunk_callback(fd, event, &IO->SendBuf);
+ AsyncIO *IO = watcher->data;
+
+ rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
if (rc == 0)
- {
-
+ {
#ifdef BIGBAD_IODBG
{
int rv = 0;
fclose(fd);
}
#endif
- event_del(&IO->send_event);
- IO->active_event = 0;
+ ev_io_stop(event_base, &IO->send_event);
switch (IO->NextState) {
case eSendReply:
break;
case eSendMore:
- IO->NextState = IO->SendDone(IO->Data);
+ IO->NextState = IO->SendDone(IO);
if ((IO->NextState == eTerminateConnection) ||
(IO->NextState == eAbort) )
ShutDownCLient(IO);
else {
- event_add(&IO->send_event, NULL);
- IO->active_event = SEND_EVENT;
+ ev_io_start(event_base, &IO->send_event);
}
break;
case eReadMessage:
HandleInbound(IO);
}
else {
- event_add(&IO->recv_event, NULL);
- IO->active_event = RECV_EVENT;
+ ev_io_start(event_base, &IO->recv_event);
}
break;
+ case eTerminateConnection:
case eAbort:
break;
}
}
- else if (rc > 0)
- ShutDownCLient(IO);
-// else
- ///abort!
+ else if (rc < 0)
+ IO->Timeout(IO);
+ /* else : must write more. */
}
+static void
+IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
+{
+ AsyncIO *IO = watcher->data;
+
+ IO->Timeout(IO);
+}
+static void
+IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
+{
+ AsyncIO *IO = watcher->data;
+ IO->ConnFail(IO);
+}
static void
-IO_recv_callback(int fd, short event, void *ctx)
+IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
{
ssize_t nbytes;
- AsyncIO *IO = ctx;
+ AsyncIO *IO = watcher->data;
-// assert(fd == IO->sock);
-
-// assert(fd == sb->fd);
-
- CtdlLogPrintf(CTDL_DEBUG, "EVENT <- %d : [%s%s%s%s]\n",
- (int) fd,
- (event&EV_TIMEOUT) ? " timeout" : "",
- (event&EV_READ) ? " read" : "",
- (event&EV_WRITE) ? " write" : "",
- (event&EV_SIGNAL) ? " signal" : "");
- 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) {
- ShutDownCLient(IO);
-/// TODO: this is a timeout??? sock_buff_invoke_free(sb, 0); seems as if socket is gone then?
+ IO->Timeout(IO); /* this is a timeout... */
return;
} else if (nbytes == -1) {
/// TODO: FD is gone. kick it. sock_buff_invoke_free(sb, errno);
}
}
+
+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;
+ }
+}
+
+int event_connect_socket(AsyncIO *IO, int conn_timeout, int 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);
+*/
+ 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;
+
+ memset( (struct sockaddr_in *)&saddr, '\0', sizeof( saddr ) );
+
+ memcpy(&saddr.sin_addr,
+ IO->HEnt->h_addr_list[0],
+ sizeof(struct in_addr));
+
+ 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 (rc >= 0){
+//// freeaddrinfo(res);
+ ev_timer_init(&IO->conn_timeout, IO_Timout_callback, first_rw_timeout, 0);
+ IO->conn_timeout.data = IO;
+ set_start_callback(event_base, IO, 0);
+ ev_timer_start(event_base, &IO->conn_timeout);
+ return 0;
+ }
+ else if (errno == EINPROGRESS) {
+ set_start_callback(event_base, IO, 0);
+ ev_timer_init(&IO->conn_timeout,
+ IO_Timout_callback,
+ conn_timeout + first_rw_timeout, 0);
+ ev_timer_start(event_base, &IO->conn_fail);
+ ev_timer_start(event_base, &IO->conn_timeout);
+
+ return 0;
+ }
+ else {
+ CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
+ StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
+/// close(IO->sock);
+/* IO->curr_ai = IO->curr_ai->ai_next;
+ if (IO->curr_ai != NULL)
+ return event_connect_socket(IO);
+ else*/
+ return -1;
+ }
+}
+
+void SetNextTimeout(AsyncIO *IO, int timeout)
+{
+ ev_timer_init(&IO->conn_timeout,
+ IO_Timout_callback,
+ timeout, 0);
+ ev_timer_again (event_base, &IO->conn_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,
+ int conn_timeout,
+ int first_rw_timeout,
int ReadFirst)
{
IO->Data = pData;
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);
-
+ IO->ConnFail = ConnFail;
+
if (ReadFirst) {
IO->NextState = eReadMessage;
- event_add(&IO->recv_event, NULL);
- IO->active_event = RECV_EVENT;
}
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);
}