2010-01-20 16:26:23 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2007-2010, Jan Vidar Krey
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
|
|
|
|
#include "network/connection.h"
|
|
|
|
#include "network/common.h"
|
|
|
|
#include "network/backend.h"
|
|
|
|
|
|
|
|
struct net_connection_select
|
|
|
|
{
|
|
|
|
NET_CON_STRUCT_COMMON
|
|
|
|
};
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_select
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
|
|
|
struct net_connection_select** conns;
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2011-02-05 16:11:23 +00:00
|
|
|
fd_set xfds;
|
2010-02-10 20:57:27 +00:00
|
|
|
int maxfd;
|
|
|
|
struct net_backend_common* common;
|
2010-01-20 16:26:23 +00:00
|
|
|
};
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
static void net_backend_set_handlers(struct net_backend_handler* handler);
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
const char* net_backend_name_select()
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
return "select";
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
int net_backend_poll_select(struct net_backend* data, int ms)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2011-01-02 01:39:25 +00:00
|
|
|
int res;
|
|
|
|
size_t n, found;
|
2010-01-22 22:18:59 +00:00
|
|
|
struct timeval tval;
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_select* backend = (struct net_backend_select*) data;
|
2010-01-28 00:11:11 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
tval.tv_sec = ms / 1000;
|
2012-04-17 08:57:10 +00:00
|
|
|
tval.tv_usec = (ms % 1000) * 1000;
|
2010-01-22 22:18:59 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
FD_ZERO(&backend->rfds);
|
|
|
|
FD_ZERO(&backend->wfds);
|
2011-02-05 16:11:23 +00:00
|
|
|
FD_ZERO(&backend->xfds);
|
2010-01-22 22:18:59 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
backend->maxfd = -1;
|
|
|
|
for (n = 0, found = 0; found < backend->common->num && n < backend->common->max; n++)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_connection_select* con = backend->conns[n];
|
2010-01-20 16:26:23 +00:00
|
|
|
if (con)
|
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
if (con->flags & NET_EVENT_READ) FD_SET(con->sd, &backend->rfds);
|
|
|
|
if (con->flags & NET_EVENT_WRITE) FD_SET(con->sd, &backend->wfds);
|
2010-01-20 16:26:23 +00:00
|
|
|
found++;
|
2010-02-10 20:57:27 +00:00
|
|
|
backend->maxfd = con->sd;
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-10 20:57:27 +00:00
|
|
|
backend->maxfd++;
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2011-02-05 16:11:23 +00:00
|
|
|
res = select(backend->maxfd, &backend->rfds, &backend->wfds, &backend->xfds, &tval);
|
|
|
|
if (res == -1)
|
|
|
|
{
|
|
|
|
printf("Error: %d\n", net_error());
|
|
|
|
}
|
|
|
|
if (res == -1 && net_error() == EINTR)
|
2010-01-20 16:26:23 +00:00
|
|
|
return 0;
|
2011-02-05 16:11:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_backend_process_select(struct net_backend* data, int res)
|
|
|
|
{
|
|
|
|
int n, found;
|
|
|
|
struct net_backend_select* backend = (struct net_backend_select*) data;
|
|
|
|
for (n = 0, found = 0; found < res && n < backend->maxfd; n++)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_connection_select* con = backend->conns[n];
|
2010-01-20 16:26:23 +00:00
|
|
|
if (con)
|
|
|
|
{
|
|
|
|
int ev = 0;
|
2010-02-10 20:57:27 +00:00
|
|
|
if (FD_ISSET(con->sd, &backend->rfds)) ev |= NET_EVENT_READ;
|
|
|
|
if (FD_ISSET(con->sd, &backend->wfds)) ev |= NET_EVENT_WRITE;
|
2010-01-20 16:26:23 +00:00
|
|
|
|
|
|
|
if (ev)
|
|
|
|
{
|
|
|
|
net_con_callback((struct net_connection*) con, ev);
|
|
|
|
found++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_connection* net_con_create_select(struct net_backend* data)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
|
|
|
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_select));
|
|
|
|
con->sd = -1;
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_initialize_select(struct net_backend* data, struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
|
|
|
struct net_connection_select* con = (struct net_connection_select*) con_;
|
|
|
|
con->sd = sd;
|
2010-02-10 20:57:27 +00:00
|
|
|
con->flags = 0;
|
2010-01-20 16:26:23 +00:00
|
|
|
con->callback = callback;
|
|
|
|
con->ptr = (void*) ptr;
|
2010-02-10 20:57:27 +00:00
|
|
|
}
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_add_select(struct net_backend* data, struct net_connection* con, int events)
|
|
|
|
{
|
|
|
|
struct net_backend_select* backend = (struct net_backend_select*) data;
|
|
|
|
backend->conns[con->sd] = (struct net_connection_select*) con;
|
2011-02-05 16:11:23 +00:00
|
|
|
con->flags |= (events & (NET_EVENT_READ | NET_EVENT_WRITE));
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:11:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_mod_select(struct net_backend* data, struct net_connection* con, int events)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2011-02-05 16:11:23 +00:00
|
|
|
con->flags |= (events & (NET_EVENT_READ | NET_EVENT_WRITE));
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_del_select(struct net_backend* data, struct net_connection* con)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_select* backend = (struct net_backend_select*) data;
|
|
|
|
backend->conns[con->sd] = 0;
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_backend_shutdown_select(struct net_backend* data)
|
2010-01-20 16:26:23 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_select* backend = (struct net_backend_select*) data;
|
|
|
|
hub_free(backend->conns);
|
|
|
|
hub_free(backend);
|
|
|
|
}
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend* net_backend_init_select(struct net_backend_handler* handler, struct net_backend_common* common)
|
|
|
|
{
|
|
|
|
struct net_backend_select* backend;
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
if (getenv("EVENT_NOSELECT"))
|
|
|
|
return 0;
|
2010-01-20 16:26:23 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
backend = hub_malloc_zero(sizeof(struct net_backend_select));
|
|
|
|
FD_ZERO(&backend->rfds);
|
|
|
|
FD_ZERO(&backend->wfds);
|
|
|
|
backend->conns = hub_malloc_zero(sizeof(struct net_connection_select*) * common->max);
|
|
|
|
backend->common = common;
|
|
|
|
net_backend_set_handlers(handler);
|
|
|
|
return (struct net_backend*) backend;
|
|
|
|
}
|
2010-01-21 22:45:13 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
static void net_backend_set_handlers(struct net_backend_handler* handler)
|
|
|
|
{
|
|
|
|
handler->backend_name = net_backend_name_select;
|
|
|
|
handler->backend_poll = net_backend_poll_select;
|
|
|
|
handler->backend_process = net_backend_process_select;
|
|
|
|
handler->backend_shutdown = net_backend_shutdown_select;
|
|
|
|
handler->con_create = net_con_create_select;
|
|
|
|
handler->con_init = net_con_initialize_select;
|
|
|
|
handler->con_add = net_con_backend_add_select;
|
|
|
|
handler->con_mod = net_con_backend_mod_select;
|
|
|
|
handler->con_del = net_con_backend_del_select;
|
2010-01-20 16:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_SELECT */
|