diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 10fb99b3f9..a9a755efa8 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -156,6 +156,7 @@ struct sofia_private { int is_static; sofia_dispatch_event_t *de; nua_handle_t *nh; + sofia_profile_t *profile; }; #define set_param(ptr,val) if (ptr) {free(ptr) ; ptr = NULL;} if (val) {ptr = strdup(val);} @@ -617,6 +618,7 @@ struct sofia_profile { uint32_t event_timeout; int watchdog_enabled; switch_mutex_t *gw_mutex; + switch_queue_t *nh_destroy_queue; }; struct private_object { @@ -1124,3 +1126,6 @@ char *sofia_glue_gen_contact_str(sofia_profile_t *profile, sip_t const *sip, sof void sofia_glue_pause_jitterbuffer(switch_core_session_t *session, switch_bool_t on); void sofia_process_dispatch_event(sofia_dispatch_event_t **dep); +int sofia_nua_handle_destroy_run(switch_queue_t *q); +void sofia_nua_handle_destroy(switch_queue_t *q, nua_handle_t **nhp); + diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index dd5cc68268..9e7f54a51f 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -1068,9 +1068,7 @@ static void our_sofia_event_callback(nua_event_t event, } if (check_destroy) { - if (0 && nh && ((sofia_private && sofia_private->destroy_nh) || !nua_handle_magic(nh))) { - - printf("FUCKER2\n"); + if (nh && ((sofia_private && sofia_private->destroy_nh) || !nua_handle_magic(nh))) { if (sofia_private) { nua_handle_bind(nh, NULL); @@ -1094,29 +1092,99 @@ static void our_sofia_event_callback(nua_event_t event, } } +int sofia_nua_handle_destroy_run(switch_queue_t *q) +{ + void *pop; + int i = 0; + uint32_t ttl; + + ttl = switch_queue_size(q); + + while(i < ttl && switch_queue_trypop(q, &pop) == SWITCH_STATUS_SUCCESS && pop) { + nua_handle_t *nh = (nua_handle_t *) pop; + int x = su_home_refcount((su_home_t *)nh) - 1; +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DESTROY %p\n", (void *) nh); +#endif + + if (x == 1) { + nua_handle_destroy(nh); + } else { +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DOH REQUEUE %p\n", (void *) nh); +#endif + switch_queue_push(q, nh); + } + nh = NULL; + i++; + } + + return i; +} + +void sofia_nua_handle_destroy(switch_queue_t *q, nua_handle_t **nhp) +{ + nua_handle_t *nh; + + switch_assert(nhp); + + nh = *nhp; + *nhp = NULL; + + if (nh) { + switch_queue_push(q, nh); + } +} + void sofia_process_dispatch_event(sofia_dispatch_event_t **dep) { sofia_dispatch_event_t *de = *dep; - nua_handle_t *nh = de->nh; + nua_handle_t *nh = de->nh, *snh = NULL; nua_t *nua = de->nua; sofia_private_t *sofia_private = nua_handle_magic(de->nh); + int destroy = 0; *dep = NULL; sofia_private = nua_handle_magic(nh); - //printf("QUEUE EVENT %s\n", nua_event_name(de->data->e_event)); - + if (de->data->e_event == nua_i_terminated) { +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "CATCH EVENT %s\n", nua_event_name(de->data->e_event)); +#endif + destroy = 1; + if (sofia_private && sofia_private->nh) { + snh = sofia_private->nh; + nua_handle_unref(nh); + } + } else { +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "PROCESS EVENT %s\n", nua_event_name(de->data->e_event)); +#endif our_sofia_event_callback(de->data->e_event, de->data->e_status, de->data->e_phrase, de->nua, de->profile, nh, sofia_private, de->sip, de, (tagi_t *) de->data->e_tags); - //printf("/QUEUE EVENT %s\n", nua_event_name(de->data->e_event)); +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "/PROCESS EVENT %s\n", nua_event_name(de->data->e_event)); +#endif + } + + nua_handle_unref(nh); nua_destroy_event(de->event); su_free(nh->nh_home, de); - nua_handle_unref(nh); + + if (destroy && snh) { + nua_handle_bind(snh, NULL); +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "go go UNREF %d\n", nh->nh_ref_by_user); +#endif + sofia_nua_handle_destroy(sofia_private->profile->nh_destroy_queue, &snh); + } + nua_stack_unref(nua); + } @@ -1213,19 +1281,10 @@ void sofia_event_callback(nua_event_t event, { sofia_dispatch_event_t *de; - //printf("EVENT %s\n", nua_event_name(event)); - - - - if (event == nua_i_terminated) { - if (sofia_private && sofia_private->nh) { - nua_handle_bind(nh, NULL); - nua_handle_destroy(sofia_private->nh); - } - - return; - } +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "GOT EVENT %s\n", x, nua_event_name(event)); +#endif de = su_alloc(nh->nh_home, sizeof(*de)); memset(de, 0, sizeof(*de)); @@ -1237,22 +1296,6 @@ void sofia_event_callback(nua_event_t event, de->nua = nua_stack_ref(nua); - if (event == nua_i_state) { - int ss_state = nua_callstate_init; - tl_gets(tags, NUTAG_CALLSTATE_REF(ss_state), TAG_END()); - - //printf("state [%s][%d]\n", nua_callstate_name(ss_state), status); - - if (ss_state == nua_callstate_terminated || ss_state == nua_callstate_terminating) { - sofia_process_dispatch_event(&de); - return; - } - } - - - - - if (event == nua_i_invite && !sofia_private) { if (!(sofia_private = su_alloc(nh->nh_home, sizeof(*sofia_private)))) { abort(); @@ -1262,6 +1305,7 @@ void sofia_event_callback(nua_event_t event, sofia_private->is_call = 2; sofia_private->de = de; sofia_private->nh = nua_handle_ref(nh); + sofia_private->profile = profile; nua_handle_bind(nh, sofia_private); return; } @@ -1270,9 +1314,12 @@ void sofia_event_callback(nua_event_t event, switch_core_session_t *session; if (!zstr(sofia_private->uuid)) { - if ((session = switch_core_session_locate(sofia_private->uuid))) { + if ((session = switch_core_session_force_locate(sofia_private->uuid))) { if (switch_core_session_running(session)) { +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SESSION QUEUE EVENT %s\n", nua_event_name(event)); +#endif switch_core_session_queue_signal_data(session, de); } else { switch_core_session_message_t msg = { 0 }; @@ -1288,6 +1335,10 @@ void sofia_event_callback(nua_event_t event, } } +#ifdef DEBUG_CONN + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "QUEUE EVENT %s\n", nua_event_name(event)); +#endif + sofia_queue_message(de); } @@ -1572,7 +1623,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread last_commit = switch_micro_time_now(); if (len) { - //printf("TRANS:\n%s\n", sqlbuf); + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "TRANS:\n%s\n", sqlbuf); switch_mutex_lock(profile->ireg_mutex); sofia_glue_actually_execute_sql_trans(profile, sqlbuf, NULL); //sofia_glue_actually_execute_sql(profile, "commit;\n", NULL); @@ -1588,6 +1639,9 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread } } + + sofia_nua_handle_destroy_run(profile->nh_destroy_queue); + if (switch_micro_time_now() - last_check >= 1000000) { if (profile->watchdog_enabled) { uint32_t event_diff = 0, step_diff = 0, event_fail = 0, step_fail = 0; @@ -1933,6 +1987,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void sofia_presence_establish_presence(profile); } + switch_queue_create(&profile->nh_destroy_queue, SOFIA_QUEUE_SIZE, profile->pool); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Starting thread for %s\n", profile->name); profile->started = switch_epoch_time_now(NULL); @@ -1962,6 +2018,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void } } + sofia_nua_handle_destroy_run(profile->nh_destroy_queue); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write lock %s\n", profile->name); switch_thread_rwlock_wrlock(profile->rwlock); sofia_reg_unregister(profile); diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index eeff2ed89b..57d0ec20e6 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -2309,7 +2309,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) memset(sofia_private, 0, sizeof(*sofia_private)); sofia_private->is_call++; sofia_private->nh = nua_handle_ref(tech_pvt->nh); - + sofia_private->profile = tech_pvt->profile; tech_pvt->sofia_private = sofia_private; switch_copy_string(tech_pvt->sofia_private->uuid, switch_core_session_get_uuid(session), sizeof(tech_pvt->sofia_private->uuid)); nua_handle_bind(tech_pvt->nh, tech_pvt->sofia_private); @@ -5352,6 +5352,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName switch_channel_set_variable(channel, "sip_handle_full_to", switch_channel_get_variable(channel, "sip_full_to")); } else { + tech_pvt->redirected = switch_core_session_sprintf(session, "sip:%s", switch_channel_get_variable(channel, "sip_contact_uri")); switch_channel_set_variable_printf(channel, "sip_invite_route_uri", "", switch_channel_get_variable(channel, "sip_from_user"), @@ -5371,6 +5372,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName tech_pvt->dest_to = tech_pvt->dest; + sofia_glue_attach_private(session, h->profile, tech_pvt, NULL); switch_channel_set_name(tech_pvt->channel, switch_channel_get_variable(channel, "channel_name"));