More work on epoll.
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
|
||||
#ifdef USE_EPOLL
|
||||
|
||||
static int g_epfd = -1;
|
||||
#define EPOLL_EVBUFFER 512
|
||||
|
||||
struct net_connection
|
||||
{
|
||||
@@ -28,8 +28,82 @@ struct net_connection
|
||||
net_connection_cb callback;
|
||||
struct epoll_event ev;
|
||||
struct timeout_evt* timeout;
|
||||
void* ptr;
|
||||
};
|
||||
|
||||
struct net_backend
|
||||
{
|
||||
int epfd;
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection** conns;
|
||||
struct epoll_event events[EPOLL_EVBUFFER];
|
||||
};
|
||||
|
||||
static struct net_backend* g_backend = 0;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
size_t max = net_get_max_sockets();
|
||||
g_backend = hub_malloc(sizeof(struct net_backend));
|
||||
g_backend->epfd = epoll_create(max);
|
||||
if (g_backend->epfd == -1)
|
||||
{
|
||||
LOG_WARN("Unable to create epoll socket.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_backend->num = 0;
|
||||
g_backend->max = max;
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection*) * max);
|
||||
memset(g_backend->events, 0, sizeof(g_backend->events));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
close(g_backend->epfd);
|
||||
hub_free(g_backend->conns);
|
||||
hub_free(g_backend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int n;
|
||||
int res = epoll_wait(g_backend->epfd, g_backend->events, EPOLL_EVBUFFER, 1000);
|
||||
if (res == -1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (n = 0; n < res; n++)
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) g_backend->events[n].data.ptr;
|
||||
if (con && con->callback)
|
||||
{
|
||||
con->callback(con, g_backend->events[n].events, con->ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_WARN("Con == NULL");
|
||||
}
|
||||
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection));
|
||||
@@ -55,7 +129,11 @@ void net_con_initialize(struct net_connection* con, int sd, net_connection_cb ca
|
||||
|
||||
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
|
||||
if (epoll_ctl(g_epfd, EPOLL_CTL_ADD, con->sd, &con->ev) == -1)
|
||||
|
||||
g_backend->conns[sd] = con;
|
||||
g_backend->num++;
|
||||
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_ADD, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_WARN("epoll_ctl() add failed.");
|
||||
}
|
||||
@@ -74,7 +152,7 @@ void net_con_update(struct net_connection* con, int events)
|
||||
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
|
||||
|
||||
if (epoll_ctl(g_epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_WARN("epoll_ctl() modify failed.");
|
||||
}
|
||||
@@ -86,7 +164,14 @@ int net_con_close(struct net_connection* con)
|
||||
return 0;
|
||||
|
||||
con->flags &= ~NET_INITIALIZED;
|
||||
if (epoll_ctl(g_epfd, EPOLL_CTL_DEL, con->sd, &con->ev) == -1)
|
||||
|
||||
if (con->sd != -1)
|
||||
{
|
||||
g_backend->conns[con->sd] = 0;
|
||||
g_backend->num--;
|
||||
}
|
||||
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_DEL, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_WARN("epoll_ctl() delete failed.");
|
||||
}
|
||||
@@ -180,4 +265,4 @@ void net_con_set_timeout(struct net_connection* con, int seconds)
|
||||
}
|
||||
|
||||
|
||||
#endif /* USE_EPOLL */
|
||||
#endif /* USE_EPOLL */
|
||||
|
||||
@@ -609,5 +609,46 @@ ssize_t net_con_ssl_handshake(struct net_connection* con, int ssl_mode)
|
||||
}
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
static struct event_base g_evbase = 0;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
g_evbase = event_init();
|
||||
if (!g_evbase)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize libevent.");
|
||||
return 0;
|
||||
}
|
||||
LOG_DEBUG("Using libevent %s, backend: %s", event_get_version(), event_get_method());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
event_base_free(g_evbase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int ret = event_base_loop(g_evbase, EVLOOP_ONCE);
|
||||
if (ret != 0)
|
||||
{
|
||||
LOG_DEBUG("event_base_loop returned: %d", (int) ret);
|
||||
}
|
||||
if (ret < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#endif /* USE_LIBEVENT */
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
struct net_connection;
|
||||
struct net_timer;
|
||||
struct net_backend;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
@@ -42,6 +43,12 @@ extern int net_backend_initialize();
|
||||
*/
|
||||
extern void net_backend_shutdown();
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
extern int net_backend_process();
|
||||
|
||||
|
||||
typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr);
|
||||
typedef void (*net_timeout_cb)(struct net_timer*, void* ptr);
|
||||
|
||||
|
||||
@@ -21,9 +21,7 @@
|
||||
|
||||
static int is_ipv6_supported = -1; /* -1 = CHECK, 0 = NO, 1 = YES */
|
||||
static int net_initialized = 0;
|
||||
#ifdef USE_LIBEVENT
|
||||
static struct event_base* net_evbase = 0;
|
||||
#endif
|
||||
|
||||
static struct net_statistics stats;
|
||||
static struct net_statistics stats_total;
|
||||
|
||||
@@ -49,16 +47,7 @@ int net_initialize()
|
||||
}
|
||||
#endif /* WINSOCK */
|
||||
|
||||
#ifdef USE_LIBEVENT
|
||||
net_evbase = event_init();
|
||||
if (!net_evbase)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize libevent.");
|
||||
return 0;
|
||||
}
|
||||
LOG_DEBUG("Using libevent %s, backend: %s", event_get_version(), event_get_method());
|
||||
#endif
|
||||
|
||||
net_backend_initialize();
|
||||
net_stats_initialize();
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
@@ -102,11 +91,8 @@ int net_destroy()
|
||||
{
|
||||
LOG_TRACE("Shutting down network monitor");
|
||||
|
||||
#ifdef USE_LIBEVENT
|
||||
event_base_free(net_evbase);
|
||||
net_evbase = 0;
|
||||
#endif
|
||||
|
||||
net_backend_shutdown();
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
/* FIXME: Shutdown OpenSSL here. */
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user