Changeset 4360 for pjproject/trunk/pjnath/src/pjnath/turn_session.c
- Timestamp:
- Feb 21, 2013 11:26:35 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath/turn_session.c
r4201 r4360 113 113 void *user_data; 114 114 pj_stun_config stun_cfg; 115 116 pj_lock_t *lock; 115 pj_bool_t is_destroying; 116 117 pj_grp_lock_t *grp_lock; 117 118 int busy; 118 119 … … 162 163 static void sess_shutdown(pj_turn_session *sess, 163 164 pj_status_t status); 165 static void turn_sess_on_destroy(void *comp); 164 166 static void do_destroy(pj_turn_session *sess); 165 167 static void send_refresh(pj_turn_session *sess, int lifetime); … … 237 239 int af, 238 240 pj_turn_tp_type conn_type, 241 pj_grp_lock_t *grp_lock, 239 242 const pj_turn_session_cb *cb, 240 243 unsigned options, … … 245 248 pj_turn_session *sess; 246 249 pj_stun_session_cb stun_cb; 247 pj_lock_t *null_lock;248 250 pj_status_t status; 249 251 … … 282 284 283 285 /* Session lock */ 284 status = pj_lock_create_recursive_mutex(pool, sess->obj_name, 285 &sess->lock); 286 if (status != PJ_SUCCESS) { 287 do_destroy(sess); 288 return status; 289 } 286 if (grp_lock) { 287 sess->grp_lock = grp_lock; 288 } else { 289 status = pj_grp_lock_create(pool, NULL, &sess->grp_lock); 290 if (status != PJ_SUCCESS) { 291 pj_pool_release(pool); 292 return status; 293 } 294 } 295 296 pj_grp_lock_add_ref(sess->grp_lock); 297 pj_grp_lock_add_handler(sess->grp_lock, pool, sess, 298 &turn_sess_on_destroy); 290 299 291 300 /* Timer */ … … 298 307 stun_cb.on_rx_indication = &stun_on_rx_indication; 299 308 status = pj_stun_session_create(&sess->stun_cfg, sess->obj_name, &stun_cb, 300 PJ_FALSE, &sess->stun);309 PJ_FALSE, sess->grp_lock, &sess->stun); 301 310 if (status != PJ_SUCCESS) { 302 311 do_destroy(sess); … … 307 316 pj_stun_session_set_user_data(sess->stun, sess); 308 317 309 /* Replace mutex in STUN session with a NULL mutex, since access to310 * STUN session is serialized.311 */312 status = pj_lock_create_null_mutex(pool, name, &null_lock);313 if (status != PJ_SUCCESS) {314 do_destroy(sess);315 return status;316 }317 pj_stun_session_set_lock(sess->stun, null_lock, PJ_TRUE);318 319 318 /* Done */ 320 319 … … 326 325 327 326 328 /* Destroy */ 329 static void do_destroy(pj_turn_session *sess) 330 { 331 /* Lock session */ 332 if (sess->lock) { 333 pj_lock_acquire(sess->lock); 334 } 335 336 /* Cancel pending timer, if any */ 337 if (sess->timer.id != TIMER_NONE) { 338 pj_timer_heap_cancel(sess->timer_heap, &sess->timer); 339 sess->timer.id = TIMER_NONE; 340 } 341 342 /* Destroy STUN session */ 343 if (sess->stun) { 344 pj_stun_session_destroy(sess->stun); 345 sess->stun = NULL; 346 } 347 348 /* Destroy lock */ 349 if (sess->lock) { 350 pj_lock_release(sess->lock); 351 pj_lock_destroy(sess->lock); 352 sess->lock = NULL; 353 } 327 static void turn_sess_on_destroy(void *comp) 328 { 329 pj_turn_session *sess = (pj_turn_session*) comp; 354 330 355 331 /* Destroy pool */ … … 362 338 pj_pool_release(pool); 363 339 } 340 } 341 342 /* Destroy */ 343 static void do_destroy(pj_turn_session *sess) 344 { 345 PJ_LOG(4,(sess->obj_name, "TURN session destroy request, ref_cnt=%d", 346 pj_grp_lock_get_ref(sess->grp_lock))); 347 348 pj_grp_lock_acquire(sess->grp_lock); 349 if (sess->is_destroying) { 350 pj_grp_lock_release(sess->grp_lock); 351 return; 352 } 353 354 sess->is_destroying = PJ_TRUE; 355 pj_timer_heap_cancel_if_active(sess->timer_heap, &sess->timer, TIMER_NONE); 356 pj_stun_session_destroy(sess->stun); 357 358 pj_grp_lock_dec_ref(sess->grp_lock); 359 pj_grp_lock_release(sess->grp_lock); 364 360 } 365 361 … … 438 434 set_state(sess, PJ_TURN_STATE_DESTROYING); 439 435 440 if (sess->timer.id != TIMER_NONE) { 441 pj_timer_heap_cancel(sess->timer_heap, &sess->timer); 442 sess->timer.id = TIMER_NONE; 443 } 444 445 sess->timer.id = TIMER_DESTROY; 446 pj_timer_heap_schedule(sess->timer_heap, &sess->timer, &delay); 436 pj_timer_heap_cancel_if_active(sess->timer_heap, &sess->timer, 437 TIMER_NONE); 438 pj_timer_heap_schedule_w_grp_lock(sess->timer_heap, &sess->timer, 439 &delay, TIMER_DESTROY, 440 sess->grp_lock); 447 441 } 448 442 } … … 456 450 PJ_ASSERT_RETURN(sess, PJ_EINVAL); 457 451 458 pj_ lock_acquire(sess->lock);452 pj_grp_lock_acquire(sess->grp_lock); 459 453 460 454 sess_shutdown(sess, PJ_SUCCESS); 461 455 462 pj_ lock_release(sess->lock);456 pj_grp_lock_release(sess->grp_lock); 463 457 464 458 return PJ_SUCCESS; … … 554 548 pj_status_t status; 555 549 556 pj_ lock_acquire(sess->lock);550 pj_grp_lock_acquire(sess->grp_lock); 557 551 status = pj_stun_session_set_software_name(sess->stun, sw); 558 pj_ lock_release(sess->lock);552 pj_grp_lock_release(sess->grp_lock); 559 553 560 554 return status; … … 577 571 PJ_ASSERT_RETURN(sess->state == PJ_TURN_STATE_NULL, PJ_EINVALIDOP); 578 572 579 pj_ lock_acquire(sess->lock);573 pj_grp_lock_acquire(sess->grp_lock); 580 574 581 575 /* See if "domain" contains just IP address */ … … 677 671 678 672 on_return: 679 pj_ lock_release(sess->lock);673 pj_grp_lock_release(sess->grp_lock); 680 674 return status; 681 675 } … … 691 685 PJ_ASSERT_RETURN(sess->stun, PJ_EINVALIDOP); 692 686 693 pj_ lock_acquire(sess->lock);687 pj_grp_lock_acquire(sess->grp_lock); 694 688 695 689 pj_stun_session_set_credential(sess->stun, PJ_STUN_AUTH_LONG_TERM, cred); 696 690 697 pj_ lock_release(sess->lock);691 pj_grp_lock_release(sess->grp_lock); 698 692 699 693 return PJ_SUCCESS; … … 716 710 PJ_EINVALIDOP); 717 711 718 pj_ lock_acquire(sess->lock);712 pj_grp_lock_acquire(sess->grp_lock); 719 713 720 714 if (param && param != &sess->alloc_param) … … 727 721 state_names[sess->state])); 728 722 729 pj_ lock_release(sess->lock);723 pj_grp_lock_release(sess->grp_lock); 730 724 return PJ_SUCCESS; 731 725 … … 739 733 PJ_STUN_MAGIC, NULL, &tdata); 740 734 if (status != PJ_SUCCESS) { 741 pj_ lock_release(sess->lock);735 pj_grp_lock_release(sess->grp_lock); 742 736 return status; 743 737 } … … 779 773 } 780 774 781 pj_ lock_release(sess->lock);775 pj_grp_lock_release(sess->grp_lock); 782 776 return status; 783 777 } … … 800 794 PJ_ASSERT_RETURN(sess && addr_cnt && addr, PJ_EINVAL); 801 795 802 pj_ lock_acquire(sess->lock);796 pj_grp_lock_acquire(sess->grp_lock); 803 797 804 798 /* Create a bare CreatePermission request */ … … 807 801 PJ_STUN_MAGIC, NULL, &tdata); 808 802 if (status != PJ_SUCCESS) { 809 pj_ lock_release(sess->lock);803 pj_grp_lock_release(sess->grp_lock); 810 804 return status; 811 805 } … … 858 852 } 859 853 860 pj_ lock_release(sess->lock);854 pj_grp_lock_release(sess->grp_lock); 861 855 return PJ_SUCCESS; 862 856 … … 875 869 invalidate_perm(sess, perm); 876 870 } 877 pj_ lock_release(sess->lock);871 pj_grp_lock_release(sess->grp_lock); 878 872 return status; 879 873 } … … 946 940 947 941 /* Lock session now */ 948 pj_ lock_acquire(sess->lock);942 pj_grp_lock_acquire(sess->grp_lock); 949 943 950 944 /* Lookup permission first */ … … 961 955 0); 962 956 if (status != PJ_SUCCESS) { 963 pj_ lock_release(sess->lock);957 pj_grp_lock_release(sess->grp_lock); 964 958 return status; 965 959 } … … 1036 1030 1037 1031 on_return: 1038 pj_ lock_release(sess->lock);1032 pj_grp_lock_release(sess->grp_lock); 1039 1033 return status; 1040 1034 } … … 1056 1050 PJ_ASSERT_RETURN(sess->state == PJ_TURN_STATE_READY, PJ_EINVALIDOP); 1057 1051 1058 pj_ lock_acquire(sess->lock);1052 pj_grp_lock_acquire(sess->grp_lock); 1059 1053 1060 1054 /* Create blank ChannelBind request */ … … 1099 1093 1100 1094 on_return: 1101 pj_ lock_release(sess->lock);1095 pj_grp_lock_release(sess->grp_lock); 1102 1096 return status; 1103 1097 } … … 1122 1116 1123 1117 /* Start locking the session */ 1124 pj_ lock_acquire(sess->lock);1118 pj_grp_lock_acquire(sess->grp_lock); 1125 1119 1126 1120 is_datagram = (sess->conn_type==PJ_TURN_TP_UDP); … … 1194 1188 1195 1189 on_return: 1196 pj_ lock_release(sess->lock);1190 pj_grp_lock_release(sess->grp_lock); 1197 1191 return status; 1198 1192 } … … 1386 1380 /* Cancel existing keep-alive timer, if any */ 1387 1381 pj_assert(sess->timer.id != TIMER_DESTROY); 1388 1389 if (sess->timer.id != TIMER_NONE) { 1390 pj_timer_heap_cancel(sess->timer_heap, &sess->timer); 1391 sess->timer.id = TIMER_NONE; 1382 if (sess->timer.id == TIMER_KEEP_ALIVE) { 1383 pj_timer_heap_cancel_if_active(sess->timer_heap, &sess->timer, 1384 TIMER_NONE); 1392 1385 } 1393 1386 1394 1387 /* Start keep-alive timer once allocation succeeds */ 1395 timeout.sec = sess->ka_interval; 1396 timeout.msec = 0; 1397 1398 sess->timer.id = TIMER_KEEP_ALIVE; 1399 pj_timer_heap_schedule(sess->timer_heap, &sess->timer, &timeout); 1400 1401 set_state(sess, PJ_TURN_STATE_READY); 1388 if (sess->state < PJ_TURN_STATE_DEALLOCATING) { 1389 timeout.sec = sess->ka_interval; 1390 timeout.msec = 0; 1391 1392 pj_timer_heap_schedule_w_grp_lock(sess->timer_heap, &sess->timer, 1393 &timeout, TIMER_KEEP_ALIVE, 1394 sess->grp_lock); 1395 1396 set_state(sess, PJ_TURN_STATE_READY); 1397 } 1402 1398 } 1403 1399 … … 1949 1945 PJ_UNUSED_ARG(th); 1950 1946 1951 pj_ lock_acquire(sess->lock);1947 pj_grp_lock_acquire(sess->grp_lock); 1952 1948 1953 1949 eid = (enum timer_id_t) e->id; … … 2026 2022 delay.msec = 0; 2027 2023 2028 sess->timer.id = TIMER_KEEP_ALIVE; 2029 pj_timer_heap_schedule(sess->timer_heap, &sess->timer, &delay); 2030 } 2031 2032 pj_lock_release(sess->lock); 2024 pj_timer_heap_schedule_w_grp_lock(sess->timer_heap, &sess->timer, 2025 &delay, TIMER_KEEP_ALIVE, 2026 sess->grp_lock); 2027 } 2033 2028 2034 2029 } else if (eid == TIMER_DESTROY) { 2035 2030 /* Time to destroy */ 2036 pj_lock_release(sess->lock);2037 2031 do_destroy(sess); 2038 2032 } else { 2039 2033 pj_assert(!"Unknown timer event"); 2040 pj_lock_release(sess->lock); 2041 } 2042 } 2043 2034 } 2035 2036 pj_grp_lock_release(sess->grp_lock); 2037 } 2038
Note: See TracChangeset
for help on using the changeset viewer.