summaryrefslogtreecommitdiff
path: root/pjlib-util/src/pjstun-srv-test/usage.c
blob: 198e0313bd04382777410027cc1c0d69eb5cd342 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* $Id$ */
/* 
 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include "server.h"

struct worker
{
    pj_ioqueue_op_key_t	read_key;
    unsigned		index;
    pj_uint8_t		readbuf[4000];
    pj_sockaddr		src_addr;
    int			src_addr_len;
};

struct pj_stun_usage
{
    pj_pool_t		*pool;
    pj_stun_server	*srv;
    pj_mutex_t		*mutex;
    pj_stun_usage_cb	 cb;
    int			 type;
    pj_sock_t		 sock;
    pj_ioqueue_key_t	*key;
    unsigned		 worker_cnt;
    struct worker	*worker;

    pj_ioqueue_op_key_t	*send_key;
    unsigned		 send_count, send_index;

    pj_bool_t		 quitting;
    void		*user_data;
};


static void on_read_complete(pj_ioqueue_key_t *key, 
                             pj_ioqueue_op_key_t *op_key, 
                             pj_ssize_t bytes_read);

/*
 * Create STUN usage.
 */
PJ_DEF(pj_status_t) pj_stun_usage_create( pj_stun_server *srv,
					  const char *name,
					  const pj_stun_usage_cb *cb,
					  int family,
					  int type,
					  int protocol,
					  const pj_sockaddr_t *local_addr,
					  int addr_len,
					  pj_stun_usage **p_usage)
{
    pj_stun_server_info *si;
    pj_pool_t *pool;
    pj_stun_usage *usage;
    pj_ioqueue_callback ioqueue_cb;
    unsigned i;
    pj_status_t status;

    si = pj_stun_server_get_info(srv);

    pool = pj_pool_create(si->pf, name, 4000, 4000, NULL);
    usage = PJ_POOL_ZALLOC_T(pool, pj_stun_usage);
    usage->pool = pool;
    usage->srv = srv;

    status = pj_mutex_create_simple(pool, name, &usage->mutex);
    if (status != PJ_SUCCESS)
	goto on_error;

    pj_memcpy(&usage->cb, cb, sizeof(*cb));

    usage->type = type;
    status = pj_sock_socket(family, type, protocol, &usage->sock);
    if (status != PJ_SUCCESS)
	goto on_error;

    status = pj_sock_bind(usage->sock, local_addr, addr_len);
    if (status != PJ_SUCCESS)
	goto on_error;

    pj_bzero(&ioqueue_cb, sizeof(ioqueue_cb));
    ioqueue_cb.on_read_complete = &on_read_complete;
    status = pj_ioqueue_register_sock(usage->pool, si->ioqueue, usage->sock,
				      usage, &ioqueue_cb, &usage->key);
    if (status != PJ_SUCCESS)
	goto on_error;

    usage->worker_cnt = si->thread_cnt;
    usage->worker = pj_pool_calloc(pool, si->thread_cnt, 
				   sizeof(struct worker));
    for (i=0; i<si->thread_cnt; ++i) {
	pj_ioqueue_op_key_init(&usage->worker[i].read_key, 
			       sizeof(usage->worker[i].read_key));
	usage->worker[i].index = i;
    }

    usage->send_count = usage->worker_cnt * 2;
    usage->send_key = pj_pool_calloc(pool, usage->send_count, 
				     sizeof(pj_ioqueue_op_key_t));
    for (i=0; i<usage->send_count; ++i) {
	pj_ioqueue_op_key_init(&usage->send_key[i], 
			       sizeof(usage->send_key[i]));
    }

    for (i=0; i<si->thread_cnt; ++i) {
	pj_ssize_t size;

	size = sizeof(usage->worker[i].readbuf);
	usage->worker[i].src_addr_len = sizeof(usage->worker[i].src_addr);
	status = pj_ioqueue_recvfrom(usage->key, &usage->worker[i].read_key,
				     usage->worker[i].readbuf, &size, 
				     PJ_IOQUEUE_ALWAYS_ASYNC,
				     &usage->worker[i].src_addr,
				     &usage->worker[i].src_addr_len);
	if (status != PJ_EPENDING)
	    goto on_error;
    }

    pj_stun_server_register_usage(srv, usage);

    *p_usage = usage;
    return PJ_SUCCESS;

on_error:
    pj_stun_usage_destroy(usage);
    return status;
}


/**
 * Destroy usage.
 */
PJ_DEF(pj_status_t) pj_stun_usage_destroy(pj_stun_usage *usage)
{
    pj_stun_server_unregister_usage(usage->srv, usage);
    if (usage->cb.on_destroy)
	(*usage->cb.on_destroy)(usage);

    if (usage->key) {
	pj_ioqueue_unregister(usage->key);
	usage->key = NULL;
	usage->sock = PJ_INVALID_SOCKET;
    } else if (usage->sock != 0 && usage->sock != PJ_INVALID_SOCKET) {
	pj_sock_close(usage->sock);
	usage->sock = PJ_INVALID_SOCKET;
    }

    if (usage->mutex) {
	pj_mutex_destroy(usage->mutex);
	usage->mutex = NULL;
    }

    if (usage->pool) {
	pj_pool_t *pool = usage->pool;
	usage->pool = NULL;
	pj_pool_release(pool);
    }

    return PJ_SUCCESS;
}


/**
 * Set user data.
 */
PJ_DEF(pj_status_t) pj_stun_usage_set_user_data( pj_stun_usage *usage,
						 void *user_data)
{
    usage->user_data = user_data;
    return PJ_SUCCESS;
}

/**
 * Get user data.
 */
PJ_DEF(void*) pj_stun_usage_get_user_data(pj_stun_usage *usage)
{
    return usage->user_data;
}


/**
 * Send with the usage.
 */
PJ_DEF(pj_status_t) pj_stun_usage_sendto( pj_stun_usage *usage,
					  const void *pkt,
					  pj_size_t pkt_size,
					  unsigned flags,
					  const pj_sockaddr_t *dst_addr,
					  unsigned addr_len)
{
    pj_ssize_t size = pkt_size;
    unsigned i, count = usage->send_count, index;

    pj_mutex_lock(usage->mutex);
    for (i=0, ++usage->send_index; i<count; ++i, ++usage->send_index) {
	if (usage->send_index >= usage->send_count)
	    usage->send_index = 0;

	if (pj_ioqueue_is_pending(usage->key, &usage->send_key[usage->send_index])==0) {
	    break;
	}
    }

    if (i==count) {
	pj_mutex_unlock(usage->mutex);
	return PJ_EBUSY;
    }

    index = usage->send_index;
    pj_mutex_unlock(usage->mutex);

    return pj_ioqueue_sendto(usage->key, &usage->send_key[index], 
			     pkt, &size, flags,
			     dst_addr, addr_len);
}


static void on_read_complete(pj_ioqueue_key_t *key, 
                             pj_ioqueue_op_key_t *op_key, 
                             pj_ssize_t bytes_read)
{
    enum { MAX_LOOP = 10 };
    pj_stun_usage *usage = pj_ioqueue_get_user_data(key);
    struct worker *worker = (struct worker*) op_key;
    unsigned count;
    pj_status_t status;
    
    for (count=0; !usage->quitting; ++count) {
	unsigned flags;

	if (bytes_read > 0) {
	    (*usage->cb.on_rx_data)(usage, worker->readbuf, bytes_read,
				    &worker->src_addr, worker->src_addr_len);
	} else if (bytes_read < 0) {
	    pj_stun_perror(usage->pool->obj_name, "recv() error", -bytes_read);
	}

	if (usage->quitting)
	    break;

	bytes_read = sizeof(worker->readbuf);
	flags = (count >= MAX_LOOP) ? PJ_IOQUEUE_ALWAYS_ASYNC : 0;
	worker->src_addr_len = sizeof(worker->src_addr);
	status = pj_ioqueue_recvfrom(usage->key, &worker->read_key,
				     worker->readbuf, &bytes_read, flags,
				     &worker->src_addr, &worker->src_addr_len);
	if (status == PJ_EPENDING)
	    break;
    }
}