From 425cd807d3762dad049419e1636f45c3a67bb2b1 Mon Sep 17 00:00:00 2001 From: niubinghui Date: Fri, 6 Sep 2024 18:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91BugFix?= =?UTF-8?q?=EF=BC=9A=E5=9F=BA=E7=A1=80=E5=8A=9F=E8=83=BD=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=B8=AD=E5=8F=91=E7=8E=B0=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua_binding_function.c | 16 ++++---- src/lua_binding_function.h | 24 +++++------ src/lua_plugin_cfunc.c | 44 +++++++++++++++------ src/lua_plugin_manage.c | 68 +++++++++++++++++--------------- src/lua_plugin_manage_internal.h | 24 +++++------ 5 files changed, 98 insertions(+), 78 deletions(-) diff --git a/src/lua_binding_function.c b/src/lua_binding_function.c index 620a49c..81be039 100644 --- a/src/lua_binding_function.c +++ b/src/lua_binding_function.c @@ -101,6 +101,7 @@ int lua_session_plugin_regist(struct lua_state *state) struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); LL_APPEND(plugin_manage->session_plugin_env_list, new_plugin_env); new_plugin_env->plugin_manage = plugin_manage; + new_plugin_env->session_plugin_id = plugin_id; lua_settop(L, 0); lua_pushinteger(L, plugin_id); @@ -146,6 +147,7 @@ int lua_packet_plugin_regist(struct lua_state *state) struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); LL_APPEND(plugin_manage->packet_plugin_env_list, new_plugin_env); new_plugin_env->plugin_manage = plugin_manage; + new_plugin_env->packet_plugin_id = plugin_id; lua_settop(L, 0); lua_pushinteger(L, plugin_id); @@ -210,9 +212,9 @@ static int lua_plugin_manage_msg_mq_create_topic(struct lua_state *state, int ty new_message_free_arg->lua_msg_free_arg_ref_id = free_arg_ref_id; int topic_id = -1; if (type == MQ_TYPE_PACKET) - stellar_packet_mq_create_topic(st, (const char *)topic_name, lpm_packet_message_free_func, new_message_free_arg); + topic_id = stellar_packet_mq_create_topic(st, (const char *)topic_name, lpm_packet_message_free_func, new_message_free_arg); else if (type == MQ_TYPE_SESSION) - stellar_session_mq_create_topic(st, (const char *)topic_name, lpm_session_message_free_func, new_message_free_arg); + topic_id = stellar_session_mq_create_topic(st, (const char *)topic_name, lpm_session_message_free_func, new_message_free_arg); if (topic_id >= 0) { struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); @@ -248,9 +250,9 @@ static int lua_plugin_manage_msg_mq_get_topic_id(struct lua_state *state, int ty int topic_id = -1; if (type == MQ_TYPE_PACKET) - stellar_packet_mq_get_topic_id(st, (const char *)topic_name); + topic_id = stellar_packet_mq_get_topic_id(st, (const char *)topic_name); else if (type == MQ_TYPE_SESSION) - stellar_session_mq_get_topic_id(st, (const char *)topic_name); + topic_id = stellar_session_mq_get_topic_id(st, (const char *)topic_name); if (topic_name) free(topic_name); @@ -351,7 +353,7 @@ static int lua_plugin_manage_msg_mq_subscribe_topic(struct lua_state *state, int int on_message_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) - on_message_fn_ref_id = luaL_ref(L, -1); + on_message_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); @@ -405,7 +407,7 @@ static int lua_plugin_manage_msg_mq_publish_message(struct lua_state *state, int if (lua_gettop(L) != 3) goto err; - int msg_ref_id = luaL_ref(L, -1); + int msg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (msg_ref_id == LUA_REFNIL) msg_ref_id = 0; @@ -415,7 +417,7 @@ static int lua_plugin_manage_msg_mq_publish_message(struct lua_state *state, int void *p = (void *)lua_topointer(L, -1); lua_settop(L, 0); - struct lua_context *new_context = (struct lua_context *)calloc(1, sizeof(struct lua_context)); + struct lua_context *new_context = lua_context_new(state); new_context->lua_context_ref_id = msg_ref_id; int publish_result = -1; diff --git a/src/lua_binding_function.h b/src/lua_binding_function.h index 0a063ae..b031c08 100644 --- a/src/lua_binding_function.h +++ b/src/lua_binding_function.h @@ -8,9 +8,9 @@ /* ***** ***** ***** ***** ***** ***** */ /* 需要注册至lua中供lua调用的所有函数原型 */ /* 通用函数 */ -int lua_get_worker_thread_num(struct lua_state * state); -int lua_get_current_thread_id(struct lua_state * state); -int lua_get_stellar_pointer(struct lua_state * state); +int lua_get_worker_thread_num(struct lua_state *state); +int lua_get_current_thread_id(struct lua_state *state); +int lua_get_stellar_pointer(struct lua_state *state); // int lua_get_plugin_manage_pointer(struct lua_state *state); /* 注册函数 */ @@ -21,12 +21,12 @@ int lua_packet_plugin_regist(struct lua_state *state); int lua_session_get_type(struct lua_state *state); /* packet message mq相关的函数 */ -int lua_packet_mq_create_topic(struct lua_state * state); -int lua_packet_mq_get_topic_id(struct lua_state * state); -int lua_packet_mq_update_topic(struct lua_state * state); -int lua_packet_mq_destory_topic(struct lua_state * state); -int lua_packet_mq_subscribe_topic(struct lua_state * state); -int lua_packet_mq_publish_message(struct lua_state * state); +int lua_packet_mq_create_topic(struct lua_state *state); +int lua_packet_mq_get_topic_id(struct lua_state *state); +int lua_packet_mq_update_topic(struct lua_state *state); +int lua_packet_mq_destory_topic(struct lua_state *state); +int lua_packet_mq_subscribe_topic(struct lua_state *state); +int lua_packet_mq_publish_message(struct lua_state *state); /* session message mq相关的函数 */ int lua_session_mq_create_topic(struct lua_state *state); @@ -45,6 +45,6 @@ void *lpm_ctx_new_func(struct session *sess, void *plugin_env); void lpm_ctx_free_func(struct session *sess, void *sess_ctx, void *plugin_env); void lpm_session_message_free_func(struct session *sess, void *msg, void *msg_free_arg); void lpm_on_session_msg_func(struct session *sess, int topic_id, const void *msg, void *sess_ctx, void *plugin_env); -void lpm_on_packet_func(struct packet * pkt, unsigned char ip_protocol, void * plugin_env); -void lpm_packet_message_free_func(struct packet *pkt, void * msg, void * msg_free_arg); -void lpm_on_packet_msg_func(struct packet *pkt, int topic_id, const void *msg, void * plugin_env); \ No newline at end of file +void lpm_on_packet_func(struct packet *pkt, unsigned char ip_protocol, void *plugin_env); +void lpm_packet_message_free_func(struct packet *pkt, void *msg, void *msg_free_arg); +void lpm_on_packet_msg_func(struct packet *pkt, int topic_id, const void *msg, void *plugin_env); \ No newline at end of file diff --git a/src/lua_plugin_cfunc.c b/src/lua_plugin_cfunc.c index c3363a1..4115453 100644 --- a/src/lua_plugin_cfunc.c +++ b/src/lua_plugin_cfunc.c @@ -9,7 +9,7 @@ void *lpm_ctx_new_func(struct session *sess, void *plugin_env) struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = (struct lua_state *)plugin_manage->state[thread_id]; + struct lua_state *state = (struct lua_state *)plugin_manage->state_array[thread_id]; struct lua_context *new_context = lua_context_new(state); struct lua_cdata param[3] = {0}; @@ -37,7 +37,7 @@ void lpm_ctx_free_func(struct session *sess, void *sess_ctx, void *plugin_env) struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[3] = {0}; param[0].type = DATATYPE_POINTER; @@ -61,7 +61,7 @@ void lpm_session_message_free_func(struct session *sess, void *msg, void *msg_fr struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[3] = {0}; param[0].type = DATATYPE_POINTER; @@ -87,16 +87,25 @@ void lpm_on_session_msg_func(struct session *sess, int topic_id, const void *msg struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[5] = {0}; param[0].type = DATATYPE_POINTER; param[0].pointer = sess; param[1].type = DATATYPE_INT; param[1].integer = topic_id; - /* 后续统一为相同结构 */ - param[2].type = DATATYPE_CONTEXT; - param[2].context = (struct lua_context *)msg; + /* 这里是不是有更好的办法 */ + struct lua_context *context = (struct lua_context *)msg; + if (context && context->magic_code == context_magic_code) + { + param[2].type = DATATYPE_CONTEXT; + param[2].context = context; + } + else + { + param[2].type = DATATYPE_POINTER; + param[2].pointer = (void *)msg; + } param[3].type = DATATYPE_CONTEXT; param[3].context = (struct lua_context *)sess_ctx; param[4].type = DATATYPE_TABLE; @@ -116,7 +125,7 @@ void lpm_on_packet_func(struct packet *pkt, unsigned char ip_protocol, void *plu struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[3] = {0}; param[0].type = DATATYPE_POINTER; @@ -139,7 +148,7 @@ void lpm_packet_message_free_func(struct packet *pkt, void *msg, void *msg_free_ struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[3] = {0}; param[0].type = DATATYPE_POINTER; @@ -165,16 +174,25 @@ void lpm_on_packet_msg_func(struct packet *pkt, int topic_id, const void *msg, v struct stellar *st = plugin_manage->st; int thread_id = stellar_get_current_thread_id(st); - struct lua_state *state = plugin_manage->state[thread_id]; + struct lua_state *state = plugin_manage->state_array[thread_id]; struct lua_cdata param[4] = {0}; param[0].type = DATATYPE_POINTER; param[0].pointer = pkt; param[1].type = DATATYPE_INT; param[1].integer = topic_id; - /* 后续统一为相同结构 */ - param[2].type = DATATYPE_CONTEXT; - param[2].context = (struct lua_context *)msg; + /* 这里是不是有更好的办法 */ + struct lua_context *context = (struct lua_context *)msg; + if (context && context->magic_code == context_magic_code) + { + param[2].type = DATATYPE_CONTEXT; + param[2].context = (struct lua_context *)msg; + } + else + { + param[2].type = DATATYPE_POINTER; + param[2].pointer = (void *)msg; + } param[3].type = DATATYPE_TABLE; param[3].table = env->lua_plug_env_ref_id; diff --git a/src/lua_plugin_manage.c b/src/lua_plugin_manage.c index 5bdd2f7..d8c7adb 100644 --- a/src/lua_plugin_manage.c +++ b/src/lua_plugin_manage.c @@ -228,18 +228,20 @@ int lua_cbinding_data(struct lua_state *state, struct lua_bind_data_spec bind_da /* ***** ***** ***** ***** ***** ***** */ /* context */ /* ***** ***** ***** ***** ***** ***** */ +const char *context_magic_code = "ctx_code"; struct lua_context *lua_context_new(struct lua_state *state) { if (!state) return NULL; lua_State *L = (lua_State *)state; + struct lua_context *new_context = (struct lua_context *)calloc(1, sizeof(struct lua_context)); + new_context->magic_code = context_magic_code; + new_context->state = state; lua_newtable(L); int ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (ref_id == LUA_REFNIL) return NULL; - - struct lua_context *new_context = (struct lua_context *)calloc(1, sizeof(struct lua_context)); new_context->lua_context_ref_id = ref_id; return new_context; } @@ -494,7 +496,7 @@ int lua_ctable_pop_stack(struct lua_state *state, struct lua_ctable *ctable) if (numkey > 0 && numkey <= (int)ctable->array_size) ctable->node_size--; } - lua_pop(L, -1); + lua_pop(L, 1); } ctable->array_data = (struct lua_cdata **)calloc(ctable->array_size, sizeof(struct lua_cdata *)); @@ -820,11 +822,11 @@ struct lua_plugin_manage *lua_plugin_manage_init( if (thread_count <= 0) return new_plugin_manage; new_plugin_manage->state_num = thread_count; - new_plugin_manage->state = (struct lua_state **)calloc(thread_count, sizeof(struct lua_state *)); - memset(new_plugin_manage->state, 0, thread_count * sizeof(struct lua_state *)); + new_plugin_manage->state_array = (struct lua_state **)calloc(thread_count, sizeof(struct lua_state *)); + memset(new_plugin_manage->state_array, 0, thread_count * sizeof(struct lua_state *)); for (int thread_index = 0; thread_index < thread_count; thread_index++) { - new_plugin_manage->state[thread_index] = thread_state_init(thread_index, st, new_plugin_manage); + new_plugin_manage->state_array[thread_index] = thread_state_init(thread_index, st, new_plugin_manage); } for (unsigned on_message_index = 0; on_message_index < HASH_MAX_NUM; on_message_index++) @@ -833,13 +835,13 @@ struct lua_plugin_manage *lua_plugin_manage_init( new_plugin_manage->on_packet_message_hashlist[on_message_index].on_use = on_message_index; } - new_plugin_manage->load_script = (struct lua_load_script *)calloc(specific_num, sizeof(struct lua_load_script)); + new_plugin_manage->load_script_array = (struct lua_load_script *)calloc(specific_num, sizeof(struct lua_load_script)); new_plugin_manage->load_script_num = specific_num; for (unsigned spec_index = 0; spec_index < specific_num; spec_index++) { - if (lua_state_load_one_specific(new_plugin_manage->state[0], + if (lua_state_load_one_specific(new_plugin_manage->state_array[0], &specific[spec_index], - &new_plugin_manage->load_script[spec_index], + &new_plugin_manage->load_script_array[spec_index], st, errlog, sizeof(errlog))) @@ -849,22 +851,23 @@ struct lua_plugin_manage *lua_plugin_manage_init( } } - lua_newtable((lua_State *)new_plugin_manage->state[0]); - int lua_ref_max_id = luaL_ref((lua_State *)new_plugin_manage->state[0], LUA_REGISTRYINDEX); + lua_newtable((lua_State *)new_plugin_manage->state_array[0]); + int lua_ref_max_id = luaL_ref((lua_State *)new_plugin_manage->state_array[0], LUA_REGISTRYINDEX); struct lua_cdata *trans_data = lua_cdata_new(); for (int ref_index = (LUA_GLOBAL_VALUE_REF_ID + 1); ref_index < lua_ref_max_id; ref_index++) { - lua_settop((lua_State *)new_plugin_manage->state[0], 0); - lua_rawgeti((lua_State *)new_plugin_manage->state[0], LUA_REGISTRYINDEX, ref_index); - lua_cdata_pop_stack(new_plugin_manage->state[0], trans_data); + lua_settop((lua_State *)new_plugin_manage->state_array[0], 0); + lua_rawgeti((lua_State *)new_plugin_manage->state_array[0], LUA_REGISTRYINDEX, ref_index); + lua_cdata_pop_stack(new_plugin_manage->state_array[0], trans_data); for (int thread_index = 1; thread_index < thread_count; thread_index++) { - lua_cdata_push_stack(new_plugin_manage->state[thread_index], trans_data); - int ref_id = luaL_ref((lua_State *)new_plugin_manage->state[thread_index], LUA_REGISTRYINDEX); + lua_cdata_push_stack(new_plugin_manage->state_array[thread_index], trans_data); + int ref_id = luaL_ref((lua_State *)new_plugin_manage->state_array[thread_index], LUA_REGISTRYINDEX); printf("ref to new thread, ref id is %d, %d\n", ref_index, ref_id); } lua_cdata_inner_data_free(trans_data); } + luaL_unref((lua_State *)new_plugin_manage->state_array[0], LUA_REGISTRYINDEX, lua_ref_max_id); return new_plugin_manage; @@ -878,38 +881,41 @@ void lua_plugin_manage_exit(struct lua_plugin_manage *lua_plug_mgr) if (!lua_plug_mgr) return; - for (unsigned script_index = 0; script_index < lua_plug_mgr->load_script_num; script_index ++) + for (unsigned script_index = 0; script_index < lua_plug_mgr->load_script_num; script_index++) { struct lua_cdata param; param.type = DATATYPE_TABLE; - param.table = lua_plug_mgr->load_script[script_index].lua_script_env_ref_id; - lua_chunk_execute(lua_plug_mgr->state[0], lua_plug_mgr->load_script[script_index].lua_unload_fn_ref_id, ¶m, 1, NULL, 0, NULL, 0); + param.table = lua_plug_mgr->load_script_array[script_index].lua_script_env_ref_id; + lua_chunk_execute(lua_plug_mgr->state_array[0], lua_plug_mgr->load_script_array[script_index].lua_unload_fn_ref_id, ¶m, 1, NULL, 0, NULL, 0); } - free(lua_plug_mgr->load_script); + free(lua_plug_mgr->load_script_array); for (unsigned state_index = 0; state_index < lua_plug_mgr->state_num; ++state_index) { - lua_close((lua_State *)lua_plug_mgr->state[state_index]); + lua_close((lua_State *)lua_plug_mgr->state_array[state_index]); } - free(lua_plug_mgr->state); + free(lua_plug_mgr->state_array); - struct lua_session_plugin_env * session_env = lua_plug_mgr->session_plugin_env_list; - while (session_env) { - struct lua_session_plugin_env * next = session_env->next; + struct lua_session_plugin_env *session_env = lua_plug_mgr->session_plugin_env_list; + while (session_env) + { + struct lua_session_plugin_env *next = session_env->next; free(session_env); session_env = next; } - struct lua_packet_plugin_env * packet_env = lua_plug_mgr->packet_plugin_env_list; - while (packet_env) { - struct lua_packet_plugin_env * next = packet_env->next; + struct lua_packet_plugin_env *packet_env = lua_plug_mgr->packet_plugin_env_list; + while (packet_env) + { + struct lua_packet_plugin_env *next = packet_env->next; free(packet_env); packet_env = next; } - struct lua_message_free_arg * message_free_arg = lua_plug_mgr->message_free_arg_list; - while (message_free_arg) { - struct lua_message_free_arg * next = message_free_arg->next; + struct lua_message_free_arg *message_free_arg = lua_plug_mgr->message_free_arg_list; + while (message_free_arg) + { + struct lua_message_free_arg *next = message_free_arg->next; free(message_free_arg); message_free_arg = next; } diff --git a/src/lua_plugin_manage_internal.h b/src/lua_plugin_manage_internal.h index 57f6e62..18ac5b6 100644 --- a/src/lua_plugin_manage_internal.h +++ b/src/lua_plugin_manage_internal.h @@ -50,8 +50,10 @@ struct lua_bind_function_spec }; int lua_cbinding_function(struct lua_state *state, struct lua_bind_function_spec bind_function[], size_t bind_func_num); +extern const char *context_magic_code; struct lua_context { + const char *magic_code; struct lua_state *state; int lua_context_ref_id; }; @@ -159,7 +161,7 @@ struct lua_session_plugin_env struct lua_packet_plugin_env { struct lua_packet_plugin_env *next; - struct lua_plugin_manage * plugin_manage; + struct lua_plugin_manage *plugin_manage; int packet_plugin_id; int lua_on_packet_fn_ref_id; int lua_plug_env_ref_id; @@ -168,7 +170,7 @@ struct lua_packet_plugin_env struct lua_message_free_arg { struct lua_message_free_arg *next; - struct lua_plugin_manage * plugin_manage; + struct lua_plugin_manage *plugin_manage; int topic_id; int lua_msg_free_fn_ref_id; int lua_msg_free_arg_ref_id; @@ -184,8 +186,8 @@ struct lua_on_message_fn #define HASH_MAX_NUM 1024 int calc_on_message_func_hash_key(int topic_id, int plugin_id); -struct lua_on_message_fn * hash_on_msg_fn_insert(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id); -struct lua_on_message_fn * hash_find_on_msg_fn(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id); +struct lua_on_message_fn *hash_on_msg_fn_insert(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id); +struct lua_on_message_fn *hash_find_on_msg_fn(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id); #define LUA_GLOBAL_THREAD_ID_KEY "__global_thread_id" #define LUA_GLOBAL_STELLAR_POINTER "__global_stellar_pointer" @@ -193,7 +195,7 @@ struct lua_on_message_fn * hash_find_on_msg_fn(struct lua_on_message_fn msg_fn_h // int lua_state_get_thread_id(struct lua_state * state); // struct stellar * lua_state_get_stellar(struct lua_state * state); -struct lua_plugin_manage * lua_state_get_plugin_manage(struct lua_state * state); +struct lua_plugin_manage *lua_state_get_plugin_manage(struct lua_state *state); struct lua_plugin_manage { @@ -201,12 +203,9 @@ struct lua_plugin_manage size_t state_num; size_t load_script_num; - struct lua_state **state; - struct lua_load_script *load_script; + struct lua_state **state_array; + struct lua_load_script *load_script_array; - // UT_array *session_plugin_env; - // UT_array *packet_plugin_env; - // UT_array *message_free_arg; struct lua_session_plugin_env *session_plugin_env_list; struct lua_packet_plugin_env *packet_plugin_env_list; struct lua_message_free_arg *message_free_arg_list; @@ -215,9 +214,4 @@ struct lua_plugin_manage struct lua_on_message_fn on_packet_message_hashlist[HASH_MAX_NUM]; }; -struct lua_plugin_manage *lua_plugin_manage_init(struct stellar *st, struct lua_config_spec specifics[], size_t specific_count); -void lua_plugin_manage_exit(struct lua_plugin_manage *lua_plug_mgr); - - - #endif \ No newline at end of file