mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-10-30 15:14:17 +00:00
FS-10167: Refactored simclist for ks-ification, and adjusted it for pool-based allocations and cleanup
This commit is contained in:
1 parent
8957953741
commit
075cd0d179
7 files changed
+443
-403
No files matched your search
@@ -43,7 +43,7 @@ struct blade_module_chat_s {
|
||||
blade_module_callbacks_t *module_callbacks;
|
||||
|
||||
const char *session_state_callback_id;
|
||||
list_t participants;
|
||||
ks_list_t *participants;
|
||||
};
|
||||
|
||||
|
||||
@@ -87,7 +87,9 @@ ks_status_t blade_module_chat_create(blade_module_chat_t **bm_chatP, blade_handl
|
||||
bm_chat->pool = pool;
|
||||
bm_chat->tpool = blade_handle_tpool_get(bh);
|
||||
bm_chat->session_state_callback_id = NULL;
|
||||
list_init(&bm_chat->participants);
|
||||
|
||||
ks_list_create(&bm_chat->participants, pool);
|
||||
ks_assert(bm_chat->participants);
|
||||
|
||||
blade_module_create(&bm_chat->module, bh, bm_chat, &g_module_chat_callbacks);
|
||||
bm_chat->module_callbacks = &g_module_chat_callbacks;
|
||||
@@ -110,7 +112,7 @@ ks_status_t blade_module_chat_destroy(blade_module_chat_t **bm_chatP)
|
||||
|
||||
blade_module_chat_on_shutdown(bm_chat->module);
|
||||
|
||||
list_destroy(&bm_chat->participants);
|
||||
ks_list_destroy(&bm_chat->participants);
|
||||
|
||||
blade_module_destroy(&bm_chat->module);
|
||||
|
||||
@@ -257,7 +259,7 @@ void blade_module_chat_on_session_state(blade_session_t *bs, blade_session_state
|
||||
|
||||
cJSON_DeleteItemFromObject(props, "blade.chat.participant");
|
||||
|
||||
list_delete(&bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
ks_list_delete(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +298,7 @@ ks_bool_t blade_chat_join_request_handler(blade_module_t *bm, blade_request_t *b
|
||||
if (props_participant) props_participant->type = cJSON_True;
|
||||
else cJSON_AddTrueToObject(props, "blade.chat.participant");
|
||||
|
||||
list_append(&bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and cleanup when removed
|
||||
ks_list_append(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and cleanup when removed
|
||||
|
||||
blade_rpc_response_create(breq->pool, &res, NULL, breq->message_id);
|
||||
|
||||
@@ -347,7 +349,7 @@ ks_bool_t blade_chat_leave_request_handler(blade_module_t *bm, blade_request_t *
|
||||
|
||||
cJSON_DeleteItemFromObject(props, "blade.chat.participant");
|
||||
|
||||
list_delete(&bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
ks_list_delete(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
|
||||
blade_rpc_response_create(breq->pool, &res, NULL, breq->message_id);
|
||||
|
||||
@@ -411,7 +413,7 @@ ks_bool_t blade_chat_send_request_handler(blade_module_t *bm, blade_request_t *b
|
||||
cJSON_AddStringToObject(res, "from", breq->session_id); // @todo should really be the identity, but we don't have that in place yet
|
||||
cJSON_AddStringToObject(res, "message", message);
|
||||
|
||||
blade_handle_sessions_send(breq->handle, &bm_chat->participants, NULL, event);
|
||||
blade_handle_sessions_send(breq->handle, bm_chat->participants, NULL, event);
|
||||
|
||||
cJSON_Delete(event);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ struct blade_module_wss_s {
|
||||
struct pollfd *listeners_poll;
|
||||
int32_t listeners_count;
|
||||
|
||||
list_t connected; // @todo consider keeping this only as the list of connection id's, since the handle retains the pointer lookup
|
||||
ks_list_t *connected; // @todo consider keeping this only as the list of connection id's, since the handle retains the pointer lookup
|
||||
};
|
||||
|
||||
struct blade_transport_wss_s {
|
||||
@@ -176,7 +176,8 @@ ks_status_t blade_module_wss_create(blade_module_wss_t **bm_wssP, blade_handle_t
|
||||
bm_wss->module_callbacks = &g_module_wss_callbacks;
|
||||
bm_wss->transport_callbacks = &g_transport_wss_callbacks;
|
||||
|
||||
list_init(&bm_wss->connected);
|
||||
ks_list_create(&bm_wss->connected, pool);
|
||||
ks_assert(bm_wss->connected);
|
||||
|
||||
*bm_wssP = bm_wss;
|
||||
|
||||
@@ -198,7 +199,7 @@ ks_status_t blade_module_wss_destroy(blade_module_wss_t **bm_wssP)
|
||||
|
||||
blade_module_destroy(&bm_wss->module);
|
||||
|
||||
list_destroy(&bm_wss->connected);
|
||||
ks_list_destroy(&bm_wss->connected);
|
||||
|
||||
ks_pool_free(bm_wss->pool, bm_wssP);
|
||||
|
||||
@@ -470,15 +471,15 @@ KS_DECLARE(ks_status_t) blade_module_wss_on_shutdown(blade_module_t *bm)
|
||||
bm_wss->listeners_count = 0;
|
||||
if (bm_wss->listeners_poll) ks_pool_free(bm_wss->pool, &bm_wss->listeners_poll);
|
||||
|
||||
if (list_size(&bm_wss->connected) > 0) {
|
||||
if (ks_list_size(bm_wss->connected) > 0) {
|
||||
// this approach to shutdown is cleaner, ensures connections will detach from sessions and be destroyed all in the same places
|
||||
list_iterator_start(&bm_wss->connected);
|
||||
while (list_iterator_hasnext(&bm_wss->connected)) {
|
||||
bc = (blade_connection_t *)list_iterator_next(&bm_wss->connected);
|
||||
ks_list_iterator_start(bm_wss->connected);
|
||||
while (ks_list_iterator_hasnext(bm_wss->connected)) {
|
||||
bc = (blade_connection_t *)ks_list_iterator_next(bm_wss->connected);
|
||||
blade_connection_disconnect(bc);
|
||||
}
|
||||
list_iterator_stop(&bm_wss->connected);
|
||||
while (list_size(&bm_wss->connected) > 0) ks_sleep_ms(100);
|
||||
ks_list_iterator_stop(bm_wss->connected);
|
||||
while (ks_list_size(bm_wss->connected) > 0) ks_sleep_ms(100);
|
||||
}
|
||||
|
||||
if (stopped) ks_log(KS_LOG_DEBUG, "Stopped\n");
|
||||
@@ -590,7 +591,7 @@ void *blade_module_wss_listeners_thread(ks_thread_t *thread, void *data)
|
||||
ks_log(KS_LOG_DEBUG, "Connection (%s) started\n", blade_connection_id_get(bc));
|
||||
|
||||
blade_handle_connections_add(bc);
|
||||
list_append(&bm_wss->connected, bc);
|
||||
ks_list_append(bm_wss->connected, bc);
|
||||
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_NEW);
|
||||
|
||||
blade_connection_read_unlock(bc);
|
||||
@@ -726,7 +727,7 @@ ks_status_t blade_transport_wss_on_connect(blade_connection_t **bcP, blade_modul
|
||||
// @todo make sure it's sensible to be mixing outbound and inbound connections in the same list, but this allows entering the destruction pipeline
|
||||
// for module shutdown, disconnects and errors without special considerations
|
||||
blade_handle_connections_add(bc);
|
||||
list_append(&bm_wss->connected, bc);
|
||||
ks_list_append(bm_wss->connected, bc);
|
||||
|
||||
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_NEW);
|
||||
|
||||
@@ -866,7 +867,7 @@ blade_connection_state_hook_t blade_transport_wss_on_state_disconnect(blade_conn
|
||||
bt_wss = (blade_transport_wss_t *)blade_connection_transport_get(bc);
|
||||
bt_wss_init = (blade_transport_wss_init_t *)blade_connection_transport_init_get(bc);
|
||||
|
||||
list_delete(&bt_wss->module->connected, bc);
|
||||
ks_list_delete(bt_wss->module->connected, bc);
|
||||
|
||||
if (bt_wss_init) blade_transport_wss_init_destroy(&bt_wss_init);
|
||||
if (bt_wss) blade_transport_wss_destroy(&bt_wss); // @TODO: Scream at this very loudly until I feel better for it wasting 2 days to track down, and then fix the issue it's causing
|
||||
|
||||
@@ -45,7 +45,7 @@ struct blade_session_s {
|
||||
ks_mutex_t *mutex;
|
||||
ks_cond_t *cond;
|
||||
|
||||
list_t connections;
|
||||
ks_list_t *connections;
|
||||
ks_time_t ttl;
|
||||
|
||||
ks_q_t *sending;
|
||||
@@ -94,11 +94,11 @@ static void blade_session_cleanup(ks_pool_t *pool, void *ptr, void *arg, ks_pool
|
||||
//}
|
||||
//list_iterator_stop(&bs->connections);
|
||||
|
||||
// called by list_destroy()
|
||||
//list_clear(&bs->connections);
|
||||
// called by ks_list_destroy()
|
||||
//ks_list_clear(&bs->connections);
|
||||
|
||||
// @todo change bs->connections to a pointer, allocate the memory in the pool, and set a cleanup callback for it to call list_destroy() automatically
|
||||
list_destroy(&bs->connections);
|
||||
// @todo change bs->connections to a pointer, allocate the memory in the pool, and set a cleanup callback for it to call ks_list_destroy() automatically
|
||||
ks_list_destroy(&bs->connections);
|
||||
// q has auto cleanup
|
||||
//ks_q_destroy(&bs->receiving);
|
||||
//ks_q_destroy(&bs->sending);
|
||||
@@ -151,7 +151,9 @@ KS_DECLARE(ks_status_t) blade_session_create(blade_session_t **bsP, blade_handle
|
||||
ks_cond_create_ex(&bs->cond, pool, bs->mutex);
|
||||
ks_assert(bs->cond);
|
||||
|
||||
list_init(&bs->connections);
|
||||
ks_list_create(&bs->connections, pool);
|
||||
ks_assert(bs->connections);
|
||||
|
||||
ks_q_create(&bs->sending, pool, 0);
|
||||
ks_assert(bs->sending);
|
||||
ks_q_create(&bs->receiving, pool, 0);
|
||||
@@ -375,7 +377,7 @@ KS_DECLARE(ks_status_t) blade_session_connections_add(blade_session_t *bs, const
|
||||
|
||||
ks_mutex_lock(bs->mutex);
|
||||
|
||||
list_append(&bs->connections, cid);
|
||||
ks_list_append(bs->connections, cid);
|
||||
|
||||
bs->ttl = 0;
|
||||
|
||||
@@ -396,18 +398,18 @@ KS_DECLARE(ks_status_t) blade_session_connections_remove(blade_session_t *bs, co
|
||||
|
||||
ks_mutex_lock(bs->mutex);
|
||||
|
||||
size = list_size(&bs->connections);
|
||||
size = ks_list_size(bs->connections);
|
||||
for (uint32_t i = 0; i < size; ++i) {
|
||||
const char *cid = (const char *)list_get_at(&bs->connections, i);
|
||||
const char *cid = (const char *)ks_list_get_at(bs->connections, i);
|
||||
if (!strcasecmp(cid, id)) {
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) connection removed (%s)\n", bs->id, id);
|
||||
list_delete_at(&bs->connections, i);
|
||||
ks_list_delete_at(bs->connections, i);
|
||||
ks_pool_free(bs->pool, &cid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (list_size(&bs->connections) == 0) bs->ttl = ks_time_now() + (5 * KS_USEC_PER_SEC);
|
||||
if (ks_list_size(bs->connections) == 0) bs->ttl = ks_time_now() + (5 * KS_USEC_PER_SEC);
|
||||
|
||||
ks_mutex_unlock(bs->mutex);
|
||||
|
||||
@@ -428,7 +430,7 @@ ks_status_t blade_session_connections_choose(blade_session_t *bs, cJSON *json, b
|
||||
|
||||
// @todo may be multiple connections, for now let's just assume there will be only one
|
||||
// later there will need to be a way to pick which connection to use
|
||||
cid = list_get_at(&bs->connections, 0);
|
||||
cid = ks_list_get_at(bs->connections, 0);
|
||||
if (!cid) {
|
||||
// no connections available
|
||||
ret = KS_STATUS_FAIL;
|
||||
@@ -515,7 +517,7 @@ void *blade_session_state_thread(ks_thread_t *thread, void *data)
|
||||
|
||||
state = bs->state;
|
||||
|
||||
if (!list_empty(&bs->connections)) {
|
||||
if (!ks_list_empty(bs->connections)) {
|
||||
while (blade_session_sending_pop(bs, &json) == KS_STATUS_SUCCESS && json) {
|
||||
blade_connection_t *bc = NULL;
|
||||
if (blade_session_connections_choose(bs, json, &bc) == KS_STATUS_SUCCESS) {
|
||||
@@ -554,7 +556,7 @@ void *blade_session_state_thread(ks_thread_t *thread, void *data)
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (list_empty(&bs->connections) &&
|
||||
if (ks_list_empty(bs->connections) &&
|
||||
bs->ttl > 0 &&
|
||||
!blade_session_terminating(bs) &&
|
||||
ks_time_now() >= bs->ttl) {
|
||||
@@ -587,20 +589,20 @@ ks_status_t blade_session_state_on_hangup(blade_session_t *bs)
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) state hangup\n", bs->id);
|
||||
|
||||
list_iterator_start(&bs->connections);
|
||||
while (list_iterator_hasnext(&bs->connections)) {
|
||||
const char *cid = (const char *)list_iterator_next(&bs->connections);
|
||||
ks_list_iterator_start(bs->connections);
|
||||
while (ks_list_iterator_hasnext(bs->connections)) {
|
||||
const char *cid = (const char *)ks_list_iterator_next(bs->connections);
|
||||
blade_connection_t *bc = blade_handle_connections_get(bs->handle, cid);
|
||||
ks_assert(bc);
|
||||
|
||||
blade_connection_disconnect(bc);
|
||||
blade_connection_read_unlock(bc);
|
||||
}
|
||||
list_iterator_stop(&bs->connections);
|
||||
ks_list_iterator_stop(bs->connections);
|
||||
|
||||
// @todo review this code, it may end up blocking forever as the mutex for the session is locked and the connection thread will not be able to lock to remove the connection...
|
||||
// isolate another mutex for the connection list, simclist is not intrinsicly thread safe like other containers
|
||||
while (!list_empty(&bs->connections)) ks_sleep(100);
|
||||
while (!ks_list_empty(bs->connections)) ks_sleep(100);
|
||||
|
||||
blade_session_state_set(bs, BLADE_SESSION_STATE_DESTROY);
|
||||
|
||||
@@ -616,7 +618,7 @@ ks_status_t blade_session_state_on_ready(blade_session_t *bs)
|
||||
//ks_log(KS_LOG_DEBUG, "Session (%s) state ready\n", bs->id);
|
||||
|
||||
// @todo for now only process messages if there is a connection available
|
||||
if (list_size(&bs->connections) > 0) {
|
||||
if (ks_list_size(bs->connections) > 0) {
|
||||
// @todo may only want to pop once per call to give sending a chance to keep up
|
||||
while (blade_session_receiving_pop(bs, &json) == KS_STATUS_SUCCESS && json) {
|
||||
blade_session_process(bs, json);
|
||||
@@ -664,7 +666,7 @@ KS_DECLARE(ks_status_t) blade_session_send(blade_session_t *bs, cJSON *json, bla
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) sending response (%s)\n", bs->id, id);
|
||||
}
|
||||
|
||||
if (list_empty(&bs->connections)) {
|
||||
if (ks_list_empty(bs->connections)) {
|
||||
blade_session_sending_push(bs, json);
|
||||
} else {
|
||||
blade_connection_t *bc = NULL;
|
||||
|
||||
@@ -711,7 +711,7 @@ KS_DECLARE(ks_status_t) blade_handle_sessions_remove(blade_session_t *bs)
|
||||
return ret;
|
||||
}
|
||||
|
||||
KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, list_t *sessions, const char *exclude, cJSON *json)
|
||||
KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, ks_list_t *sessions, const char *exclude, cJSON *json)
|
||||
{
|
||||
blade_session_t *bs = NULL;
|
||||
|
||||
@@ -719,9 +719,9 @@ KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, list_t *sessions
|
||||
ks_assert(sessions);
|
||||
ks_assert(json);
|
||||
|
||||
list_iterator_start(sessions);
|
||||
while (list_iterator_hasnext(sessions)) {
|
||||
const char *sessionid = list_iterator_next(sessions);
|
||||
ks_list_iterator_start(sessions);
|
||||
while (ks_list_iterator_hasnext(sessions)) {
|
||||
const char *sessionid = ks_list_iterator_next(sessions);
|
||||
if (exclude && !strcmp(exclude, sessionid)) continue;
|
||||
bs = blade_handle_sessions_get(bh, sessionid);
|
||||
if (!bs) {
|
||||
@@ -731,7 +731,7 @@ KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, list_t *sessions
|
||||
blade_session_send(bs, json, NULL);
|
||||
blade_session_read_unlock(bs);
|
||||
}
|
||||
list_iterator_stop(sessions);
|
||||
ks_list_iterator_stop(sessions);
|
||||
}
|
||||
|
||||
KS_DECLARE(ks_status_t) blade_handle_session_state_callback_register(blade_handle_t *bh, void *data, blade_session_state_callback_t callback, const char **id)
|
||||
|
||||
@@ -68,7 +68,7 @@ KS_DECLARE(ks_status_t) blade_handle_connections_remove(blade_connection_t *bc);
|
||||
KS_DECLARE(blade_session_t *) blade_handle_sessions_get(blade_handle_t *bh, const char *sid);
|
||||
KS_DECLARE(ks_status_t) blade_handle_sessions_add(blade_session_t *bs);
|
||||
KS_DECLARE(ks_status_t) blade_handle_sessions_remove(blade_session_t *bs);
|
||||
KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, list_t *sessions, const char *exclude, cJSON *json);
|
||||
KS_DECLARE(void) blade_handle_sessions_send(blade_handle_t *bh, ks_list_t *sessions, const char *exclude, cJSON *json);
|
||||
KS_DECLARE(ks_status_t) blade_handle_session_state_callback_register(blade_handle_t *bh, void *data, blade_session_state_callback_t callback, const char **id);
|
||||
KS_DECLARE(ks_status_t) blade_handle_session_state_callback_unregister(blade_handle_t *bh, const char *id);
|
||||
KS_DECLARE(void) blade_handle_session_state_callbacks_execute(blade_session_t *bs, blade_session_state_condition_t condition);
|
||||
|
||||
Reference in new issue
Block a user