diff --git a/src/core/hubevent.c b/src/core/hubevent.c index 4078b45..1b493f7 100644 --- a/src/core/hubevent.c +++ b/src/core/hubevent.c @@ -69,7 +69,7 @@ void on_login_success(struct hub_info* hub, struct hub_user* u) hub_send_motd(hub, u); /* reset timeout */ - net_con_set_timeout(u->connection, TIMEOUT_IDLE); + net_con_clear_timeout(u->connection); } void on_login_failure(struct hub_info* hub, struct hub_user* u, enum status_message msg) diff --git a/src/network/epoll.c b/src/network/epoll.c index 7cdf594..5631f9d 100644 --- a/src/network/epoll.c +++ b/src/network/epoll.c @@ -36,22 +36,6 @@ struct net_connection struct timeout_evt* timeout; }; -static void net_con_print(const char* prefix, struct net_connection* con) -{ - char buf[512]; - int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, ev={ events=%s%s, data.ptr=%p }", - prefix, con->sd, con->flags, con->callback, con->ptr, (con->ev.events & EPOLLIN ? "R" : ""),(con->ev.events & EPOLLOUT ? "W" : "") , con->ev.data.ptr); - if (con->timeout) - { - sprintf(buf + off, ", timeout={ %d seconds left }", (int) (time(0) - con->timeout->timestamp)); - } - else - { - sprintf(buf + off, ", timeout=NULL"); - } - LOG_WARN(buf); -} - struct net_backend { int epfd; @@ -65,6 +49,23 @@ struct net_backend static struct net_backend* g_backend = 0; + +static void net_con_print(const char* prefix, struct net_connection* con) +{ + char buf[512]; + int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, ev={ events=%s%s, data.ptr=%p }", + prefix, con->sd, con->flags, con->callback, con->ptr, (con->ev.events & EPOLLIN ? "R" : ""),(con->ev.events & EPOLLOUT ? "W" : "") , con->ev.data.ptr); + if (con->timeout) + { + sprintf(buf + off, ", timeout={ %d seconds left }", (int) (con->timeout->timestamp - g_backend->now)); + } + else + { + sprintf(buf + off, ", timeout=NULL"); + } + LOG_TRACE(buf); +} + /** * Initialize the network backend. * Returns 1 on success, or 0 on failure. @@ -106,7 +107,7 @@ void net_backend_shutdown() int net_backend_process() { int n; - LOG_WARN("epoll_wait: fd=%d, events=%x, max=%zu", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER)); + LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER)); int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), 1000); if (res == -1) { @@ -118,7 +119,7 @@ int net_backend_process() { struct net_connection* con = (struct net_connection*) g_backend->events[n].data.ptr; int ev = 0; - if (g_backend->events[n].events & EPOLLIN) ev |= NET_EVENT_READ; + if (g_backend->events[n].events & EPOLLIN) ev |= NET_EVENT_READ; if (g_backend->events[n].events & EPOLLOUT) ev |= NET_EVENT_WRITE; con->callback(con, ev, con->ptr); } @@ -157,7 +158,7 @@ void net_con_initialize(struct net_connection* con, int sd, net_connection_cb ca if (epoll_ctl(g_backend->epfd, EPOLL_CTL_ADD, con->sd, &con->ev) == -1) { - LOG_WARN("epoll_ctl() add failed."); + LOG_TRACE("epoll_ctl() add failed."); } net_con_print("ADD", con); @@ -178,7 +179,7 @@ void net_con_update(struct net_connection* con, int events) if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1) { - LOG_WARN("epoll_ctl() modify failed."); + LOG_TRACE("epoll_ctl() modify failed."); } net_con_print("MOD", con); } @@ -313,5 +314,15 @@ void net_con_set_timeout(struct net_connection* con, int seconds) } } +void net_con_clear_timeout(struct net_connection* con) +{ + if (con->timeout && timeout_evt_is_scheduled(con->timeout)) + { + timeout_queue_remove(&g_backend->timeout_queue, con->timeout); + hub_free(con->timeout); + con->timeout = 0; + } +} + #endif /* USE_EPOLL */ diff --git a/src/util/timeout.c b/src/util/timeout.c index 58bfad5..ed21576 100644 --- a/src/util/timeout.c +++ b/src/util/timeout.c @@ -100,7 +100,8 @@ void timeout_queue_remove(struct timeout_queue* t, struct timeout_evt* evt) if (first == evt) { - first->next->prev = first->prev; + if (first->next) + first->next->prev = first->prev; t->events[pos] = first->next; } else