Changeset 4360 for pjproject/trunk/pjnath/src/pjnath/stun_transaction.c
- Timestamp:
- Feb 21, 2013 11:26:35 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath/stun_transaction.c
r4352 r4360 27 27 28 28 29 #define THIS_FILE "stun_transaction.c" 30 #define TIMER_INACTIVE 0 29 31 #define TIMER_ACTIVE 1 30 32 … … 35 37 pj_stun_tsx_cb cb; 36 38 void *user_data; 39 pj_grp_lock_t *grp_lock; 37 40 38 41 pj_bool_t complete; … … 52 55 53 56 57 #if 1 58 # define TRACE_(expr) PJ_LOG(5,expr) 59 #else 60 # define TRACE_(expr) 61 #endif 62 63 54 64 static void retransmit_timer_callback(pj_timer_heap_t *timer_heap, 55 65 pj_timer_entry *timer); … … 57 67 pj_timer_entry *timer); 58 68 59 #define stun_perror(tsx,msg,rc) pjnath_perror(tsx->obj_name, msg, rc)60 61 69 /* 62 70 * Create a STUN client transaction. … … 64 72 PJ_DEF(pj_status_t) pj_stun_client_tsx_create(pj_stun_config *cfg, 65 73 pj_pool_t *pool, 74 pj_grp_lock_t *grp_lock, 66 75 const pj_stun_tsx_cb *cb, 67 76 pj_stun_client_tsx **p_tsx) … … 75 84 tsx->rto_msec = cfg->rto_msec; 76 85 tsx->timer_heap = cfg->timer_heap; 86 tsx->grp_lock = grp_lock; 77 87 pj_memcpy(&tsx->cb, cb, sizeof(*cb)); 78 88 … … 83 93 tsx->destroy_timer.user_data = tsx; 84 94 85 pj_ansi_snprintf(tsx->obj_name, sizeof(tsx->obj_name), " stuntsx%p", tsx);95 pj_ansi_snprintf(tsx->obj_name, sizeof(tsx->obj_name), "utsx%p", tsx); 86 96 87 97 *p_tsx = tsx; … … 101 111 PJ_ASSERT_RETURN(tsx->cb.on_destroy, PJ_EINVAL); 102 112 113 pj_grp_lock_acquire(tsx->grp_lock); 114 103 115 /* Cancel previously registered timer */ 104 if (tsx->destroy_timer.id != 0) { 105 pj_timer_heap_cancel(tsx->timer_heap, &tsx->destroy_timer); 106 tsx->destroy_timer.id = 0; 107 } 116 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->destroy_timer, 117 TIMER_INACTIVE); 108 118 109 119 /* Stop retransmission, just in case */ 110 if (tsx->retransmit_timer.id != 0) {111 pj_timer_heap_cancel(tsx->timer_heap, &tsx->retransmit_timer);112 tsx->retransmit_timer.id = 0; 113 }114 115 status = pj_timer_heap_schedule(tsx->timer_heap,116 &tsx->destroy_timer, delay); 117 if (status != PJ_SUCCESS) 120 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->retransmit_timer, 121 TIMER_INACTIVE); 122 123 status = pj_timer_heap_schedule_w_grp_lock(tsx->timer_heap, 124 &tsx->destroy_timer, delay, 125 TIMER_ACTIVE, tsx->grp_lock); 126 if (status != PJ_SUCCESS) { 127 pj_grp_lock_release(tsx->grp_lock); 118 128 return status; 119 120 tsx->destroy_timer.id = TIMER_ACTIVE; 129 } 130 121 131 tsx->cb.on_complete = NULL; 122 132 133 pj_grp_lock_release(tsx->grp_lock); 134 135 TRACE_((tsx->obj_name, "STUN transaction %p schedule destroy", tsx)); 136 123 137 return PJ_SUCCESS; 124 138 } … … 128 142 * Destroy transaction immediately. 129 143 */ 130 PJ_DEF(pj_status_t) pj_stun_client_tsx_ destroy(pj_stun_client_tsx *tsx)144 PJ_DEF(pj_status_t) pj_stun_client_tsx_stop(pj_stun_client_tsx *tsx) 131 145 { 132 146 PJ_ASSERT_RETURN(tsx, PJ_EINVAL); 133 147 134 if (tsx->retransmit_timer.id != 0) { 135 pj_timer_heap_cancel(tsx->timer_heap, &tsx->retransmit_timer); 136 tsx->retransmit_timer.id = 0; 137 } 138 if (tsx->destroy_timer.id != 0) { 139 pj_timer_heap_cancel(tsx->timer_heap, &tsx->destroy_timer); 140 tsx->destroy_timer.id = 0; 141 } 142 143 PJ_LOG(5,(tsx->obj_name, "STUN client transaction destroyed")); 148 /* Don't call grp_lock_acquire() because we might be called on 149 * group lock's destructor. 150 */ 151 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->retransmit_timer, 152 TIMER_INACTIVE); 153 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->destroy_timer, 154 TIMER_INACTIVE); 155 156 PJ_LOG(5,(tsx->obj_name, "STUN client transaction %p stopped, ref_cnt=%d", 157 tsx, pj_grp_lock_get_ref(tsx->grp_lock))); 158 144 159 return PJ_SUCCESS; 145 160 } … … 186 201 pj_status_t status; 187 202 188 PJ_ASSERT_RETURN(tsx->retransmit_timer.id == 0||203 PJ_ASSERT_RETURN(tsx->retransmit_timer.id == TIMER_INACTIVE || 189 204 !tsx->require_retransmit, PJ_EBUSY); 190 205 … … 212 227 * cancel transmission). 213 228 */; 214 status = pj_timer_heap_schedule(tsx->timer_heap, 215 &tsx->retransmit_timer, 216 &tsx->retransmit_time); 229 status = pj_timer_heap_schedule_w_grp_lock(tsx->timer_heap, 230 &tsx->retransmit_timer, 231 &tsx->retransmit_time, 232 TIMER_ACTIVE, 233 tsx->grp_lock); 217 234 if (status != PJ_SUCCESS) { 218 tsx->retransmit_timer.id = 0;235 tsx->retransmit_timer.id = TIMER_INACTIVE; 219 236 return status; 220 237 } 221 tsx->retransmit_timer.id = TIMER_ACTIVE;222 238 } 223 239 … … 236 252 /* We've been destroyed, don't access the object. */ 237 253 } else if (status != PJ_SUCCESS) { 238 if ( tsx->retransmit_timer.id != 0 &&mod_count) {239 pj_timer_heap_cancel(tsx->timer_heap,240 &tsx->retransmit_timer);241 tsx->retransmit_timer.id = 0;242 } 243 stun_perror(tsx, "STUN error sending message", status);254 if (mod_count) { 255 pj_timer_heap_cancel_if_active( tsx->timer_heap, 256 &tsx->retransmit_timer, 257 TIMER_INACTIVE); 258 } 259 PJ_PERROR(4, (tsx->obj_name, status, "STUN error sending message")); 244 260 } 245 261 … … 261 277 PJ_ASSERT_RETURN(tsx && pkt && pkt_len, PJ_EINVAL); 262 278 PJ_ASSERT_RETURN(tsx->retransmit_timer.id == 0, PJ_EBUSY); 279 280 pj_grp_lock_acquire(tsx->grp_lock); 263 281 264 282 /* Encode message */ … … 287 305 * cancel transmission). 288 306 */; 289 status = pj_timer_heap_schedule(tsx->timer_heap, 290 &tsx->retransmit_timer, 291 &tsx->retransmit_time); 307 status = pj_timer_heap_schedule_w_grp_lock(tsx->timer_heap, 308 &tsx->retransmit_timer, 309 &tsx->retransmit_time, 310 TIMER_ACTIVE, 311 tsx->grp_lock); 292 312 if (status != PJ_SUCCESS) { 293 tsx->retransmit_timer.id = 0; 313 tsx->retransmit_timer.id = TIMER_INACTIVE; 314 pj_grp_lock_release(tsx->grp_lock); 294 315 return status; 295 316 } 296 tsx->retransmit_timer.id = TIMER_ACTIVE;297 317 } 298 318 … … 300 320 status = tsx_transmit_msg(tsx, PJ_TRUE); 301 321 if (status != PJ_SUCCESS) { 302 if (tsx->retransmit_timer.id != 0) { 303 pj_timer_heap_cancel(tsx->timer_heap, 304 &tsx->retransmit_timer); 305 tsx->retransmit_timer.id = 0; 306 } 322 pj_timer_heap_cancel_if_active(tsx->timer_heap, 323 &tsx->retransmit_timer, 324 TIMER_INACTIVE); 325 pj_grp_lock_release(tsx->grp_lock); 307 326 return status; 308 327 } 309 328 329 pj_grp_lock_release(tsx->grp_lock); 310 330 return PJ_SUCCESS; 311 331 } … … 320 340 321 341 PJ_UNUSED_ARG(timer_heap); 342 pj_grp_lock_acquire(tsx->grp_lock); 322 343 323 344 if (tsx->transmit_count >= PJ_STUN_MAX_TRANSMIT_COUNT) { … … 332 353 } 333 354 } 355 pj_grp_lock_release(tsx->grp_lock); 334 356 /* We might have been destroyed, don't try to access the object */ 335 357 pj_log_pop_indent(); … … 339 361 tsx->retransmit_timer.id = 0; 340 362 status = tsx_transmit_msg(tsx, PJ_TRUE); 341 if (status == PJNATH_ESTUNDESTROYED) { 342 /* We've been destroyed, don't try to access the object */ 343 } else if (status != PJ_SUCCESS) { 363 if (status != PJ_SUCCESS) { 344 364 tsx->retransmit_timer.id = 0; 345 365 if (!tsx->complete) { … … 349 369 } 350 370 } 351 /* We might have been destroyed, don't try to access the object */ 352 } 371 } 372 373 pj_grp_lock_release(tsx->grp_lock); 374 /* We might have been destroyed, don't try to access the object */ 353 375 } 354 376 … … 363 385 } 364 386 365 if (tsx->retransmit_timer.id != 0) { 366 pj_timer_heap_cancel(tsx->timer_heap, &tsx->retransmit_timer); 367 tsx->retransmit_timer.id = 0; 368 } 387 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->retransmit_timer, 388 TIMER_INACTIVE); 369 389 370 390 return tsx_transmit_msg(tsx, mod_count); … … 380 400 381 401 tsx->destroy_timer.id = PJ_FALSE; 402 382 403 tsx->cb.on_destroy(tsx); 383 404 /* Don't access transaction after this */ … … 409 430 * We can cancel retransmit timer now. 410 431 */ 411 if (tsx->retransmit_timer.id) { 412 pj_timer_heap_cancel(tsx->timer_heap, &tsx->retransmit_timer); 413 tsx->retransmit_timer.id = 0; 414 } 432 pj_timer_heap_cancel_if_active(tsx->timer_heap, &tsx->retransmit_timer, 433 TIMER_INACTIVE); 415 434 416 435 /* Find STUN error code attribute */
Note: See TracChangeset
for help on using the changeset viewer.