[go: up one dir, main page]

File: backend.c

package info (click to toggle)
uhub 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,016 kB
  • sloc: ansic: 12,630; xml: 588; sh: 356; perl: 233; makefile: 60
file content (220 lines) | stat: -rw-r--r-- 5,521 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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"

#include "network/connection.h"

struct net_backend;
struct net_connection;

struct net_cleanup_handler
{
	size_t num;
	size_t max;
	struct net_connection** queue;
};

struct net_backend
{
	struct net_backend_common common;
	time_t now; /* the time now (used for timeout handling) */
	struct timeout_queue timeout_queue; /* used for timeout handling */
	struct net_cleanup_handler* cleaner; /* handler to cleanup connections at a safe point */
	struct net_backend_handler handler; /* backend event handler */
	struct net_backend* data; /* backend specific data */
};

static struct net_backend* g_backend;


#ifdef USE_EPOLL
extern struct net_backend* net_backend_init_epoll(struct net_backend_handler*, struct net_backend_common*);
#endif

#ifdef USE_KQUEUE
extern struct net_backend* net_backend_init_kqueue(struct net_backend_handler*, struct net_backend_common*);
#endif

#ifdef USE_SELECT
extern struct net_backend* net_backend_init_select(struct net_backend_handler*, struct net_backend_common*);
#endif

static net_backend_init_t net_backend_init_funcs[] = {
#ifdef USE_EPOLL
	net_backend_init_epoll,
#endif
#ifdef USE_KQUEUE
	net_backend_init_kqueue,
#endif
#ifdef USE_SELECT
	net_backend_init_select,
#endif
	0
};

int net_backend_init()
{
	size_t n;
	g_backend = hub_malloc_zero(sizeof(struct net_backend));
	g_backend->common.num = 0;
	g_backend->common.max = net_get_max_sockets();
	g_backend->now = time(0);
	timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 120); /* FIXME: max 120 secs! */
	g_backend->cleaner = net_cleanup_initialize(g_backend->common.max);

	for (n = 0; net_backend_init_funcs[n]; n++)
	{
		g_backend->data = net_backend_init_funcs[n](&g_backend->handler, &g_backend->common);
		if (g_backend->data)
		{
			LOG_DEBUG("Initialized %s network backend.", g_backend->handler.backend_name());
			return 1;
		}
	}
	LOG_FATAL("Unable to find a suitable network backend");
	return 0;
}

void net_backend_shutdown()
{
	g_backend->handler.backend_shutdown(g_backend->data);
	timeout_queue_shutdown(&g_backend->timeout_queue);
	net_cleanup_shutdown(g_backend->cleaner);
	hub_free(g_backend);
	g_backend = 0;
}


void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
{
	con->callback = callback;
	con->ptr = (void*) ptr;
	net_con_update(con, events);
}

void net_con_update(struct net_connection* con, int events)
{
	g_backend->handler.con_mod(g_backend->data, con, events);
}

struct net_connection* net_con_create()
{
	return g_backend->handler.con_create(g_backend->data);
}

struct timeout_queue* net_backend_get_timeout_queue()
{
	if (!g_backend)
		return 0;
	return &g_backend->timeout_queue;
}


/**
 * Process the network backend.
 */
int net_backend_process()
{
	int res;
	size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);

	res = g_backend->handler.backend_poll(g_backend->data, secs * 1000);

	g_backend->now = time(0);
	timeout_queue_process(&g_backend->timeout_queue, g_backend->now);

	if (res == -1)
	{
		LOG_WARN("backend error.");
		return 0;
	}

	g_backend->handler.backend_process(g_backend->data, res);

	net_cleanup_process(g_backend->cleaner);
	return 1;
}

time_t net_get_time()
{
	return g_backend->now;
}


void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events)
{
	g_backend->handler.con_init(g_backend->data, con, sd, callback, ptr);

	net_set_nonblocking(con->sd, 1);
	net_set_nosigpipe(con->sd, 1);

	g_backend->handler.con_add(g_backend->data, con, events);
	g_backend->common.num++;
}

void net_con_close(struct net_connection* con)
{
	if (con->flags & NET_CLEANUP)
		return;

	g_backend->common.num--;
	net_con_clear_timeout(con);

	g_backend->handler.con_del(g_backend->data, con);

	net_close(con->sd);
	con->sd = -1;

	net_cleanup_delayed_free(g_backend->cleaner, con);
}

struct net_cleanup_handler* net_cleanup_initialize(size_t max)
{
	struct net_cleanup_handler* handler = (struct net_cleanup_handler*) hub_malloc(sizeof(struct net_cleanup_handler));
	handler->num = 0;
	handler->max = max;
	handler->queue = hub_malloc_zero(sizeof(struct net_connection*) * max);
	return handler;
}

void net_cleanup_shutdown(struct net_cleanup_handler* handler)
{
	hub_free(handler->queue);
	hub_free(handler);
}

void net_cleanup_delayed_free(struct net_cleanup_handler* handler, struct net_connection* con)
{
	handler->queue[handler->num++] = con;
	con->flags |= NET_CLEANUP;
}

void net_cleanup_process(struct net_cleanup_handler* handler)
{
	size_t n;
	for (n = 0; n < handler->num; n++)
	{
		struct net_connection* con = handler->queue[n];
		LOG_TRACE("net_cleanup_process: free: %p", con);
		hub_free(con);
	}
	handler->num = 0;
}