Changeset 6002 for pjproject/trunk/pjsip/src/test/transport_tcp_test.c
- Timestamp:
- May 23, 2019 5:21:59 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip/src/test/transport_tcp_test.c
r5311 r6002 30 30 */ 31 31 #if PJ_HAS_TCP 32 33 static pj_status_t multi_listener_test(pjsip_tpfactory *factory[], 34 unsigned num_factory, 35 pjsip_transport *tp[], 36 unsigned *num_tp) 37 { 38 pj_status_t status; 39 unsigned i = 0; 40 pj_str_t s; 41 pjsip_transport *tcp; 42 pjsip_tpfactory *tpfactory = NULL; 43 pj_sockaddr_in rem_addr; 44 pjsip_tpselector tp_sel; 45 unsigned ntp = 0; 46 47 for (;i<num_factory;++i) 48 { 49 /* Start TCP listener on arbitrary port. */ 50 status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory); 51 if (status != PJ_SUCCESS) { 52 app_perror(" Error: unable to start TCP transport", status); 53 return -10; 54 } 55 56 factory[i] = tpfactory; 57 } 58 59 /* Get the last listener address */ 60 status = pj_sockaddr_in_init(&rem_addr, &tpfactory->addr_name.host, 61 (pj_uint16_t)tpfactory->addr_name.port); 62 if (status != PJ_SUCCESS) { 63 app_perror(" Error: possibly invalid TCP address name", status); 64 return -11; 65 } 66 67 /* Acquire transport without selector. */ 68 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, 69 &rem_addr, sizeof(rem_addr), 70 NULL, &tcp); 71 if (status != PJ_SUCCESS || tcp == NULL) { 72 app_perror(" Error: unable to acquire TCP transport", status); 73 return -12; 74 } 75 tp[ntp++] = tcp; 76 77 /* After pjsip_endpt_acquire_transport, TCP transport must have 78 * reference counter 1. 79 */ 80 if (pj_atomic_get(tcp->ref_cnt) != 1) 81 return -13; 82 83 /* Acquire with the same remote address, should return the same tp. */ 84 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, 85 &rem_addr, sizeof(rem_addr), 86 NULL, &tcp); 87 if (status != PJ_SUCCESS || tcp == NULL) { 88 app_perror(" Error: unable to acquire TCP transport", status); 89 return -14; 90 } 91 92 /* Should return existing transport. */ 93 if (tp[ntp-1] != tcp) { 94 return -15; 95 } 96 97 /* Using the same TCP transport, it must have reference counter 2. 98 */ 99 if (pj_atomic_get(tcp->ref_cnt) != 2) 100 return -16; 101 102 /* Decrease the reference. */ 103 pjsip_transport_dec_ref(tcp); 104 105 /* Test basic transport attributes */ 106 status = generic_transport_test(tcp); 107 if (status != PJ_SUCCESS) 108 return status; 109 110 /* Check again that reference counter is 1. */ 111 if (pj_atomic_get(tcp->ref_cnt) != 1) 112 return -17; 113 114 /* Acquire transport test with selector. */ 115 pj_bzero(&tp_sel, sizeof(tp_sel)); 116 tp_sel.type = PJSIP_TPSELECTOR_LISTENER; 117 tp_sel.u.listener = factory[num_factory/2]; 118 pj_sockaddr_in_init(&rem_addr, pj_cstr(&s, "1.1.1.1"), 80); 119 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP, 120 &rem_addr, sizeof(rem_addr), 121 &tp_sel, &tcp); 122 if (status != PJ_SUCCESS) { 123 app_perror(" Error: unable to acquire TCP transport", status); 124 return -18; 125 } 126 127 /* The transport should have the same factory set on the selector. */ 128 if (tcp->factory != factory[num_factory/2]) 129 return -19; 130 131 /* The transport should be newly created. */ 132 for (i = 0; i < ntp; ++i) { 133 if (tp[i] == tcp) { 134 break; 135 } 136 } 137 if (i != ntp) 138 return -20; 139 140 tp[ntp++] = tcp; 141 142 for (i = 0; i<ntp; ++i) { 143 if (pj_atomic_get(tp[i]->ref_cnt) != 1) 144 return -21; 145 } 146 *num_tp = ntp; 147 148 return PJ_SUCCESS; 149 } 150 32 151 int transport_tcp_test(void) 33 152 { 34 153 enum { SEND_RECV_LOOP = 8 }; 35 pjsip_tpfactory *tpfactory; 36 pjsip_transport *tcp; 154 enum { NUM_LISTENER = 4 }; 155 enum { NUM_TP = 8 }; 156 pjsip_tpfactory *tpfactory[NUM_LISTENER]; 157 pjsip_transport *tcp[NUM_TP]; 37 158 pj_sockaddr_in rem_addr; 38 159 pj_status_t status; … … 40 161 char addr[PJ_INET_ADDRSTRLEN]; 41 162 int rtt[SEND_RECV_LOOP], min_rtt; 42 int i, pkt_lost; 43 44 /* Start TCP listener on arbitrary port. */ 45 status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory); 46 if (status != PJ_SUCCESS) { 47 app_perror(" Error: unable to start TCP transport", status); 48 return -10; 49 } 50 51 52 /* Get the listener address */ 53 status = pj_sockaddr_in_init(&rem_addr, &tpfactory->addr_name.host, 54 (pj_uint16_t)tpfactory->addr_name.port); 55 if (status != PJ_SUCCESS) { 56 app_perror(" Error: possibly invalid TCP address name", status); 57 return -14; 58 } 163 int pkt_lost; 164 unsigned i; 165 unsigned num_listener = NUM_LISTENER; 166 unsigned num_tp = NUM_TP; 167 168 status = multi_listener_test(tpfactory, num_listener, tcp, &num_tp); 169 if (status != PJ_SUCCESS) 170 return status; 171 172 /* Get the last listener address */ 173 status = pj_sockaddr_in_init(&rem_addr, &tpfactory[0]->addr_name.host, 174 (pj_uint16_t)tpfactory[0]->addr_name.port); 59 175 60 176 pj_ansi_sprintf(url, "sip:alice@%s:%d;transport=tcp", 61 177 pj_inet_ntop2(pj_AF_INET(), &rem_addr.sin_addr, addr, 62 178 sizeof(addr)), 63 179 pj_ntohs(rem_addr.sin_port)); 64 65 66 /* Acquire one TCP transport. */67 status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_TCP,68 &rem_addr, sizeof(rem_addr),69 NULL, &tcp);70 if (status != PJ_SUCCESS || tcp == NULL) {71 app_perror(" Error: unable to acquire TCP transport", status);72 return -17;73 }74 75 /* After pjsip_endpt_acquire_transport, TCP transport must have76 * reference counter 1.77 */78 if (pj_atomic_get(tcp->ref_cnt) != 1)79 return -20;80 81 /* Test basic transport attributes */82 status = generic_transport_test(tcp);83 if (status != PJ_SUCCESS)84 return status;85 86 87 /* Check again that reference counter is 1. */88 if (pj_atomic_get(tcp->ref_cnt) != 1)89 return -40;90 180 91 181 /* Load test */ … … 95 185 /* Basic transport's send/receive loopback test. */ 96 186 for (i=0; i<SEND_RECV_LOOP; ++i) { 97 status = transport_send_recv_test(PJSIP_TRANSPORT_TCP, tcp, url, &rtt[i]); 187 status = transport_send_recv_test(PJSIP_TRANSPORT_TCP, tcp[0], url, 188 &rtt[i]); 98 189 99 190 if (status != 0) { 100 pjsip_transport_dec_ref(tcp); 191 for (i = 0; i < num_tp ; ++i) { 192 pjsip_transport_dec_ref(tcp[i]); 193 } 101 194 flush_events(500); 102 195 return -72; … … 116 209 117 210 /* Multi-threaded round-trip test. */ 118 status = transport_rt_test(PJSIP_TRANSPORT_TCP, tcp , url, &pkt_lost);211 status = transport_rt_test(PJSIP_TRANSPORT_TCP, tcp[0], url, &pkt_lost); 119 212 if (status != 0) { 120 pjsip_transport_dec_ref(tcp); 213 for (i = 0; i < num_tp ; ++i) { 214 pjsip_transport_dec_ref(tcp[i]); 215 } 121 216 return status; 122 217 } … … 126 221 127 222 /* Check again that reference counter is still 1. */ 128 if (pj_atomic_get(tcp->ref_cnt) != 1) 129 return -80; 130 131 /* Destroy this transport. */ 132 pjsip_transport_dec_ref(tcp); 133 134 /* Force destroy this transport. */ 135 status = pjsip_transport_destroy(tcp); 136 if (status != PJ_SUCCESS) 137 return -90; 138 139 /* Unregister factory */ 140 status = pjsip_tpmgr_unregister_tpfactory(pjsip_endpt_get_tpmgr(endpt), 141 tpfactory); 142 if (status != PJ_SUCCESS) 143 return -95; 223 for (i = 0; i < num_tp; ++i) { 224 if (pj_atomic_get(tcp[i]->ref_cnt) != 1) 225 return -80; 226 } 227 228 for (i = 0; i < num_tp; ++i) { 229 /* Destroy this transport. */ 230 pjsip_transport_dec_ref(tcp[i]); 231 232 /* Force destroy this transport. */ 233 status = pjsip_transport_destroy(tcp[i]); 234 if (status != PJ_SUCCESS) 235 return -90; 236 } 237 238 for (i = 0; i < num_listener; ++i) { 239 /* Unregister factory */ 240 status = pjsip_tpmgr_unregister_tpfactory(pjsip_endpt_get_tpmgr(endpt), 241 tpfactory[i]); 242 if (status != PJ_SUCCESS) 243 return -95; 244 } 144 245 145 246 /* Flush events. */
Note: See TracChangeset
for help on using the changeset viewer.