1
/* $Id: transport_tcp_test.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
25
#define THIS_FILE "transport_tcp_test.c"
32
int transport_tcp_test(void)
34
enum { SEND_RECV_LOOP = 8 };
35
pjsip_tpfactory *tpfactory;
37
pj_sockaddr_in rem_addr;
39
char url[PJSIP_MAX_URL_SIZE];
40
int rtt[SEND_RECV_LOOP], min_rtt;
43
/* Start TCP listener on arbitrary port. */
44
status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory);
45
if (status != PJ_SUCCESS) {
46
app_perror(" Error: unable to start TCP transport", status);
51
/* Get the listener address */
52
status = pj_sockaddr_in_init(&rem_addr, &tpfactory->addr_name.host,
53
(pj_uint16_t)tpfactory->addr_name.port);
54
if (status != PJ_SUCCESS) {
55
app_perror(" Error: possibly invalid TCP address name", status);
59
pj_ansi_sprintf(url, "sip:alice@%s:%d;transport=tcp",
60
pj_inet_ntoa(rem_addr.sin_addr),
61
pj_ntohs(rem_addr.sin_port));
64
/* Acquire one TCP transport. */
65
status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP,
66
&rem_addr, sizeof(rem_addr),
68
if (status != PJ_SUCCESS || tcp == NULL) {
69
app_perror(" Error: unable to acquire TCP transport", status);
73
/* After pjsip_endpt_acquire_transport, TCP transport must have
74
* reference counter 1.
76
if (pj_atomic_get(tcp->ref_cnt) != 1)
79
/* Test basic transport attributes */
80
status = generic_transport_test(tcp);
81
if (status != PJ_SUCCESS)
85
/* Check again that reference counter is 1. */
86
if (pj_atomic_get(tcp->ref_cnt) != 1)
90
if (transport_load_test(url) != 0)
93
/* Basic transport's send/receive loopback test. */
94
for (i=0; i<SEND_RECV_LOOP; ++i) {
95
status = transport_send_recv_test(PJSIP_TRANSPORT_TCP, tcp, url, &rtt[i]);
98
pjsip_transport_dec_ref(tcp);
105
for (i=0; i<SEND_RECV_LOOP; ++i)
106
if (rtt[i] < min_rtt) min_rtt = rtt[i];
108
report_ival("tcp-rtt-usec", min_rtt, "usec",
109
"Best TCP transport round trip time, in microseconds "
110
"(time from sending request until response is received. "
111
"Tests were performed on local machine only, and after "
112
"TCP socket has been established by previous test)");
115
/* Multi-threaded round-trip test. */
116
status = transport_rt_test(PJSIP_TRANSPORT_TCP, tcp, url, &pkt_lost);
118
pjsip_transport_dec_ref(tcp);
123
PJ_LOG(3,(THIS_FILE, " note: %d packet(s) was lost", pkt_lost));
125
/* Check again that reference counter is still 1. */
126
if (pj_atomic_get(tcp->ref_cnt) != 1)
129
/* Destroy this transport. */
130
pjsip_transport_dec_ref(tcp);
132
/* Force destroy this transport. */
133
status = pjsip_transport_destroy(tcp);
134
if (status != PJ_SUCCESS)
137
/* Unregister factory */
138
status = pjsip_tpmgr_unregister_tpfactory(pjsip_endpt_get_tpmgr(endpt),
140
if (status != PJ_SUCCESS)
144
PJ_LOG(3,(THIS_FILE, " Flushing events, 1 second..."));
150
#else /* PJ_HAS_TCP */
151
int transport_tcp_test(void)
155
#endif /* PJ_HAS_TCP */