2010-01-07 19:55:13 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2010-01-19 13:53:47 +00:00
|
|
|
* Copyright (C) 2007-2010, Jan Vidar Krey
|
2010-01-07 19:55:13 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-18 14:55:40 +00:00
|
|
|
#include "uhub.h"
|
|
|
|
|
2010-01-07 19:55:13 +00:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
|
2010-01-18 14:55:40 +00:00
|
|
|
#include "network/connection.h"
|
|
|
|
#include "network/common.h"
|
2010-01-19 13:53:47 +00:00
|
|
|
#include "network/backend.h"
|
2010-01-18 14:55:40 +00:00
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
#define EPOLL_EVBUFFER 512
|
2010-01-07 19:55:13 +00:00
|
|
|
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
NET_CON_STRUCT_COMMON
|
|
|
|
struct epoll_event ev;
|
2010-01-07 19:55:13 +00:00
|
|
|
};
|
|
|
|
|
2010-01-18 18:34:18 +00:00
|
|
|
struct net_backend
|
|
|
|
{
|
|
|
|
int epfd;
|
|
|
|
size_t num;
|
|
|
|
size_t max;
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll** conns;
|
2010-01-18 18:34:18 +00:00
|
|
|
struct epoll_event events[EPOLL_EVBUFFER];
|
|
|
|
time_t now;
|
|
|
|
struct timeout_queue timeout_queue;
|
2010-01-21 00:12:57 +00:00
|
|
|
struct net_cleanup_handler* cleaner;
|
2010-01-18 18:34:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct net_backend* g_backend = 0;
|
|
|
|
|
2010-01-20 16:26:23 +00:00
|
|
|
static void net_con_print(const char* prefix, struct net_connection_epoll* con)
|
2010-01-18 14:55:40 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2010-01-18 18:34:18 +00:00
|
|
|
sprintf(buf + off, ", timeout={ %d seconds left }", (int) (con->timeout->timestamp - g_backend->now));
|
2010-01-18 14:55:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(buf + off, ", timeout=NULL");
|
|
|
|
}
|
2010-01-18 18:34:18 +00:00
|
|
|
LOG_TRACE(buf);
|
2010-01-18 14:55:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2010-01-20 16:26:23 +00:00
|
|
|
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection_epoll*) * max);
|
2010-01-17 21:59:51 +00:00
|
|
|
memset(g_backend->events, 0, sizeof(g_backend->events));
|
2010-01-18 14:55:40 +00:00
|
|
|
|
|
|
|
g_backend->now = time(0);
|
|
|
|
timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 600); /* look max 10 minutes into the future. */
|
2010-01-21 00:12:57 +00:00
|
|
|
g_backend->cleaner = net_cleanup_initialize(max);
|
2010-01-17 21:59:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown the network connection backend.
|
|
|
|
*/
|
|
|
|
void net_backend_shutdown()
|
|
|
|
{
|
|
|
|
close(g_backend->epfd);
|
2010-01-22 22:18:59 +00:00
|
|
|
timeout_queue_shutdown(&g_backend->timeout_queue);
|
2010-01-21 00:12:57 +00:00
|
|
|
net_cleanup_shutdown(g_backend->cleaner);
|
2010-01-17 21:59:51 +00:00
|
|
|
hub_free(g_backend->conns);
|
|
|
|
hub_free(g_backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the network backend.
|
|
|
|
*/
|
|
|
|
int net_backend_process()
|
|
|
|
{
|
|
|
|
int n;
|
2010-01-22 22:18:59 +00:00
|
|
|
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
|
|
|
|
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu, seconds=%d", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), (int) secs);
|
|
|
|
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), secs * 1000);
|
2010-01-22 23:43:38 +00:00
|
|
|
|
|
|
|
g_backend->now = time(0);
|
|
|
|
timeout_queue_process(&g_backend->timeout_queue, g_backend->now);
|
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
if (res == -1)
|
|
|
|
{
|
2010-01-18 14:55:40 +00:00
|
|
|
LOG_WARN("epoll_wait returned -1");
|
2010-01-17 21:59:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < res; n++)
|
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll* con = (struct net_connection_epoll*) g_backend->events[n].data.ptr;
|
2010-01-18 14:55:40 +00:00
|
|
|
int ev = 0;
|
2010-01-18 18:34:18 +00:00
|
|
|
if (g_backend->events[n].events & EPOLLIN) ev |= NET_EVENT_READ;
|
2010-01-18 14:55:40 +00:00
|
|
|
if (g_backend->events[n].events & EPOLLOUT) ev |= NET_EVENT_WRITE;
|
2010-01-20 16:26:23 +00:00
|
|
|
net_con_callback((struct net_connection*) con, ev);
|
2010-01-17 21:59:51 +00:00
|
|
|
}
|
2010-01-21 00:12:57 +00:00
|
|
|
|
|
|
|
net_cleanup_process(g_backend->cleaner);
|
2010-01-17 21:59:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-20 16:26:23 +00:00
|
|
|
struct timeout_queue* net_backend_get_timeout_queue()
|
|
|
|
{
|
|
|
|
return &g_backend->timeout_queue;
|
|
|
|
}
|
2010-01-17 21:59:51 +00:00
|
|
|
|
2010-01-07 19:55:13 +00:00
|
|
|
struct net_connection* net_con_create()
|
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_epoll));
|
2010-01-19 13:53:47 +00:00
|
|
|
con->sd = -1;
|
2010-01-07 19:55:13 +00:00
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_con_destroy(struct net_connection* con)
|
|
|
|
{
|
|
|
|
hub_free(con);
|
|
|
|
}
|
|
|
|
|
2010-01-20 16:26:23 +00:00
|
|
|
void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr, int events)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
2010-01-07 19:55:13 +00:00
|
|
|
con->sd = sd;
|
2010-01-21 00:12:57 +00:00
|
|
|
con->flags = 0;
|
2010-01-07 19:55:13 +00:00
|
|
|
con->callback = callback;
|
|
|
|
con->ev.events = 0;
|
2010-01-18 14:55:40 +00:00
|
|
|
con->ptr = (void*) ptr;
|
|
|
|
con->ev.data.ptr = (void*) con;
|
2010-01-07 19:55:13 +00:00
|
|
|
|
|
|
|
net_set_nonblocking(con->sd, 1);
|
|
|
|
net_set_nosigpipe(con->sd, 1);
|
|
|
|
|
|
|
|
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
|
|
|
|
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
|
2010-01-17 21:59:51 +00:00
|
|
|
|
|
|
|
g_backend->conns[sd] = con;
|
|
|
|
g_backend->num++;
|
|
|
|
|
|
|
|
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_ADD, con->sd, &con->ev) == -1)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-18 18:34:18 +00:00
|
|
|
LOG_TRACE("epoll_ctl() add failed.");
|
2010-01-07 19:55:13 +00:00
|
|
|
}
|
2010-01-18 14:55:40 +00:00
|
|
|
|
|
|
|
net_con_print("ADD", con);
|
2010-01-07 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
|
|
|
|
{
|
|
|
|
con->callback = callback;
|
2010-01-18 14:55:40 +00:00
|
|
|
con->ptr = (void*) ptr;
|
2010-01-07 19:55:13 +00:00
|
|
|
net_con_update(con, events);
|
|
|
|
}
|
|
|
|
|
2010-01-20 16:26:23 +00:00
|
|
|
void net_con_update(struct net_connection* con_, int events)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
2010-01-22 23:03:45 +00:00
|
|
|
int newev = 0;
|
|
|
|
if (events & NET_EVENT_READ) newev |= EPOLLIN;
|
|
|
|
if (events & NET_EVENT_WRITE) newev |= EPOLLOUT;
|
|
|
|
|
|
|
|
if (newev == con->ev.events)
|
|
|
|
return;
|
2010-01-07 19:55:13 +00:00
|
|
|
|
2010-01-22 23:03:45 +00:00
|
|
|
con->ev.events = newev;
|
2010-01-17 21:59:51 +00:00
|
|
|
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-18 18:34:18 +00:00
|
|
|
LOG_TRACE("epoll_ctl() modify failed.");
|
2010-01-07 19:55:13 +00:00
|
|
|
}
|
2010-01-18 14:55:40 +00:00
|
|
|
net_con_print("MOD", con);
|
2010-01-07 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 00:12:57 +00:00
|
|
|
void net_con_close(struct net_connection* con_)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
2010-01-20 16:26:23 +00:00
|
|
|
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
2010-01-21 00:12:57 +00:00
|
|
|
if (con->flags & NET_CLEANUP)
|
|
|
|
return;
|
2010-01-17 21:59:51 +00:00
|
|
|
|
|
|
|
if (con->sd != -1)
|
|
|
|
{
|
|
|
|
g_backend->conns[con->sd] = 0;
|
|
|
|
g_backend->num--;
|
|
|
|
}
|
|
|
|
|
2010-01-21 21:56:07 +00:00
|
|
|
net_con_clear_timeout(con_);
|
2010-01-18 14:55:40 +00:00
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_DEL, con->sd, &con->ev) == -1)
|
2010-01-07 19:55:13 +00:00
|
|
|
{
|
|
|
|
LOG_WARN("epoll_ctl() delete failed.");
|
|
|
|
}
|
2010-01-21 22:45:13 +00:00
|
|
|
|
|
|
|
net_close(con->sd);
|
|
|
|
con->sd = -1;
|
|
|
|
|
2010-01-18 14:55:40 +00:00
|
|
|
net_con_print("DEL", con);
|
2010-01-21 00:12:57 +00:00
|
|
|
net_cleanup_delayed_free(g_backend->cleaner, con_);
|
2010-01-07 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
#endif /* USE_EPOLL */
|