1
/* $Id: activesock.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* \page page_pjlib_activesock_test Test: Active Socket
26
* This file is <b>pjlib-test/activesock.c</b>
28
* \include pjlib-test/activesock.c
31
#if INCLUDE_ACTIVESOCK_TEST
33
#define THIS_FILE "activesock.c"
36
/*******************************************************************
37
* Simple UDP echo server.
41
pj_activesock_t *asock;
42
pj_bool_t echo_enabled;
44
pj_ioqueue_op_key_t send_key;
47
unsigned rx_err_cnt, tx_err_cnt;
50
static void udp_echo_err(const char *title, pj_status_t status)
52
char errmsg[PJ_ERR_MSG_SIZE];
54
pj_strerror(status, errmsg, sizeof(errmsg));
55
PJ_LOG(3,(THIS_FILE, " error: %s: %s", title, errmsg));
58
static pj_bool_t udp_echo_srv_on_data_recvfrom(pj_activesock_t *asock,
61
const pj_sockaddr_t *src_addr,
65
struct udp_echo_srv *srv;
69
srv = (struct udp_echo_srv*) pj_activesock_get_user_data(asock);
71
if (status != PJ_SUCCESS) {
74
udp_echo_err("recvfrom() callback", status);
80
/* Send back if echo is enabled */
81
if (srv->echo_enabled) {
83
srv->status = pj_activesock_sendto(asock, &srv->send_key, data,
86
if (srv->status != PJ_SUCCESS) {
88
udp_echo_err("sendto()", status);
96
static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
97
pj_ioqueue_t *ioqueue,
98
pj_bool_t enable_echo,
99
struct udp_echo_srv **p_srv)
101
struct udp_echo_srv *srv;
102
pj_sock_t sock_fd = PJ_INVALID_SOCKET;
105
pj_activesock_cb activesock_cb;
108
srv = PJ_POOL_ZALLOC_T(pool, struct udp_echo_srv);
109
srv->echo_enabled = enable_echo;
111
pj_sockaddr_in_init(&addr.ipv4, NULL, 0);
112
addr_len = sizeof(addr);
114
pj_bzero(&activesock_cb, sizeof(activesock_cb));
115
activesock_cb.on_data_recvfrom = &udp_echo_srv_on_data_recvfrom;
117
status = pj_activesock_create_udp(pool, &addr, NULL, ioqueue, &activesock_cb,
118
srv, &srv->asock, &addr);
119
if (status != PJ_SUCCESS) {
120
pj_sock_close(sock_fd);
121
udp_echo_err("pj_activesock_create()", status);
125
srv->port = pj_ntohs(addr.ipv4.sin_port);
127
pj_ioqueue_op_key_init(&srv->send_key, sizeof(srv->send_key));
129
status = pj_activesock_start_recvfrom(srv->asock, pool, 32, 0);
130
if (status != PJ_SUCCESS) {
131
pj_activesock_close(srv->asock);
132
udp_echo_err("pj_activesock_start_recvfrom()", status);
141
static void udp_echo_srv_destroy(struct udp_echo_srv *srv)
143
pj_activesock_close(srv->asock);
146
/*******************************************************************
147
* UDP ping pong test (send packet back and forth between two UDP echo
150
static int udp_ping_pong_test(void)
152
pj_ioqueue_t *ioqueue = NULL;
153
pj_pool_t *pool = NULL;
154
struct udp_echo_srv *srv1=NULL, *srv2=NULL;
155
pj_bool_t need_send = PJ_TRUE;
160
pool = pj_pool_create(mem, "pingpong", 512, 512, NULL);
164
status = pj_ioqueue_create(pool, 4, &ioqueue);
165
if (status != PJ_SUCCESS) {
167
udp_echo_err("pj_ioqueue_create()", status);
171
status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv1);
172
if (status != PJ_SUCCESS) {
177
status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv2);
178
if (status != PJ_SUCCESS) {
183
/* initiate the first send */
184
for (count=0; count<1000; ++count) {
185
unsigned last_rx1, last_rx2;
196
loopback = pj_str("127.0.0.1");
197
pj_sockaddr_in_init(&addr, &loopback, srv2->port);
198
status = pj_activesock_sendto(srv1->asock, &srv1->send_key,
200
&addr, sizeof(addr));
201
if (status != PJ_SUCCESS && status != PJ_EPENDING) {
203
udp_echo_err("sendto()", status);
207
need_send = PJ_FALSE;
210
last_rx1 = srv1->rx_cnt;
211
last_rx2 = srv2->rx_cnt;
213
for (i=0; i<10 && last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt; ++i) {
214
pj_time_val delay = {0, 10};
216
pj_symbianos_poll(-1, 100);
218
pj_ioqueue_poll(ioqueue, &delay);
222
if (srv1->rx_err_cnt+srv1->tx_err_cnt != 0 ||
223
srv2->rx_err_cnt+srv2->tx_err_cnt != 0)
230
if (last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt) {
233
udp_echo_err("packets have been lost", PJ_ETIMEDOUT);
242
udp_echo_srv_destroy(srv2);
244
udp_echo_srv_destroy(srv1);
246
pj_ioqueue_destroy(ioqueue);
248
pj_pool_release(pool);
255
#define SIGNATURE 0xdeadbeef
258
pj_uint32_t signature;
267
pj_uint32_t next_recv_seq;
273
pj_ioqueue_op_key_t op_key;
277
static pj_bool_t tcp_on_data_read(pj_activesock_t *asock,
281
pj_size_t *remainder)
283
struct tcp_state *st = (struct tcp_state*) pj_activesock_get_user_data(asock);
284
char *next = (char*) data;
286
if (status != PJ_SUCCESS && status != PJ_EPENDING) {
287
PJ_LOG(1,("", " err: status=%d", status));
292
while (size >= sizeof(struct tcp_pkt)) {
293
struct tcp_pkt *tcp_pkt = (struct tcp_pkt*) next;
295
if (tcp_pkt->signature != SIGNATURE) {
296
PJ_LOG(1,("", " err: invalid signature at seq=%d",
301
if (tcp_pkt->seq != st->next_recv_seq) {
302
PJ_LOG(1,("", " err: wrong sequence"));
308
next += sizeof(struct tcp_pkt);
309
size -= sizeof(struct tcp_pkt);
313
pj_memmove(data, next, size);
320
static pj_bool_t tcp_on_data_sent(pj_activesock_t *asock,
321
pj_ioqueue_op_key_t *op_key,
324
struct tcp_state *st=(struct tcp_state*)pj_activesock_get_user_data(asock);
326
PJ_UNUSED_ARG(op_key);
338
static int tcp_perf_test(void)
340
enum { COUNT=10000 };
341
pj_pool_t *pool = NULL;
342
pj_ioqueue_t *ioqueue = NULL;
343
pj_sock_t sock1=PJ_INVALID_SOCKET, sock2=PJ_INVALID_SOCKET;
344
pj_activesock_t *asock1 = NULL, *asock2 = NULL;
346
struct tcp_state *state1, *state2;
350
pool = pj_pool_create(mem, "tcpperf", 256, 256, NULL);
352
status = app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock1,
354
if (status != PJ_SUCCESS) {
359
status = pj_ioqueue_create(pool, 4, &ioqueue);
360
if (status != PJ_SUCCESS) {
365
pj_bzero(&cb, sizeof(cb));
366
cb.on_data_read = &tcp_on_data_read;
367
cb.on_data_sent = &tcp_on_data_sent;
369
state1 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
370
status = pj_activesock_create(pool, sock1, pj_SOCK_STREAM(), NULL, ioqueue,
371
&cb, state1, &asock1);
372
if (status != PJ_SUCCESS) {
377
state2 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
378
status = pj_activesock_create(pool, sock2, pj_SOCK_STREAM(), NULL, ioqueue,
379
&cb, state2, &asock2);
380
if (status != PJ_SUCCESS) {
385
status = pj_activesock_start_read(asock1, pool, 1000, 0);
386
if (status != PJ_SUCCESS) {
391
/* Send packet as quickly as possible */
392
for (i=0; i<COUNT && !state1->err && !state2->err; ++i) {
394
struct send_key send_key[2], *op_key;
397
pkt = (struct tcp_pkt*)state2->pkt;
398
pkt->signature = SIGNATURE;
400
pj_memset(pkt->fill, 'a', sizeof(pkt->fill));
402
op_key = &send_key[i%2];
403
pj_ioqueue_op_key_init(&op_key->op_key, sizeof(*op_key));
405
state2->sent = PJ_FALSE;
407
status = pj_activesock_send(asock2, &op_key->op_key, pkt, &len, 0);
408
if (status == PJ_EPENDING) {
411
pj_symbianos_poll(-1, -1);
413
pj_ioqueue_poll(ioqueue, NULL);
415
} while (!state2->sent);
418
/* The Symbian socket always returns PJ_SUCCESS for TCP send,
419
* eventhough the remote end hasn't received the data yet.
420
* If we continue sending, eventually send() will block,
421
* possibly because the send buffer is full. So we need to
422
* poll the ioqueue periodically, to let receiver gets the
425
pj_symbianos_poll(-1, 0);
427
if (status != PJ_SUCCESS) {
428
PJ_LOG(1,("", " err: send status=%d", status));
431
} else if (status == PJ_SUCCESS) {
432
if (len != sizeof(*pkt)) {
433
PJ_LOG(1,("", " err: shouldn't report partial sent"));
442
pj_time_val timeout = {0, 10};
443
if (pj_ioqueue_poll(ioqueue, &timeout) < 1)
450
/* Wait until everything has been sent/received */
451
if (state1->next_recv_seq < COUNT) {
453
while (pj_symbianos_poll(-1, 1000) == PJ_TRUE)
456
pj_time_val delay = {0, 100};
457
while (pj_ioqueue_poll(ioqueue, &delay) > 0)
462
if (status == PJ_EPENDING)
476
if (state1->next_recv_seq != COUNT) {
477
PJ_LOG(3,("", " err: only %u packets received, expecting %u",
478
state1->next_recv_seq, COUNT));
485
pj_activesock_close(asock2);
487
pj_activesock_close(asock1);
489
pj_ioqueue_destroy(ioqueue);
491
pj_pool_release(pool);
498
int activesock_test(void)
502
PJ_LOG(3,("", "..udp ping/pong test"));
503
ret = udp_ping_pong_test();
507
PJ_LOG(3,("", "..tcp perf test"));
508
ret = tcp_perf_test();
515
#else /* INCLUDE_ACTIVESOCK_TEST */
516
/* To prevent warning about "translation unit is empty"
517
* when this test is disabled.
519
int dummy_active_sock_test;
520
#endif /* INCLUDE_ACTIVESOCK_TEST */