1
/* $Id: activesock.c 4238 2012-08-31 06:17:56Z 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_UNUSED_ARG(delay);
217
pj_symbianos_poll(-1, 100);
219
pj_ioqueue_poll(ioqueue, &delay);
223
if (srv1->rx_err_cnt+srv1->tx_err_cnt != 0 ||
224
srv2->rx_err_cnt+srv2->tx_err_cnt != 0)
231
if (last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt) {
234
udp_echo_err("packets have been lost", PJ_ETIMEDOUT);
243
udp_echo_srv_destroy(srv2);
245
udp_echo_srv_destroy(srv1);
247
pj_ioqueue_destroy(ioqueue);
249
pj_pool_release(pool);
256
#define SIGNATURE 0xdeadbeef
259
pj_uint32_t signature;
268
pj_uint32_t next_recv_seq;
274
pj_ioqueue_op_key_t op_key;
278
static pj_bool_t tcp_on_data_read(pj_activesock_t *asock,
282
pj_size_t *remainder)
284
struct tcp_state *st = (struct tcp_state*) pj_activesock_get_user_data(asock);
285
char *next = (char*) data;
287
if (status != PJ_SUCCESS && status != PJ_EPENDING) {
288
PJ_LOG(1,("", " err: status=%d", status));
293
while (size >= sizeof(struct tcp_pkt)) {
294
struct tcp_pkt *tcp_pkt = (struct tcp_pkt*) next;
296
if (tcp_pkt->signature != SIGNATURE) {
297
PJ_LOG(1,("", " err: invalid signature at seq=%d",
302
if (tcp_pkt->seq != st->next_recv_seq) {
303
PJ_LOG(1,("", " err: wrong sequence"));
309
next += sizeof(struct tcp_pkt);
310
size -= sizeof(struct tcp_pkt);
314
pj_memmove(data, next, size);
321
static pj_bool_t tcp_on_data_sent(pj_activesock_t *asock,
322
pj_ioqueue_op_key_t *op_key,
325
struct tcp_state *st=(struct tcp_state*)pj_activesock_get_user_data(asock);
327
PJ_UNUSED_ARG(op_key);
339
static int tcp_perf_test(void)
341
enum { COUNT=10000 };
342
pj_pool_t *pool = NULL;
343
pj_ioqueue_t *ioqueue = NULL;
344
pj_sock_t sock1=PJ_INVALID_SOCKET, sock2=PJ_INVALID_SOCKET;
345
pj_activesock_t *asock1 = NULL, *asock2 = NULL;
347
struct tcp_state *state1, *state2;
351
pool = pj_pool_create(mem, "tcpperf", 256, 256, NULL);
353
status = app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock1,
355
if (status != PJ_SUCCESS) {
360
status = pj_ioqueue_create(pool, 4, &ioqueue);
361
if (status != PJ_SUCCESS) {
366
pj_bzero(&cb, sizeof(cb));
367
cb.on_data_read = &tcp_on_data_read;
368
cb.on_data_sent = &tcp_on_data_sent;
370
state1 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
371
status = pj_activesock_create(pool, sock1, pj_SOCK_STREAM(), NULL, ioqueue,
372
&cb, state1, &asock1);
373
if (status != PJ_SUCCESS) {
378
state2 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
379
status = pj_activesock_create(pool, sock2, pj_SOCK_STREAM(), NULL, ioqueue,
380
&cb, state2, &asock2);
381
if (status != PJ_SUCCESS) {
386
status = pj_activesock_start_read(asock1, pool, 1000, 0);
387
if (status != PJ_SUCCESS) {
392
/* Send packet as quickly as possible */
393
for (i=0; i<COUNT && !state1->err && !state2->err; ++i) {
395
struct send_key send_key[2], *op_key;
398
pkt = (struct tcp_pkt*)state2->pkt;
399
pkt->signature = SIGNATURE;
401
pj_memset(pkt->fill, 'a', sizeof(pkt->fill));
403
op_key = &send_key[i%2];
404
pj_ioqueue_op_key_init(&op_key->op_key, sizeof(*op_key));
406
state2->sent = PJ_FALSE;
408
status = pj_activesock_send(asock2, &op_key->op_key, pkt, &len, 0);
409
if (status == PJ_EPENDING) {
412
pj_symbianos_poll(-1, -1);
414
pj_ioqueue_poll(ioqueue, NULL);
416
} while (!state2->sent);
419
/* The Symbian socket always returns PJ_SUCCESS for TCP send,
420
* eventhough the remote end hasn't received the data yet.
421
* If we continue sending, eventually send() will block,
422
* possibly because the send buffer is full. So we need to
423
* poll the ioqueue periodically, to let receiver gets the
426
pj_symbianos_poll(-1, 0);
428
if (status != PJ_SUCCESS) {
429
PJ_LOG(1,("", " err: send status=%d", status));
432
} else if (status == PJ_SUCCESS) {
433
if (len != sizeof(*pkt)) {
434
PJ_LOG(1,("", " err: shouldn't report partial sent"));
443
pj_time_val timeout = {0, 10};
444
if (pj_ioqueue_poll(ioqueue, &timeout) < 1)
451
/* Wait until everything has been sent/received */
452
if (state1->next_recv_seq < COUNT) {
454
while (pj_symbianos_poll(-1, 1000) == PJ_TRUE)
457
pj_time_val delay = {0, 100};
458
while (pj_ioqueue_poll(ioqueue, &delay) > 0)
463
if (status == PJ_EPENDING)
477
if (state1->next_recv_seq != COUNT) {
478
PJ_LOG(3,("", " err: only %u packets received, expecting %u",
479
state1->next_recv_seq, COUNT));
486
pj_activesock_close(asock2);
488
pj_activesock_close(asock1);
490
pj_ioqueue_destroy(ioqueue);
492
pj_pool_release(pool);
499
int activesock_test(void)
503
PJ_LOG(3,("", "..udp ping/pong test"));
504
ret = udp_ping_pong_test();
508
PJ_LOG(3,("", "..tcp perf test"));
509
ret = tcp_perf_test();
516
#else /* INCLUDE_ACTIVESOCK_TEST */
517
/* To prevent warning about "translation unit is empty"
518
* when this test is disabled.
520
int dummy_active_sock_test;
521
#endif /* INCLUDE_ACTIVESOCK_TEST */