+ AsyncIO *IO = watcher->data;
+ IO->Now = ev_now(event_base);
+ EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
+ become_session(IO->CitContext);
+ assert(IO->DNS.Query->PostDNS);
+ switch (IO->DNS.Query->PostDNS(IO))
+ {
+ case eAbort:
+ assert(IO->DNS.Fail);
+ switch (IO->DNS.Fail(IO)) {
+ case eAbort:
+//// StopClientWatchers(IO);
+ ShutDownCLient(IO);
+ default:
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+
+eNextState EvConnectSock(AsyncIO *IO,
+ double conn_timeout,
+ double first_rw_timeout,
+ int ReadFirst)
+{
+ struct sockaddr_in egress_sin;
+ int fdflags;
+ int rc = -1;
+
+ become_session(IO->CitContext);
+
+ if (ReadFirst) {
+ IO->NextState = eReadMessage;
+ }
+ else {
+ IO->NextState = eSendReply;
+ }
+
+ IO->SendBuf.fd = IO->RecvBuf.fd =
+ socket(
+ (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
+ SOCK_STREAM,
+ IPPROTO_TCP);
+
+ if (IO->SendBuf.fd < 0) {
+ EV_syslog(LOG_ERR,
+ "EVENT: socket() failed: %s\n",
+ strerror(errno));
+
+ StrBufPrintf(IO->ErrMsg,
+ "Failed to create socket: %s",
+ strerror(errno));
+ IO->SendBuf.fd = IO->RecvBuf.fd = 0;
+ return eAbort;
+ }
+ fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
+ if (fdflags < 0) {
+ EV_syslog(LOG_DEBUG,
+ "EVENT: unable to get socket flags! %s \n",
+ strerror(errno));
+ StrBufPrintf(IO->ErrMsg,
+ "Failed to get socket flags: %s",
+ strerror(errno));
+ close(IO->SendBuf.fd);
+ IO->SendBuf.fd = IO->RecvBuf.fd = 0;
+ return eAbort;
+ }
+ fdflags = fdflags | O_NONBLOCK;
+ if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
+ EV_syslog(
+ LOG_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->SendBuf.fd);
+ IO->SendBuf.fd = IO->RecvBuf.fd = 0;
+ 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->RecvBuf.fd, EV_READ);
+ IO->recv_event.data = IO;
+ ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, 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_Timeout_callback, first_rw_timeout,0);
+ IO->rw_timeout.data = IO;
+
+
+