Refactored pm_session_dettach_others to pm_session_take_over
This commit is contained in:
@@ -10,14 +10,20 @@
|
||||
* CallBack Runtime (For Per Session)
|
||||
******************************************************************************/
|
||||
|
||||
enum plugin_status
|
||||
{
|
||||
PLUGIN_STATUS_NORMAL = 0x00,
|
||||
PLUGIN_STATUS_DETTACH_ME = 0x01,
|
||||
PLUGIN_STATUS_TAKEN_OVER = 0x02, // Noitce: this is taken over not take over
|
||||
};
|
||||
|
||||
struct callback_runtime
|
||||
{
|
||||
int skip;
|
||||
enum plugin_status status;
|
||||
void *cb_args;
|
||||
|
||||
enum session_event_type event;
|
||||
fn_session_event_callback *event_cb;
|
||||
fn_session_error_callback *error_cb;
|
||||
};
|
||||
|
||||
struct session_plugin_ctx
|
||||
@@ -35,7 +41,6 @@ struct callback_static
|
||||
{
|
||||
enum session_event_type event;
|
||||
fn_session_event_callback *event_cb;
|
||||
fn_session_error_callback *error_cb;
|
||||
};
|
||||
|
||||
struct plugin_manager_eventcb
|
||||
@@ -90,10 +95,9 @@ static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin
|
||||
|
||||
for (int i = 0; i < plug_ctx->callback_num; i++)
|
||||
{
|
||||
plug_ctx->callbacks[i].skip = 0;
|
||||
plug_ctx->callbacks[i].status = PLUGIN_STATUS_NORMAL;
|
||||
plug_ctx->callbacks[i].event = elem->callbacks[i].event;
|
||||
plug_ctx->callbacks[i].event_cb = elem->callbacks[i].event_cb;
|
||||
plug_ctx->callbacks[i].error_cb = elem->callbacks[i].error_cb;
|
||||
plug_ctx->callbacks[i].cb_args = NULL;
|
||||
}
|
||||
|
||||
@@ -336,7 +340,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr)
|
||||
}
|
||||
}
|
||||
|
||||
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb)
|
||||
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb)
|
||||
{
|
||||
if (strlen(session_name) <= 0)
|
||||
{
|
||||
@@ -356,12 +360,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (error_cb == NULL)
|
||||
{
|
||||
plugin_manager_log(ERROR, "invalid parameter, the error callback corresponding to the session name '%s' is null", session_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct plugin_manager_eventcb *elem;
|
||||
HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem);
|
||||
// session_name exists, add a new cb to the end of the callbacks dynamic array
|
||||
@@ -371,7 +369,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session
|
||||
|
||||
elem->callbacks[elem->callback_num].event = event;
|
||||
elem->callbacks[elem->callback_num].event_cb = event_cb;
|
||||
elem->callbacks[elem->callback_num].error_cb = error_cb;
|
||||
|
||||
elem->callback_num++;
|
||||
}
|
||||
@@ -385,7 +382,6 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session
|
||||
|
||||
elem->callbacks[elem->callback_num].event = event;
|
||||
elem->callbacks[elem->callback_num].event_cb = event_cb;
|
||||
elem->callbacks[elem->callback_num].error_cb = error_cb;
|
||||
|
||||
elem->callback_num++;
|
||||
|
||||
@@ -431,17 +427,43 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve
|
||||
for (int i = 0; i < plug_ctx->callback_num; i++)
|
||||
{
|
||||
struct callback_runtime *runtime = &plug_ctx->callbacks[i];
|
||||
if (runtime->skip == 1)
|
||||
|
||||
if (runtime->status == PLUGIN_STATUS_DETTACH_ME)
|
||||
{
|
||||
plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'dettach me', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (runtime->event & event_type)
|
||||
else if (runtime->status == PLUGIN_STATUS_TAKEN_OVER)
|
||||
{
|
||||
plug_ctx->callback_index = i;
|
||||
plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args);
|
||||
/*
|
||||
* This plugin may be taken over when the session is open (SESSION_EVENT_OPENING),
|
||||
* and the pme may be NULL when event_cb() is run to free memory with the session is closed (SESSION_EVENT_CLOSING).
|
||||
* So, The plugin must check whether the pme is NULL before freeing memory.
|
||||
*/
|
||||
if (event_type & SESSION_EVENT_CLOSING)
|
||||
{
|
||||
plug_ctx->callback_index = i;
|
||||
plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'taken over', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
runtime->event_cb(seesion, SESSION_EVENT_CLOSING, packet, payload, payload_len, &runtime->cb_args);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'taken over', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
}
|
||||
}
|
||||
else if (runtime->status == PLUGIN_STATUS_NORMAL)
|
||||
{
|
||||
if (runtime->event & event_type)
|
||||
{
|
||||
plug_ctx->callback_index = i;
|
||||
plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, plugin status: 'normal', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args);
|
||||
}
|
||||
else
|
||||
{
|
||||
plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, plugin status: 'normal', session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -462,21 +484,29 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve
|
||||
* Public API For Plugin
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* pm_session_dettach_me just sets the flag to disable this plugin and no longer call this event callback.
|
||||
* Before calling pm_session_dettach_me, the current plugin must release related resources for the current session.
|
||||
*/
|
||||
void pm_session_dettach_me(const struct stellar_session *session)
|
||||
{
|
||||
struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session);
|
||||
assert(plugin_ctx);
|
||||
struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index];
|
||||
|
||||
/*
|
||||
* Just set the skip flag and don't call this event callback next.
|
||||
* The plugin is closed before calling pm_session_dettach_me.
|
||||
*/
|
||||
runtime_me->skip = 1;
|
||||
runtime_me->status = PLUGIN_STATUS_DETTACH_ME;
|
||||
plugin_manager_log(DEBUG, "%p dettach me, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_me->event_cb, stellar_session_get_name(session));
|
||||
}
|
||||
|
||||
void pm_session_dettach_others(const struct stellar_session *session)
|
||||
/*
|
||||
* The current plugin takes over the current session, the pm_session_take_over setting flag disables other plugins,
|
||||
* and the current session does not call other plugins except for the SESSION_EVENT_CLOSING event.
|
||||
*
|
||||
* The current plugin may be taken over while the session is open (SESSION_EVENT_OPENING),
|
||||
* other plugins event_cb is not called, the session pme on other plugins is NULL,
|
||||
* When the session is closed (SESSION_EVENT_CLOSING), call other plugin even_cb to destroy pme must check if pme is NULL
|
||||
*/
|
||||
void pm_session_take_over(const struct stellar_session *session)
|
||||
{
|
||||
struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session);
|
||||
assert(plugin_ctx);
|
||||
@@ -487,9 +517,8 @@ void pm_session_dettach_others(const struct stellar_session *session)
|
||||
if (i != plugin_ctx->callback_index)
|
||||
{
|
||||
struct callback_runtime *runtime_other = &plugin_ctx->callbacks[i];
|
||||
runtime_other->skip = 1;
|
||||
plugin_manager_log(DEBUG, "%p dettach others, run error_cb: %p, session: %s", runtime_me->event_cb, runtime_other->error_cb, stellar_session_get_name(session));
|
||||
runtime_other->error_cb(session, ERROR_EVENT_DETTACH, &runtime_other->cb_args);
|
||||
runtime_other->status = PLUGIN_STATUS_TAKEN_OVER;
|
||||
plugin_manager_log(DEBUG, "%p take over, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_other->event_cb, stellar_session_get_name(session));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user