Move the timeout handling to the connection object.

This commit is contained in:
Jan Vidar Krey 2009-08-03 21:57:18 +02:00
parent 4a5993ccc2
commit fbe98d6011
9 changed files with 89 additions and 60 deletions

View File

@ -141,7 +141,7 @@ int hub_handle_support(struct hub_info* hub, struct hub_user* u, struct adc_mess
if (ok)
{
hub_send_handshake(hub, u);
user_set_timeout(u, TIMEOUT_HANDSHAKE);
net_con_set_timeout(&u->net.connection, TIMEOUT_HANDSHAKE);
}
else
{

View File

@ -68,8 +68,8 @@ void on_login_success(struct hub_info* hub, struct hub_user* u)
if (user_is_logged_in(u)) /* Previous send() can fail! */
hub_send_motd(hub, u);
/* reset to idle timeout */
user_set_timeout(u, TIMEOUT_IDLE);
/* reset timeout */
net_con_clear_timeout(&u->net.connection);
}
void on_login_failure(struct hub_info* hub, struct hub_user* u, enum status_message msg)

View File

@ -182,33 +182,38 @@ int handle_net_write(struct hub_user* user)
return 0;
}
void net_event(int fd, short ev, void *arg)
void net_event(struct net_connection* con, int event, void *arg)
{
struct hub_user* user = (struct hub_user*) arg;
int flag_close = 0;
#ifdef DEBUG_SENDQ
LOG_TRACE("net_on_read() : fd=%d, ev=%d, arg=%p", fd, (int) ev, arg);
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg);
#endif
if (ev & EV_TIMEOUT)
if (event == NET_EVENT_SOCKERROR)
{
hub_disconnect_user(g_hub, user, quit_socket_error);
return;
}
else if (event == NET_EVENT_CLOSED)
{
hub_disconnect_user(g_hub, user, quit_disconnected);
return;
}
else if (event == NET_EVENT_TIMEOUT)
{
if (user_is_connecting(user))
{
flag_close = quit_timeout;
}
else
{
// FIXME: hub is not neccesarily set!
// hub_send_ping(hub, user);
hub_disconnect_user(g_hub, user, quit_timeout);
return;
}
}
if (ev & EV_READ)
else if (event & NET_EVENT_READ)
{
flag_close = handle_net_read(user);
}
else if (ev & EV_WRITE)
else if (event & NET_EVENT_WRITE)
{
flag_close = handle_net_write(user);
}
@ -220,7 +225,7 @@ void net_event(int fd, short ev, void *arg)
}
}
void net_on_accept(int server_fd, short ev, void *arg)
void net_on_accept(int server_fd, short event, void *arg)
{
struct hub_info* hub = (struct hub_info*) arg;
struct hub_user* user = 0;

View File

@ -25,7 +25,7 @@
*/
extern void net_on_accept(int fd, short ev, void *arg);
extern void net_event(int fd, short ev, void *arg);
extern void net_event(struct net_connection* con, int event, void *arg);
extern int handle_net_read(struct hub_user* user);
extern int handle_net_write(struct hub_user* user);

View File

@ -50,12 +50,8 @@ struct hub_user* user_create(struct hub_info* hub, int sd, struct ip_addr_encap*
user->net.send_queue = hub_sendq_create();
user->net.recv_queue = hub_recvq_create();
net_con_initialize(&user->net.connection, sd, addr, user, EV_READ);
evtimer_set(&user->net.timeout, net_event, user);
event_base_set(hub->evbase, &user->net.timeout);
user_set_timeout(user, TIMEOUT_CONNECTED);
net_con_initialize(&user->net.connection, sd, addr, net_event, user, EV_READ);
net_con_set_timeout(&user->net.connection, TIMEOUT_CONNECTED);
user_set_state(user, state_protocol);
return user;
@ -67,7 +63,7 @@ void user_destroy(struct hub_user* user)
LOG_TRACE("user_destroy(), user=%p", user);
net_con_close(&user->net.connection);
evtimer_del(&user->net.timeout);
net_con_clear_timeout(&user->net.connection);
hub_recvq_destroy(user->net.recv_queue);
hub_sendq_destroy(user->net.send_queue);
@ -335,15 +331,6 @@ void user_net_io_want_read(struct hub_user* user)
net_con_update(&user->net.connection, EV_READ);
}
void user_set_timeout(struct hub_user* user, int seconds)
{
#ifdef DEBUG_SENDQ
LOG_TRACE("user_set_timeout to %d seconds: %s", seconds, user_log_str(user));
#endif
struct timeval timeout = { seconds, 0 };
evtimer_add(&user->net.timeout, &timeout);
}
const char* user_get_quit_reason_string(enum user_quit_reason reason)
{
switch (reason)

View File

@ -100,7 +100,6 @@ struct hub_user_limits
struct hub_user_net_io
{
struct net_connection connection; /** Connection data */
struct event timeout; /** timeout handling */
struct hub_recvq* recv_queue;
struct hub_sendq* send_queue;
time_t tm_connected; /** time when user connected */
@ -267,12 +266,6 @@ extern void user_net_io_want_write(struct hub_user* user);
*/
extern void user_net_io_want_read(struct hub_user* user);
/**
* Set timeout for connetion.
* @param seconds the number of seconds into the future.
*/
extern void user_set_timeout(struct hub_user* user, int seconds);
#endif /* HAVE_UHUB_USER_H */

View File

@ -39,54 +39,61 @@ static inline void net_con_flag_unset(struct net_connection* con, unsigned int f
static void net_con_event(int fd, short ev, void *arg)
{
struct net_connection* con = (struct net_connection*) arg;
int events = 0;
if (ev & EV_READ) events |= NET_EVENT_READ;
if (ev & EV_WRITE) events |= NET_EVENT_WRITE;
if (ev == EV_TIMEOUT) events = NET_EVENT_TIMEOUT;
#ifdef SSL_SUPPORT
if (!con->ssl)
{
#endif
net_event(fd, ev, con->ptr);
con->callback(con, events, con->ptr);
#ifdef SSL_SUPPORT
}
else
{
LOG_DUMP("net_con_event: ev=%d, con=%p", ev, con);
LOG_DUMP("net_con_event: events=%d, con=%p", ev, con);
if (ev & (EV_READ | EV_WRITE))
{
if (net_con_flag_get(con, NET_WANT_SSL_ACCEPT))
{
if (net_con_ssl_accept(con) < 0)
net_event(fd, EV_READ, con->ptr);
net_event(con, NET_EVENT_SOCKERROR, con->ptr);
}
else if (net_con_flag_get(con, NET_WANT_SSL_CONNECT))
{
if (net_con_ssl_connect(con) < 0)
net_event(fd, EV_READ, con->ptr);
con->callback(con, NET_EVENT_SOCKERROR, con->ptr);
}
else if (ev == EV_READ && net_con_flag_get(con, NET_WANT_SSL_READ))
{
net_event(fd, EV_WRITE, con->ptr);
con->callback(con, NET_EVENT_WRITE, con->ptr);
}
else if (ev == EV_WRITE && net_con_flag_get(con, NET_WANT_SSL_WRITE))
{
net_event(fd, ev & EV_READ, con->ptr);
con->callback(con, events & NET_EVENT_READ, con->ptr);
}
else
{
net_event(fd, ev, con->ptr);
con->callback(con, events, con->ptr);
}
}
else
{
net_event(fd, ev, con->ptr);
con->callback(con, events, con->ptr);
}
}
#endif
}
void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap* addr, const void* ptr, int events)
void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap* addr, net_connection_cb callback, const void* ptr, int events)
{
LOG_DUMP("net_con_initialize: sd=%d, ptr=%p", sd, ptr);
con->sd = sd;
con->ptr = (void*) ptr;
con->callback = callback;
con->last_send = time(0);
con->last_recv = con->last_send;
@ -280,6 +287,24 @@ ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
#endif
}
void net_con_set_timeout(struct net_connection* con, int seconds)
{
struct timeval timeout = { seconds, 0 };
evtimer_set(&con->timeout, net_con_event, con);
event_base_set(g_hub->evbase, &con->timeout);
evtimer_add(&con->timeout, &timeout);
}
void net_con_clear_timeout(struct net_connection* con)
{
if (evtimer_pending(&con->timeout, 0))
{
evtimer_del(&con->timeout);
memset(&con->timeout, 0, sizeof(&con->timeout));
}
}
#ifdef SSL_SUPPORT
ssize_t net_con_ssl_accept(struct net_connection* con)
{

View File

@ -22,12 +22,17 @@
#include "uhub.h"
struct net_connection;
typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr);
struct net_connection
{
int sd; /** socket descriptor */
unsigned int flags; /** Connection flags */
void* ptr; /** data pointer */
net_connection_cb callback; /** Callback function */
struct event event; /** libevent struct for read/write events */
struct event timeout; /** timeout handling */
struct ip_addr_encap ipaddr; /** IP address of peer */
time_t last_recv; /** Timestamp for last recv() */
time_t last_send; /** Timestamp for last send() */
@ -37,7 +42,7 @@ struct net_connection
#endif /* SSL_SUPPORT */
};
extern void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap*, const void* ptr, int events);
extern void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap*, net_connection_cb callback, const void* ptr, int events);
extern void net_con_update(struct net_connection* con, int events);
extern void net_con_close(struct net_connection* con);
@ -59,6 +64,14 @@ extern ssize_t net_con_send(struct net_connection* con, const void* buf, size_t
*/
extern ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len);
/**
* Set timeout for connetion.
*
* @param seconds the number of seconds into the future.
*/
extern void net_con_set_timeout(struct net_connection* con, int seconds);
extern void net_con_clear_timeout(struct net_connection* con);
#ifdef SSL_SUPPORT
/**
* Start SSL_accept()

View File

@ -32,14 +32,20 @@ struct net_statistics
struct net_socket_t;
#define NET_WANT_READ 0x01
#define NET_WANT_WRITE 0x02
#define NET_WANT_ACCEPT 0x08
#define NET_WANT_SSL_READ 0x10
#define NET_WANT_SSL_WRITE 0x20
#define NET_WANT_SSL_ACCEPT 0x40
#define NET_WANT_SSL_CONNECT 0x40
#define NET_WANT_SSL_X509_LOOKUP 0x80
#define NET_WANT_READ 0x0001
#define NET_WANT_WRITE 0x0002
#define NET_WANT_ACCEPT 0x0008
#define NET_WANT_SSL_READ 0x0010
#define NET_WANT_SSL_WRITE 0x0020
#define NET_WANT_SSL_ACCEPT 0x0040
#define NET_WANT_SSL_CONNECT 0x0080
#define NET_WANT_SSL_X509_LOOKUP 0x0100
#define NET_EVENT_TIMEOUT 0x0001
#define NET_EVENT_READ 0x0002
#define NET_EVENT_WRITE 0x0004
#define NET_EVENT_SOCKERROR 0x1000 /* Socket error, closed */
#define NET_EVENT_CLOSED 0x2000 /* Socket closed */
/**
* Initialize the socket monitor subsystem.