【修改】重新组织部分数据结构,所有逻辑均改为面向数据结构的编程思路
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "stellar/module_manager.h"
|
||||
|
||||
#define LUA_MODULE_MANAGE_MODULE_NAME "lua_moduel_manager_module"
|
||||
#define LUA_MODULE_MANAGE_CONF_PATH "lua_module_manage.toml"
|
||||
|
||||
typedef void lua_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg, void *after_dispatch_arg);
|
||||
typedef void lua_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg, void *after_dispatch_arg);
|
||||
|
||||
struct stellar_module *lua_module_manager_on_init(struct stellar_module_manager *mod_mgr);
|
||||
void lua_module_manager_on_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod);
|
||||
struct stellar_module *lua_module_manager_on_init(struct stellar_module_manager *mod_mgr);
|
||||
void lua_module_manager_on_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod);
|
||||
|
||||
/* 提供的通用函数 */
|
||||
/* 通用的Lua mq订阅函数, on_msg */
|
||||
/* 注册新数据及自定义函数的接口,能够传参或获取返回值 */
|
||||
|
||||
/* 提供的通用函数 */
|
||||
/* 通用的Lua mq订阅函数, on_msg */
|
||||
/* 注册新数据及自定义函数的接口,能够传参或获取返回值 */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,14 +1,13 @@
|
||||
#include "lua_binding_cfunc.h"
|
||||
#include "lua_module_manage_internal.h"
|
||||
#include "lua_binding_cfunc.h"
|
||||
|
||||
static inline struct lua_state *lua_fn_arg_pair_get_thread_state(struct lua_fn_arg_pair *pair)
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = pair->lua_mod_mgr;
|
||||
int thread_id = stellar_module_manager_get_thread_id(lua_mod_mgr->mod_mgr);
|
||||
return lua_mod_mgr->state_array[thread_id];
|
||||
struct lua_module_manager *lua_mod_mgr = lua_fn_arg_pair_get_lua_mod_mgr(pair);
|
||||
return lua_module_manager_get_current_thread_state(lua_mod_mgr);
|
||||
}
|
||||
|
||||
void lua_manager_mq_msg_free_cb_func(void *msg, void *msg_free_arg)
|
||||
void lua_cfunc_mq_msg_free_cb_func(void *msg, void *msg_free_arg)
|
||||
{
|
||||
if (!msg_free_arg)
|
||||
return;
|
||||
@@ -20,13 +19,13 @@ void lua_manager_mq_msg_free_cb_func(void *msg, void *msg_free_arg)
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_CONTEXT, msg, 0);
|
||||
else
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_POINTER, msg, 0);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_TABLE, NULL, free_arg->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_TABLE, NULL, lua_fn_arg_pair_get_arg_ref_id(free_arg));
|
||||
|
||||
lua_chunk_execute(state, free_arg->lua_fn_ref_id, param, 2, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(free_arg), param, 2, NULL, 0, NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void lua_manager_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg)
|
||||
void lua_cfunc_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg)
|
||||
{
|
||||
if (!on_msg_arg)
|
||||
return;
|
||||
@@ -39,13 +38,13 @@ void lua_manager_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg)
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_CONTEXT, msg, 0);
|
||||
else
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_POINTER, msg, 0);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, msg_arg->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, lua_fn_arg_pair_get_arg_ref_id(msg_arg));
|
||||
|
||||
lua_chunk_execute(state, msg_arg->lua_fn_ref_id, param, 3, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(msg_arg), param, 3, NULL, 0, NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void lua_manager_mq_on_msg_dispatch_cb_func(int topic_id, void *msg, on_msg_cb_func *on_msg_cb, void *on_msg_cb_arg, void *dispatch_arg)
|
||||
void lua_cfunc_mq_on_msg_dispatch_cb_func(int topic_id, void *msg, on_msg_cb_func *on_msg_cb, void *on_msg_cb_arg, void *dispatch_arg)
|
||||
{
|
||||
if (!dispatch_arg)
|
||||
return;
|
||||
@@ -56,17 +55,17 @@ void lua_manager_mq_on_msg_dispatch_cb_func(int topic_id, void *msg, on_msg_cb_f
|
||||
struct lua_context *new_context = lua_context_new(state);
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_INT, NULL, topic_id);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_CONTEXT, msg, 0);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, 0, dis_arg->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, 0, lua_fn_arg_pair_get_arg_ref_id(dis_arg));
|
||||
lua_cdata_data_set(¶m[3], DATATYPE_CONTEXT, (void *)new_context, 0);
|
||||
|
||||
lua_chunk_execute(state, dis_arg->lua_fn_ref_id, param, 4, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(dis_arg), param, 4, NULL, 0, NULL, 0);
|
||||
((lua_mq_on_msg_cb_func *)(void *)on_msg_cb)(topic_id, msg, on_msg_cb_arg, (void *)new_context);
|
||||
|
||||
lua_context_free(new_context);
|
||||
return;
|
||||
}
|
||||
|
||||
void lua_manager_packet_on_stage_callback(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
void lua_cfunc_packet_on_stage_callback(enum packet_stage stage, struct packet *pkt, void *args)
|
||||
{
|
||||
if (!args)
|
||||
return;
|
||||
@@ -76,13 +75,13 @@ void lua_manager_packet_on_stage_callback(enum packet_stage stage, struct packet
|
||||
struct lua_cdata param[3];
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_INT, NULL, stage);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_POINTER, pkt, 0);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, on_stage_callback->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, lua_fn_arg_pair_get_arg_ref_id(on_stage_callback));
|
||||
|
||||
lua_chunk_execute(state, on_stage_callback->lua_fn_ref_id, param, 3, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(on_stage_callback), param, 3, NULL, 0, NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void lua_manager_session_callback(struct session *sess, struct packet *pkt, void *args)
|
||||
void lua_cfunc_session_callback(struct session *sess, struct packet *pkt, void *args)
|
||||
{
|
||||
if (!args)
|
||||
return;
|
||||
@@ -92,13 +91,13 @@ void lua_manager_session_callback(struct session *sess, struct packet *pkt, void
|
||||
struct lua_cdata param[3];
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_POINTER, sess, 0);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_POINTER, pkt, 0);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, sess_callback->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_TABLE, NULL, lua_fn_arg_pair_get_arg_ref_id(sess_callback));
|
||||
|
||||
lua_chunk_execute(state, sess_callback->lua_fn_ref_id, param, 3, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(sess_callback), param, 3, NULL, 0, NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void lua_manager_tcp_stream_callback(struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len, void *args)
|
||||
void lua_cfunc_tcp_stream_callback(struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len, void *args)
|
||||
{
|
||||
if (!args)
|
||||
return;
|
||||
@@ -109,8 +108,8 @@ void lua_manager_tcp_stream_callback(struct session *sess, const char *tcp_paylo
|
||||
lua_cdata_data_set(¶m[0], DATATYPE_POINTER, sess, 0);
|
||||
lua_cdata_data_set(¶m[1], DATATYPE_BUFF, (void *)tcp_payload, (int)tcp_payload_len);
|
||||
lua_cdata_data_set(¶m[2], DATATYPE_INT, NULL, (int)tcp_payload_len);
|
||||
lua_cdata_data_set(¶m[3], DATATYPE_TABLE, NULL, tcp_stream_callback->lua_arg_ref_id);
|
||||
lua_cdata_data_set(¶m[3], DATATYPE_TABLE, NULL, lua_fn_arg_pair_get_arg_ref_id(tcp_stream_callback));
|
||||
|
||||
lua_chunk_execute(state, tcp_stream_callback->lua_fn_ref_id, param, 3, NULL, 0, NULL, 0);
|
||||
lua_state_execute_chunk(state, lua_fn_arg_pair_get_fn_ref_id(tcp_stream_callback), param, 4, NULL, 0, NULL, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
#ifndef LUA_MODULE_MANAGE_BINDING_CFUNC_H
|
||||
#define LUA_MODULE_MANAGE_BINDING_CFUNC_H
|
||||
#pragma once
|
||||
|
||||
#include "stellar/module_manager.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "stellar/mq.h"
|
||||
#include "stellar/packet_manager.h"
|
||||
#include "stellar/session_manager.h"
|
||||
|
||||
void lua_manager_mq_msg_free_cb_func(void *msg, void *msg_free_arg);
|
||||
void lua_manager_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg);
|
||||
void lua_manager_mq_on_msg_dispatch_cb_func(int topic_id, void *msg, on_msg_cb_func *on_msg_cb, void *on_msg_cb_arg, void *dispatch_arg);
|
||||
void lua_cfunc_mq_msg_free_cb_func(void *msg, void *msg_free_arg);
|
||||
void lua_cfunc_mq_on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg);
|
||||
void lua_cfunc_mq_on_msg_dispatch_cb_func(int topic_id, void *msg, on_msg_cb_func *on_msg_cb, void *on_msg_cb_arg, void *dispatch_arg);
|
||||
|
||||
void lua_manager_packet_on_stage_callback(enum packet_stage stage, struct packet *pkt, void *args);
|
||||
void lua_cfunc_packet_on_stage_callback(enum packet_stage stage, struct packet *pkt, void *args);
|
||||
|
||||
void lua_manager_session_callback(struct session *sess, struct packet *pkt, void *args);
|
||||
void lua_manager_tcp_stream_callback(struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len, void *args);
|
||||
void lua_cfunc_session_callback(struct session *sess, struct packet *pkt, void *args);
|
||||
void lua_cfunc_tcp_stream_callback(struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len, void *args);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "lua_module_manage_internal.h"
|
||||
#include "lua_binding_function.h"
|
||||
#include "lua_binding_cfunc.h"
|
||||
|
||||
@@ -19,7 +20,7 @@
|
||||
#define LUA_BINDING_FUNCTION_GET_VALUE_POINTER(func_name, param_type, value_type, value_func) \
|
||||
int func_name(struct lua_state *state) \
|
||||
{ \
|
||||
lua_State *L = (lua_State *)state; \
|
||||
lua_State *L = (lua_State *)lua_state_get_lua_State(state); \
|
||||
if (lua_gettop(L) != 1) \
|
||||
{ \
|
||||
lua_settop(L, 0); \
|
||||
@@ -35,7 +36,7 @@
|
||||
#define LUA_BINDING_FUNCTION_GET_VALUE_INT(func_name, param_type, value_func) \
|
||||
int func_name(struct lua_state *state) \
|
||||
{ \
|
||||
lua_State *L = (lua_State *)state; \
|
||||
lua_State *L = (lua_State *)lua_state_get_lua_State(state); \
|
||||
if (lua_gettop(L) != 1) \
|
||||
{ \
|
||||
lua_settop(L, 0); \
|
||||
@@ -50,7 +51,7 @@
|
||||
|
||||
int lua_mq_schema_get_topic_id(struct lua_state *state)
|
||||
{
|
||||
lua_State *L = (lua_State *)state;
|
||||
lua_State *L = (lua_State *)lua_state_get_lua_State(state);
|
||||
if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TSTRING || lua_type(L, -2) != LUA_TLIGHTUSERDATA)
|
||||
{
|
||||
lua_settop(L, 0);
|
||||
@@ -72,7 +73,7 @@ int lua_mq_schema_get_topic_id(struct lua_state *state)
|
||||
|
||||
int lua_mq_schema_create_topic(struct lua_state *state)
|
||||
{
|
||||
lua_State *L = (lua_State *)state;
|
||||
lua_State *L = (lua_State *)lua_state_get_lua_State(state);
|
||||
if (lua_gettop(L) != 6 || lua_type(L, -5) != LUA_TSTRING || lua_type(L, -6) != LUA_TLIGHTUSERDATA)
|
||||
{
|
||||
lua_settop(L, 0);
|
||||
@@ -96,27 +97,19 @@ int lua_mq_schema_create_topic(struct lua_state *state)
|
||||
struct mq_schema *s = (struct mq_schema *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_mq_dispatch_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
memset(new_mq_dispatch_arg, 0, sizeof(struct lua_fn_arg_pair));
|
||||
new_mq_dispatch_arg->lua_fn_ref_id = on_dispatch_cb_fn_ref_id;
|
||||
new_mq_dispatch_arg->lua_arg_ref_id = on_dispatch_arg_ref_id;
|
||||
struct lua_fn_arg_pair *new_mq_msg_free_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
memset(new_mq_msg_free_arg, 0, sizeof(struct lua_fn_arg_pair));
|
||||
new_mq_msg_free_arg->lua_fn_ref_id = free_cb_fn_ref_id;
|
||||
new_mq_msg_free_arg->lua_arg_ref_id = free_arg_ref_id;
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_mq_dispatch_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_dispatch_cb_fn_ref_id, on_dispatch_arg_ref_id);
|
||||
struct lua_fn_arg_pair *new_mq_msg_free_arg = lua_fn_arg_pair_new(lua_mod_mgr, free_cb_fn_ref_id, free_arg_ref_id);
|
||||
int topic_id = mq_schema_create_topic(s,
|
||||
(const char *)topic_name,
|
||||
lua_manager_mq_on_msg_dispatch_cb_func,
|
||||
lua_cfunc_mq_on_msg_dispatch_cb_func,
|
||||
new_mq_dispatch_arg,
|
||||
lua_manager_mq_msg_free_cb_func,
|
||||
lua_cfunc_mq_msg_free_cb_func,
|
||||
new_mq_msg_free_arg);
|
||||
if (topic_id >= 0)
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_mq_dispatch_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
new_mq_msg_free_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
LL_APPEND(lua_mod_mgr->mq_dispatch_list, new_mq_dispatch_arg);
|
||||
LL_APPEND(lua_mod_mgr->mq_msg_free_list, new_mq_msg_free_arg);
|
||||
lua_fn_arg_pair_insert(new_mq_dispatch_arg);
|
||||
lua_fn_arg_pair_insert(new_mq_msg_free_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -128,10 +121,8 @@ int lua_mq_schema_create_topic(struct lua_state *state)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_dispatch_arg_ref_id);
|
||||
if (on_dispatch_cb_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_dispatch_cb_fn_ref_id);
|
||||
if (new_mq_dispatch_arg)
|
||||
FREE(new_mq_dispatch_arg);
|
||||
if (new_mq_msg_free_arg)
|
||||
FREE(new_mq_msg_free_arg);
|
||||
lua_fn_arg_pair_free(new_mq_dispatch_arg);
|
||||
lua_fn_arg_pair_free(new_mq_msg_free_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, topic_id);
|
||||
@@ -164,19 +155,14 @@ int lua_mq_schema_update_topic(struct lua_state *state)
|
||||
struct mq_schema *s = (struct mq_schema *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_mq_dispatch_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
memset(new_mq_dispatch_arg, 0, sizeof(struct lua_fn_arg_pair));
|
||||
new_mq_dispatch_arg->lua_fn_ref_id = on_dispatch_cb_fn_ref_id;
|
||||
new_mq_dispatch_arg->lua_arg_ref_id = on_dispatch_arg_ref_id;
|
||||
struct lua_fn_arg_pair *new_mq_msg_free_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
memset(new_mq_msg_free_arg, 0, sizeof(struct lua_fn_arg_pair));
|
||||
new_mq_msg_free_arg->lua_fn_ref_id = free_cb_fn_ref_id;
|
||||
new_mq_msg_free_arg->lua_arg_ref_id = free_arg_ref_id;
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_mq_dispatch_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_dispatch_cb_fn_ref_id, on_dispatch_arg_ref_id);
|
||||
struct lua_fn_arg_pair *new_mq_msg_free_arg = lua_fn_arg_pair_new(lua_mod_mgr, free_cb_fn_ref_id, free_arg_ref_id);
|
||||
int update_ret = mq_schema_update_topic(s,
|
||||
topic_id,
|
||||
lua_manager_mq_on_msg_dispatch_cb_func,
|
||||
lua_cfunc_mq_on_msg_dispatch_cb_func,
|
||||
new_mq_dispatch_arg,
|
||||
lua_manager_mq_msg_free_cb_func,
|
||||
lua_cfunc_mq_msg_free_cb_func,
|
||||
new_mq_msg_free_arg);
|
||||
if (topic_id)
|
||||
{
|
||||
@@ -188,18 +174,13 @@ int lua_mq_schema_update_topic(struct lua_state *state)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_dispatch_arg_ref_id);
|
||||
if (on_dispatch_cb_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_dispatch_cb_fn_ref_id);
|
||||
if (new_mq_dispatch_arg)
|
||||
FREE(new_mq_dispatch_arg);
|
||||
if (new_mq_msg_free_arg)
|
||||
FREE(new_mq_msg_free_arg);
|
||||
lua_fn_arg_pair_free(new_mq_dispatch_arg);
|
||||
lua_fn_arg_pair_free(new_mq_msg_free_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_mq_dispatch_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
new_mq_msg_free_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
LL_APPEND(lua_mod_mgr->mq_dispatch_list, new_mq_dispatch_arg);
|
||||
LL_APPEND(lua_mod_mgr->mq_msg_free_list, new_mq_msg_free_arg);
|
||||
lua_fn_arg_pair_insert(new_mq_dispatch_arg);
|
||||
lua_fn_arg_pair_insert(new_mq_msg_free_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, update_ret);
|
||||
@@ -242,25 +223,20 @@ int lua_mq_schema_subscribe(struct lua_state *state)
|
||||
struct mq_schema *s = (struct mq_schema *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_mq_on_msg_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
memset(new_mq_on_msg_arg, 0, sizeof(struct lua_fn_arg_pair));
|
||||
new_mq_on_msg_arg->lua_arg_ref_id = on_msg_arg_ref_id;
|
||||
new_mq_on_msg_arg->lua_fn_ref_id = on_msg_fn_ref_id;
|
||||
int subscribe_ret = mq_schema_subscribe(s, topic_id, lua_manager_mq_on_msg_cb_func, new_mq_on_msg_arg);
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_mq_on_msg_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_msg_fn_ref_id, on_msg_arg_ref_id);
|
||||
int subscribe_ret = mq_schema_subscribe(s, topic_id, lua_cfunc_mq_on_msg_cb_func, new_mq_on_msg_arg);
|
||||
if (subscribe_ret)
|
||||
{
|
||||
if (on_msg_arg_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_msg_arg_ref_id);
|
||||
if (on_msg_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_msg_fn_ref_id);
|
||||
if (new_mq_on_msg_arg)
|
||||
FREE(new_mq_on_msg_arg);
|
||||
lua_fn_arg_pair_free(new_mq_on_msg_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_mq_on_msg_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
LL_APPEND(lua_mod_mgr->mq_on_msg_list, new_mq_on_msg_arg);
|
||||
lua_fn_arg_pair_insert(new_mq_on_msg_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, subscribe_ret);
|
||||
@@ -293,25 +269,30 @@ int lua_mq_runtime_publish_message(struct lua_state *state)
|
||||
return 1;
|
||||
}
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_INT(lua_module_manager_get_thread_id,
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_stellar_get_stellar_module_manager,
|
||||
struct lua_module_manager,
|
||||
struct stellar_module_manager,
|
||||
lua_module_manager_get_stellar_module_manager)
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_INT(lua_stellar_get_thread_id,
|
||||
struct stellar_module_manager,
|
||||
stellar_module_manager_get_thread_id)
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_INT(lua_module_manager_get_max_thread_num,
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_INT(lua_stellar_get_max_thread_num,
|
||||
struct stellar_module_manager,
|
||||
stellar_module_manager_get_max_thread_num)
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_module_manager_get_mq_runtime,
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_stellar_get_mq_runtime,
|
||||
struct stellar_module_manager,
|
||||
struct mq_runtime,
|
||||
stellar_module_manager_get_mq_runtime)
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_module_manager_get_mq_schema,
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_stellar_get_mq_schema,
|
||||
struct stellar_module_manager,
|
||||
struct mq_schema,
|
||||
stellar_module_manager_get_mq_schema)
|
||||
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_module_manager_get_logger,
|
||||
LUA_BINDING_FUNCTION_GET_VALUE_POINTER(lua_stellar_get_logger,
|
||||
struct stellar_module_manager,
|
||||
struct logger,
|
||||
stellar_module_manager_get_logger)
|
||||
@@ -345,24 +326,20 @@ int lua_packet_manager_subscribe(struct lua_state *state)
|
||||
struct packet_manager *pkt_mgr = (struct packet_manager *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_on_packet_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
new_on_packet_arg->lua_arg_ref_id = on_packet_stage_arg_ref_id;
|
||||
new_on_packet_arg->lua_fn_ref_id = on_packet_stage_fn_ref_id;
|
||||
int subscribe_ret = packet_manager_subscribe(pkt_mgr, (enum packet_stage)stage, lua_manager_packet_on_stage_callback, new_on_packet_arg);
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_on_packet_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_packet_stage_fn_ref_id, on_packet_stage_arg_ref_id);
|
||||
int subscribe_ret = packet_manager_subscribe(pkt_mgr, (enum packet_stage)stage, lua_cfunc_packet_on_stage_callback, new_on_packet_arg);
|
||||
if (subscribe_ret)
|
||||
{
|
||||
if (on_packet_stage_arg_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_packet_stage_arg_ref_id);
|
||||
if (on_packet_stage_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_packet_stage_fn_ref_id);
|
||||
if (new_on_packet_arg)
|
||||
FREE(new_on_packet_arg);
|
||||
lua_fn_arg_pair_free(new_on_packet_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_on_packet_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
LL_APPEND(lua_mod_mgr->on_packet_stage_list, new_on_packet_arg);
|
||||
lua_fn_arg_pair_insert(new_on_packet_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, subscribe_ret);
|
||||
@@ -397,20 +374,19 @@ static int lua_session_manager_subscribe(struct lua_state *state, enum sess_mgr_
|
||||
struct session_manager *sess_mgr = (struct session_manager *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_on_sess_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
new_on_sess_arg->lua_arg_ref_id = on_session_arg_ref_id;
|
||||
new_on_sess_arg->lua_fn_ref_id = on_session_fn_ref_id;
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_on_sess_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_session_fn_ref_id, on_session_arg_ref_id);
|
||||
int subscribe_ret = -1;
|
||||
switch (type)
|
||||
{
|
||||
case sub_type_tcp:
|
||||
subscribe_ret = session_manager_subscribe_tcp(sess_mgr, lua_manager_session_callback, new_on_sess_arg);
|
||||
subscribe_ret = session_manager_subscribe_tcp(sess_mgr, lua_cfunc_session_callback, new_on_sess_arg);
|
||||
break;
|
||||
case sub_type_udp:
|
||||
subscribe_ret = session_manager_subscribe_udp(sess_mgr, lua_manager_session_callback, new_on_sess_arg);
|
||||
subscribe_ret = session_manager_subscribe_udp(sess_mgr, lua_cfunc_session_callback, new_on_sess_arg);
|
||||
break;
|
||||
case sub_type_control_pkt:
|
||||
subscribe_ret = session_manager_subscribe_control_packet(sess_mgr, lua_manager_session_callback, new_on_sess_arg);
|
||||
subscribe_ret = session_manager_subscribe_control_packet(sess_mgr, lua_cfunc_session_callback, new_on_sess_arg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -421,27 +397,11 @@ static int lua_session_manager_subscribe(struct lua_state *state, enum sess_mgr_
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_session_arg_ref_id);
|
||||
if (on_session_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_session_fn_ref_id);
|
||||
if (new_on_sess_arg)
|
||||
FREE(new_on_sess_arg);
|
||||
lua_fn_arg_pair_free(new_on_sess_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_on_sess_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
switch (type)
|
||||
{
|
||||
case sub_type_tcp:
|
||||
LL_APPEND(lua_mod_mgr->on_session_tcp_list, new_on_sess_arg);
|
||||
break;
|
||||
case sub_type_udp:
|
||||
LL_APPEND(lua_mod_mgr->on_session_udp_list, new_on_sess_arg);
|
||||
break;
|
||||
case sub_type_control_pkt:
|
||||
LL_APPEND(lua_mod_mgr->on_session_control_packet_list, new_on_sess_arg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
lua_fn_arg_pair_insert(new_on_sess_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, subscribe_ret);
|
||||
@@ -477,24 +437,20 @@ int lua_session_manager_subscribe_tcp_stream(struct lua_state *state)
|
||||
struct session_manager *sess_mgr = (struct session_manager *)lua_topointer(L, -1);
|
||||
lua_settop(L, 0);
|
||||
|
||||
struct lua_fn_arg_pair *new_on_tcp_stream_arg = CALLOC(struct lua_fn_arg_pair, 1);
|
||||
new_on_tcp_stream_arg->lua_arg_ref_id = on_tcp_stream_arg_ref_id;
|
||||
new_on_tcp_stream_arg->lua_fn_ref_id = on_tcp_stream_fn_ref_id;
|
||||
int subscribe_ret = session_manager_subscribe_tcp_stream(sess_mgr, lua_manager_tcp_stream_callback, new_on_tcp_stream_arg);
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
struct lua_fn_arg_pair *new_on_tcp_stream_arg = lua_fn_arg_pair_new(lua_mod_mgr, on_tcp_stream_fn_ref_id, on_tcp_stream_arg_ref_id);
|
||||
int subscribe_ret = session_manager_subscribe_tcp_stream(sess_mgr, lua_cfunc_tcp_stream_callback, new_on_tcp_stream_arg);
|
||||
if (subscribe_ret)
|
||||
{
|
||||
if (on_tcp_stream_arg_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_tcp_stream_arg_ref_id);
|
||||
if (on_tcp_stream_fn_ref_id + 1)
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, on_tcp_stream_fn_ref_id);
|
||||
if (new_on_tcp_stream_arg)
|
||||
FREE(new_on_tcp_stream_arg);
|
||||
lua_fn_arg_pair_free(new_on_tcp_stream_arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct lua_module_manager *lua_mod_mgr = lua_state_get_lua_module_manager(state);
|
||||
new_on_tcp_stream_arg->lua_mod_mgr = lua_mod_mgr;
|
||||
LL_APPEND(lua_mod_mgr->on_tcp_stream_list, new_on_tcp_stream_arg);
|
||||
lua_fn_arg_pair_insert(new_on_tcp_stream_arg);
|
||||
}
|
||||
|
||||
lua_pushinteger(L, subscribe_ret);
|
||||
|
||||
@@ -1,32 +1,39 @@
|
||||
#ifndef LUA_MODULE_MANAGE_BINDING_FUNCTION_H
|
||||
#define LUA_MODULE_MANAGE_BINDING_FUNCTION_H
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "lua_module_manage_internal.h"
|
||||
|
||||
int lua_mq_schema_get_topic_id(struct lua_state *state);
|
||||
int lua_mq_schema_create_topic(struct lua_state *state);
|
||||
int lua_mq_schema_update_topic(struct lua_state *state);
|
||||
int lua_mq_shcema_destory_topic(struct lua_state *state);
|
||||
int lua_mq_schema_subscribe(struct lua_state *state);
|
||||
int lua_mq_runtime_publish_message(struct lua_state *state);
|
||||
int lua_mq_schema_get_topic_id(struct lua_state *state);
|
||||
int lua_mq_schema_create_topic(struct lua_state *state);
|
||||
int lua_mq_schema_update_topic(struct lua_state *state);
|
||||
int lua_mq_shcema_destory_topic(struct lua_state *state);
|
||||
int lua_mq_schema_subscribe(struct lua_state *state);
|
||||
int lua_mq_runtime_publish_message(struct lua_state *state);
|
||||
|
||||
int lua_module_manager_get_thread_id(struct lua_state *state);
|
||||
int lua_module_manager_get_max_thread_num(struct lua_state *state);
|
||||
int lua_module_manager_get_mq_runtime(struct lua_state *state);
|
||||
int lua_module_manager_get_mq_schema(struct lua_state *state);
|
||||
int lua_module_manager_get_logger(struct lua_state *state);
|
||||
int lua_stellar_get_stellar_module_manager(struct lua_state *state);
|
||||
int lua_stellar_get_thread_id(struct lua_state *state);
|
||||
int lua_stellar_get_max_thread_num(struct lua_state *state);
|
||||
int lua_stellar_get_mq_runtime(struct lua_state *state);
|
||||
int lua_stellar_get_mq_schema(struct lua_state *state);
|
||||
int lua_stellar_get_logger(struct lua_state *state);
|
||||
|
||||
int lua_packet_get_direction(struct lua_state *state);
|
||||
int lua_packet_get_payload(struct lua_state *state);
|
||||
int lua_packet_get_payload_len(struct lua_state *state);
|
||||
int lua_packet_get_direction(struct lua_state *state);
|
||||
int lua_packet_get_payload(struct lua_state *state);
|
||||
int lua_packet_get_payload_len(struct lua_state *state);
|
||||
|
||||
int lua_packet_manager_subscribe(struct lua_state *state);
|
||||
int lua_packet_manager_subscribe(struct lua_state *state);
|
||||
|
||||
int lua_session_get0_current_packet(struct lua_state *state);
|
||||
int lua_session_get0_current_packet(struct lua_state *state);
|
||||
|
||||
int lua_session_manager_subscribe_tcp(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_udp(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_control_packet(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_tcp_stream(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_tcp(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_udp(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_control_packet(struct lua_state *state);
|
||||
int lua_session_manager_subscribe_tcp_stream(struct lua_state *state);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -15,7 +15,7 @@
|
||||
#define SESSION_SPACE_NAME "session"
|
||||
#define SESSION_MANAGER_SPACE_NAME "session_manager"
|
||||
|
||||
struct lua_bind_func_spec lua_bind_function[] = {
|
||||
struct lua_state_cbind_func_spec lua_bind_function[] = {
|
||||
{lua_mq_schema_get_topic_id, MQ_SCHEMA_SPACE_NAME, "get_topic_id"},
|
||||
{lua_mq_schema_create_topic, MQ_SCHEMA_SPACE_NAME, "create_topic"},
|
||||
{lua_mq_schema_update_topic, MQ_SCHEMA_SPACE_NAME, "update_topic"},
|
||||
@@ -23,11 +23,11 @@ struct lua_bind_func_spec lua_bind_function[] = {
|
||||
{lua_mq_schema_subscribe, MQ_SCHEMA_SPACE_NAME, "subcribe"},
|
||||
{lua_mq_runtime_publish_message, MQ_RUNTIME_SPACE_NAME, "publish_message"},
|
||||
|
||||
{lua_module_manager_get_thread_id, MODULE_MANAGER_SPACE_NAME, "get_thread_id"},
|
||||
{lua_module_manager_get_max_thread_num, MODULE_MANAGER_SPACE_NAME, "get_max_thread_num"},
|
||||
{lua_module_manager_get_mq_runtime, MODULE_MANAGER_SPACE_NAME, "get_mq_runtime"},
|
||||
{lua_module_manager_get_mq_schema, MODULE_MANAGER_SPACE_NAME, "get_mq_schema"},
|
||||
{lua_module_manager_get_logger, MODULE_MANAGER_SPACE_NAME, "get_logger"},
|
||||
{lua_stellar_get_thread_id, MODULE_MANAGER_SPACE_NAME, "get_thread_id"},
|
||||
{lua_stellar_get_max_thread_num, MODULE_MANAGER_SPACE_NAME, "get_max_thread_num"},
|
||||
{lua_stellar_get_mq_runtime, MODULE_MANAGER_SPACE_NAME, "get_mq_runtime"},
|
||||
{lua_stellar_get_mq_schema, MODULE_MANAGER_SPACE_NAME, "get_mq_schema"},
|
||||
{lua_stellar_get_logger, MODULE_MANAGER_SPACE_NAME, "get_logger"},
|
||||
|
||||
{lua_packet_get_direction, PACKET_SPACE_NAME, "get_direction"},
|
||||
{lua_packet_get_payload, PACKET_SPACE_NAME, "get_payload"},
|
||||
@@ -46,7 +46,7 @@ struct lua_bind_func_spec lua_bind_function[] = {
|
||||
|
||||
#define ENUM_PACKET_STAGE_SPACE_NAME "packet_stage"
|
||||
|
||||
struct lua_bind_data_spec lua_bind_data[] = {
|
||||
struct lua_state_cbind_data_spec lua_bind_data[] = {
|
||||
{DATATYPE_INT, "0", ENUM_PACKET_STAGE_SPACE_NAME, "prerouting"},
|
||||
{DATATYPE_INT, "1", ENUM_PACKET_STAGE_SPACE_NAME, "input"},
|
||||
{DATATYPE_INT, "2", ENUM_PACKET_STAGE_SPACE_NAME, "forward"},
|
||||
@@ -55,76 +55,30 @@ struct lua_bind_data_spec lua_bind_data[] = {
|
||||
{DATATYPE_END, NULL, NULL, NULL},
|
||||
};
|
||||
|
||||
static void lua_module_manager_free(struct lua_module_manager *lua_mod_mgr)
|
||||
{
|
||||
if (!lua_mod_mgr)
|
||||
return;
|
||||
for (size_t script_index = 0; script_index < lua_mod_mgr->load_script_num; ++script_index)
|
||||
{
|
||||
lua_chunk_execute(lua_mod_mgr->state_array[0], lua_mod_mgr->load_script_array[script_index].lua_unload_fn_ref_id, NULL, 0, NULL, 0, NULL, 0);
|
||||
}
|
||||
for (size_t state_index = 0; state_index < lua_mod_mgr->state_num; ++state_index)
|
||||
{
|
||||
lua_state_free(lua_mod_mgr->state_array[state_index]);
|
||||
}
|
||||
FREE(lua_mod_mgr);
|
||||
return;
|
||||
}
|
||||
|
||||
struct stellar_module *lua_module_manager_on_init(struct stellar_module_manager *mod_mgr)
|
||||
{
|
||||
struct stellar_module *new_mod = NULL;
|
||||
struct lua_module_manager *new_lua_manage = CALLOC(struct lua_module_manager, 1);
|
||||
memset(new_lua_manage, 0, sizeof(struct lua_module_manager));
|
||||
new_lua_manage->mod_mgr = mod_mgr;
|
||||
struct lua_module_manager *new_lua_mod_mgr = lua_module_manager_new(mod_mgr);
|
||||
if (lua_module_manager_state_create(
|
||||
new_lua_mod_mgr,
|
||||
lua_bind_function,
|
||||
sizeof(lua_bind_function) / sizeof(struct lua_state_cbind_func_spec),
|
||||
lua_bind_data,
|
||||
sizeof(lua_bind_data) / sizeof(struct lua_state_cbind_data_spec)))
|
||||
goto err;
|
||||
|
||||
if (lua_module_manager_load_config(new_lua_mod_mgr, LUA_MODULE_MANAGE_CONF_PATH))
|
||||
goto err;
|
||||
|
||||
int thread_count = stellar_module_manager_get_max_thread_num(mod_mgr);
|
||||
if (thread_count <= 0)
|
||||
goto init_lua_module_finish;
|
||||
new_lua_manage->state_num = (size_t)thread_count;
|
||||
new_lua_manage->state_array = CALLOC(struct lua_state *, thread_count);
|
||||
memset(new_lua_manage->state_array, 0, thread_count * sizeof(struct lua_state *));
|
||||
for (size_t thread_index = 0; thread_index < new_lua_manage->state_num; ++thread_index)
|
||||
{
|
||||
new_lua_manage->state_array[thread_index] = lua_state_new(new_lua_manage);
|
||||
lua_cbinding_function(new_lua_manage->state_array[thread_index], lua_bind_function, (sizeof(lua_bind_function) / sizeof(struct lua_bind_func_spec)));
|
||||
lua_cbinding_data(new_lua_manage->state_array[thread_index], lua_bind_data, (sizeof(lua_bind_data) / sizeof(struct lua_bind_data_spec)));
|
||||
}
|
||||
|
||||
struct lua_module_spec *module_spec = lua_module_config_file_load(LUA_MODULE_MANAGE_CONF_PATH, &new_lua_manage->load_script_num);
|
||||
if (new_lua_manage->load_script_num && module_spec)
|
||||
{
|
||||
new_lua_manage->load_script_array = CALLOC(struct lua_load_script, new_lua_manage->load_script_num);
|
||||
for (size_t script_index = 0; script_index < new_lua_manage->load_script_num; ++script_index)
|
||||
{
|
||||
if (lua_load_script_load_single_specific(&new_lua_manage->load_script_array[script_index], new_lua_manage->state_array[0], &module_spec[script_index]))
|
||||
{
|
||||
lua_module_spec_free(module_spec, new_lua_manage->load_script_num);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
goto init_lua_module_finish;
|
||||
}
|
||||
lua_module_spec_free(module_spec, new_lua_manage->load_script_num);
|
||||
for (size_t script_index = 0; script_index < new_lua_manage->load_script_num; ++script_index)
|
||||
{
|
||||
lua_chunk_execute(new_lua_manage->state_array[0], new_lua_manage->load_script_array[script_index].lua_load_fn_ref_id, NULL, 0, NULL, 0, NULL, 0);
|
||||
}
|
||||
|
||||
for (size_t thread_index = 1; thread_index < new_lua_manage->state_num; ++thread_index)
|
||||
{
|
||||
lua_state_copy_ref_value(new_lua_manage->state_array[thread_index], new_lua_manage->state_array[0]);
|
||||
}
|
||||
|
||||
init_lua_module_finish:
|
||||
new_mod = stellar_module_new(LUA_MODULE_MANAGE_MODULE_NAME, (void *)new_lua_manage);
|
||||
return new_mod;
|
||||
if (lua_module_manager_call_init(new_lua_mod_mgr))
|
||||
goto err;
|
||||
|
||||
if (lua_module_manager_duplicate_state(new_lua_mod_mgr))
|
||||
goto err;
|
||||
|
||||
struct stellar_module *module = stellar_module_new(LUA_MODULE_MANAGE_MODULE_NAME, (void *)new_lua_mod_mgr);
|
||||
return module;
|
||||
err:
|
||||
if (new_lua_manage)
|
||||
lua_module_manager_free(new_lua_manage);
|
||||
lua_module_manager_free(new_lua_mod_mgr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,231 +1,156 @@
|
||||
#ifndef LUA_MODULE_MANAGE_INTERNAL_H
|
||||
#define LUA_MODULE_MANAGE_INTERNAL_H
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include "lua_module_manage.h"
|
||||
|
||||
struct lua_state;
|
||||
struct lua_module_manager;
|
||||
|
||||
enum LUA_MODULE_ERR_CODE
|
||||
{
|
||||
/* 加载配置到状态机中错误码 */
|
||||
SCRIPT_ERR = -400,
|
||||
SCRIPT_LOAD_FILE_ERR, /* 配置拷贝至状态机过程中无法处理Lua文件 */
|
||||
SCRIPT_LOAD_FUNCTION_TYPE_ERR, /* 获取得到的函数名称不是全局函数, 无法加载 */
|
||||
/* 代码块执行过程中错误码 */
|
||||
CHUNK_ERR = -300,
|
||||
CHUNK_EXECUTE_TYPE_NOT_FUNCTION, /* 执行代码块过程中根据ref_id获取得到的变量不是函数 */
|
||||
CHUNK_EXECUTE_RUN_CODE_ERR, /* 执行代码块运行错误 */
|
||||
/* 函数与全局变量操作错误码 */
|
||||
CDATA_ERR = -200,
|
||||
CFUNC_BIND_NAMESPACE_CONFLICT, /* 命名空间数据类型不符 */
|
||||
CFUNC_BIND_NAME_CONFLICT, /* 绑定函数的函数名称与已有数据冲突 */
|
||||
CDATA_BIND_NAMESPACE_CONFLICT, /* 命名空间数据类型不符 */
|
||||
CDATA_BIND_NAME_CONFLICT, /* 全局变量名称与已有数据冲突 */
|
||||
CDATA_BIND_TYPE_ERR, /* 数据类型无法识别或存在异常 */
|
||||
CDATA_SET_VALUE_TYPE_UNSUPPORT, /* 使用了未识别的数据类型 */
|
||||
CDATA_PUSH_STACK_TYPE_UNKNOWN, /* 入栈数据无法识别数据类型 */
|
||||
CDATA_POP_STACK_TYPE_UNSUPPORT, /* 出栈数据类型不支持出栈 */
|
||||
/* 状态机基础操作中错误码 */
|
||||
STATE_ERR = -100,
|
||||
STATE_COPY_REF_VALUE_ERR, /* 在状态机之间拷贝数据过程中出现异常 */
|
||||
/* 通用返回值 */
|
||||
PARAM_ERR = -1, /* 传入参数错误, 可能是指针为空或类型不符合 */
|
||||
SUCCESS = 0, /* 运行成功 */
|
||||
};
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_state *lua_state_new(struct lua_module_manager *lua_mod_mgr);
|
||||
void lua_state_free(struct lua_state *state);
|
||||
struct lua_module_manager *lua_state_get_lua_module_manager(struct lua_state *state);
|
||||
int lua_state_copy_ref_value(struct lua_state *to, struct lua_state *from);
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_module_spec
|
||||
{
|
||||
char *spec_file_path; /* 插件需要使用的文件名 */
|
||||
char *spec_load_func_name; /* 加载插件需要调用的函数名称 */
|
||||
char *spec_unload_func_name; /* 卸载插件需要调用的函数名称 */
|
||||
};
|
||||
struct lua_module_spec *lua_module_config_file_load(const char *config_file_name, size_t *spec_num);
|
||||
void lua_module_spec_free(struct lua_module_spec spec[], size_t spec_num);
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
/* 需要注册至lua中的函数 */
|
||||
typedef int (*lua_cbind_func)(struct lua_state *state);
|
||||
struct lua_bind_func_spec
|
||||
{
|
||||
lua_cbind_func func; /* 注册函数原型 */
|
||||
char *func_name; /* 注册至lua中的函数名称 */
|
||||
char *space_name; /* 注册至lua中的命名空间名称 */
|
||||
};
|
||||
int lua_cbinding_function(struct lua_state *state, struct lua_bind_func_spec bind_function[], size_t bind_func_num);
|
||||
|
||||
enum LUA_DATATYPE
|
||||
{
|
||||
DATATYPE_BEGIN = 0,
|
||||
DATATYPE_NIL, /* nil类型 */
|
||||
DATATYPE_BOOL, /* bool类型 */
|
||||
DATATYPE_INT, /* int类型 */
|
||||
DATATYPE_NUM, /* double类型 */
|
||||
DATATYPE_STRING, /* 字符串类型 */
|
||||
/* 以下类型不能用于全局变量注册 */
|
||||
DATATYPE_BUFF, /* 传递报文payload时使用 */
|
||||
DATATYPE_TABLE, /* table类型 */
|
||||
DATATYPE_POINTER, /* 指针类型 */
|
||||
DATATYPE_CONTEXT, /* context上下文类型 */
|
||||
DATATYPE_LUA_TABLE, /* 此类型用于在lua之间翻译传递数据 */
|
||||
DATATYPE_FUNCTION, /* 函数类型 */
|
||||
DATATYPE_END
|
||||
};
|
||||
|
||||
/* 需要注册至lua状态机中的数据 */
|
||||
struct lua_bind_data_spec
|
||||
{
|
||||
enum LUA_DATATYPE data_type; /* 注册的数据类型 */
|
||||
char *data_value; /* 注册数数据值 */
|
||||
char *data_name; /* 注册的数据名称 */
|
||||
char *space_name; /* 注册至lua中的命名空间名称 */
|
||||
};
|
||||
int lua_cbinding_data(struct lua_state *state, struct lua_bind_data_spec bind_data[], size_t bind_data_num);
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_context;
|
||||
struct lua_context *lua_context_new(struct lua_state *state);
|
||||
struct lua_context *lua_context_new_with_ref_id(struct lua_state *state, int ref_id);
|
||||
void lua_context_free(struct lua_context *context);
|
||||
void lua_context_push_stack(struct lua_context *context);
|
||||
int lua_context_check_if_context(void *context);
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_ctable;
|
||||
struct lua_cdata
|
||||
{
|
||||
enum LUA_DATATYPE type;
|
||||
size_t data_len; /* 只有在类型为buff或function时使用此标识 */
|
||||
union
|
||||
enum LUA_MODULE_ERR_CODE
|
||||
{
|
||||
int bool;
|
||||
int integer;
|
||||
double number;
|
||||
char *string;
|
||||
/* 函数与全局变量操作错误码 */
|
||||
CDATA_ERR = -300,
|
||||
CDATA_SET_VALUE_TYPE_UNSUPPORT, /* 使用了未识别的数据类型 */
|
||||
CDATA_PUSH_STACK_TYPE_UNKNOWN, /* 入栈数据无法识别数据类型 */
|
||||
CDATA_POP_STACK_TYPE_UNSUPPORT, /* 出栈数据类型不支持出栈 */
|
||||
/* 状态机基础操作中错误码 */
|
||||
STATE_ERR = -200,
|
||||
STATE_BIND_FUNC_NAMESPACE_CONFLICT, /* 命名空间数据类型不符 */
|
||||
STATE_BIND_FUNC_NAME_CONFLICT, /* 绑定函数的函数名称与已有数据冲突 */
|
||||
STATE_BIND_DATA_NAMESPACE_CONFLICT, /* 命名空间数据类型不符 */
|
||||
STATE_BIND_DATA_NAME_CONFLICT, /* 全局变量名称与已有数据冲突 */
|
||||
STATE_BIND_DATA_TYPE_ERR, /* 数据类型无法识别或存在异常 */
|
||||
STATE_COPY_REF_VALUE_ERR, /* 在状态机之间拷贝数据过程中数据拷贝出现异常 */
|
||||
STATE_COPY_REF_VALUE_CONFLICT, /* 在状态机之间拷贝数据过程中出现引用ID不匹配的情况 */
|
||||
STATE_CHUNK_REF_ID_NOT_FUNCTION, /* 传入的ref_id获取值之后不是函数类型 */
|
||||
STATE_CHUNK_DATA_PUSH_ERR, /* 参数入栈过程中异常 */
|
||||
STATE_CHUNK_DATA_POP_ERR, /* 返回值出栈过程中异常 */
|
||||
STATE_CHUNK_RUNNING_ERR, /* CHUNK在调用pcall执行过程中运行错误 */
|
||||
/* lua module manager错误码 */
|
||||
LUA_MOD_MGR_ERR = -100,
|
||||
LMM_STATE_THREAD_NUM_ERR,
|
||||
LMM_STATE_CREATE_ERR,
|
||||
LMM_LOAD_CONFIG_FILE_NOT_EXIST,
|
||||
LMM_LOAD_CONFIG_FILE_OPEN_ERR,
|
||||
LMM_LOAD_CONFIG_FILE_TOML_PARSE_ERR,
|
||||
LMM_LOAD_SCRIPT_FILE_OPEN_ERR,
|
||||
LMM_LOAD_SCRIPT_FUNC_TYPE_ERR,
|
||||
|
||||
char *buff;
|
||||
int table;
|
||||
void *pointer;
|
||||
struct lua_context *context;
|
||||
void *function;
|
||||
struct lua_ctable *ctable;
|
||||
LMM_CALL_INIT_FILE_OPEN_ERR,
|
||||
LMM_CALL_INIT_FUNCTION_TYPE_ERR,
|
||||
LMM_CALL_EXIT_FILE_OPEN_ERR,
|
||||
LMM_CALL_EXIT_FUNCTION_TYPE_ERR,
|
||||
/* 通用返回值 */
|
||||
PARAM_ERR = -1, /* 传入参数错误, 可能是指针为空或类型不符合 */
|
||||
SUCCESS = 0, /* 运行成功 */
|
||||
};
|
||||
};
|
||||
struct lua_cdata *lua_cdata_new(void);
|
||||
void lua_cdata_free(struct lua_cdata *cdata);
|
||||
int lua_cdata_data_set(struct lua_cdata *cdata, enum LUA_DATATYPE type, void *value_p, int value_i);
|
||||
int lua_cdata_push_stack(struct lua_state *state, struct lua_cdata *cdata);
|
||||
int lua_cdata_pop_stack(struct lua_state *state, struct lua_cdata *cdata);
|
||||
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
int lua_chunk_execute(struct lua_state *state, int fn_ref_id, struct lua_cdata param[], size_t param_num, struct lua_cdata returnvalue[], size_t r_num, char *errlog, size_t err_len);
|
||||
struct lua_state;
|
||||
struct lua_module_manager;
|
||||
|
||||
struct lua_load_script
|
||||
{
|
||||
int lua_load_fn_ref_id;
|
||||
int lua_unload_fn_ref_id;
|
||||
int lua_script_env_ref_id;
|
||||
};
|
||||
int lua_load_script_load_single_specific(struct lua_load_script *load_script, struct lua_state *state, struct lua_module_spec *module_spec);
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_context;
|
||||
struct lua_context *lua_context_new(struct lua_state *state);
|
||||
struct lua_context *lua_context_new_with_ref_id(struct lua_state *state, int ref_id);
|
||||
void lua_context_free(struct lua_context *context);
|
||||
void lua_context_push_stack(struct lua_context *context);
|
||||
int lua_context_check_if_context(void *context);
|
||||
|
||||
struct lua_fn_arg_pair
|
||||
{
|
||||
struct lua_fn_arg_pair *next;
|
||||
struct lua_module_manager *lua_mod_mgr;
|
||||
int lua_fn_ref_id;
|
||||
int lua_arg_ref_id;
|
||||
};
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
enum LUA_DATATYPE
|
||||
{
|
||||
DATATYPE_BEGIN = 0,
|
||||
DATATYPE_NIL, /* nil类型 */
|
||||
DATATYPE_BOOL, /* bool类型 */
|
||||
DATATYPE_INT, /* int类型 */
|
||||
DATATYPE_NUM, /* double类型 */
|
||||
DATATYPE_STRING, /* 字符串类型 */
|
||||
/* 以下类型不能用于全局变量注册 */
|
||||
DATATYPE_BUFF, /* 传递报文payload时使用 */
|
||||
DATATYPE_TABLE, /* table类型 */
|
||||
DATATYPE_POINTER, /* 指针类型 */
|
||||
DATATYPE_CONTEXT, /* context上下文类型 */
|
||||
DATATYPE_LUA_TABLE, /* 此类型用于在lua之间翻译传递数据 */
|
||||
DATATYPE_FUNCTION, /* 函数类型 */
|
||||
DATATYPE_END
|
||||
};
|
||||
|
||||
/*
|
||||
struct lua_mq_dispatch_arg
|
||||
{
|
||||
struct lua_mq_dispatch_arg *next;
|
||||
struct lua_module_manage *module_manage;
|
||||
int topic_id;
|
||||
int lua_mq_dispatch_fn_ref_id;
|
||||
int lua_mq_dispatch_arg_ref_id;
|
||||
};
|
||||
struct lua_ctable;
|
||||
struct lua_cdata
|
||||
{
|
||||
enum LUA_DATATYPE type;
|
||||
size_t data_len; /* 只有在类型为buff或function时使用此标识 */
|
||||
union
|
||||
{
|
||||
int bool;
|
||||
int integer;
|
||||
double number;
|
||||
char *string;
|
||||
|
||||
struct lua_mq_msg_free_arg
|
||||
{
|
||||
struct lua_mq_msg_free_arg *next;
|
||||
struct lua_module_manage *module_manage;
|
||||
int topic_id;
|
||||
int lua_mq_msg_free_fn_ref_id;
|
||||
int lua_mq_msg_free_arg_ref_id;
|
||||
};
|
||||
char *buff;
|
||||
int table;
|
||||
void *pointer;
|
||||
struct lua_context *context;
|
||||
void *function;
|
||||
struct lua_ctable *ctable;
|
||||
};
|
||||
};
|
||||
struct lua_cdata *lua_cdata_new(void);
|
||||
void lua_cdata_free(struct lua_cdata *cdata);
|
||||
int lua_cdata_data_set(struct lua_cdata *cdata, enum LUA_DATATYPE type, void *value_p, int value_i);
|
||||
int lua_cdata_push_stack(struct lua_state *state, struct lua_cdata *cdata);
|
||||
int lua_cdata_pop_stack(struct lua_state *state, struct lua_cdata *cdata);
|
||||
|
||||
struct lua_mq_on_msg_arg
|
||||
{
|
||||
struct lua_mq_on_msg_arg *next;
|
||||
struct lua_module_manage *module_manage;
|
||||
int topic_id;
|
||||
int lua_mq_on_msg_fn_ref_id;
|
||||
int lua_mq_on_msg_arg_ref_id;
|
||||
};
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
typedef int (*lua_cbind_func)(struct lua_state *state);
|
||||
struct lua_state_cbind_func_spec
|
||||
{
|
||||
lua_cbind_func func; /* 注册函数原型 */
|
||||
char *func_name; /* 注册至lua中的函数名称 */
|
||||
char *space_name; /* 注册至lua中的命名空间名称 */
|
||||
};
|
||||
|
||||
struct lua_on_packet_stage_callback_arg{
|
||||
struct lua_on_packet_stage_callback_arg * next;
|
||||
struct lua_module_manage * module_manage;
|
||||
int lua_on_packet_stage_fn_ref_id;
|
||||
int lua_on_packet_stage_arg_ref_id;
|
||||
};
|
||||
struct lua_state_cbind_data_spec
|
||||
{
|
||||
enum LUA_DATATYPE data_type; /* 注册的数据类型 */
|
||||
char *data_value; /* 注册数数据值 */
|
||||
char *data_name; /* 注册的数据名称 */
|
||||
char *space_name; /* 注册至lua中的命名空间名称 */
|
||||
};
|
||||
|
||||
struct lua_on_session_callback_arg
|
||||
{
|
||||
struct lua_on_session_callback_arg * next;
|
||||
struct lua_module_manage * module_manage;
|
||||
int lua_on_session_fn_ref_id;
|
||||
int lua_on_session_arg_ref_id;
|
||||
};
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_state *lua_state_new(struct lua_module_manager *lua_mod_mgr);
|
||||
void lua_state_free(struct lua_state *state);
|
||||
struct lua_module_manager *lua_state_get_lua_module_manager(struct lua_state *state);
|
||||
void *lua_state_get_lua_State(struct lua_state *state);
|
||||
int lua_state_cbinding_function(struct lua_state *state, struct lua_state_cbind_func_spec *bind_func_array, size_t bind_func_num);
|
||||
int lua_state_cbinding_data(struct lua_state *state, struct lua_state_cbind_data_spec *bind_data_array, size_t bind_data_num);
|
||||
int lua_state_copy_ref_value(struct lua_state *to, struct lua_state *from);
|
||||
int lua_state_execute_chunk(struct lua_state *state, int chunk_ref_id, struct lua_cdata *param_array, size_t param_num, struct lua_cdata *return_array, size_t return_num, char *err_log, size_t err_log_len);
|
||||
|
||||
struct lua_on_tcp_stream_callback_arg
|
||||
{
|
||||
struct lua_on_tcp_stream_callback_arg * next;
|
||||
struct lua_module_manage * module_manage;
|
||||
int lua_on_tcp_stream_fn_ref_id;
|
||||
int lua_on_tcp_stream_arg_ref_id;
|
||||
};
|
||||
*/
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_fn_arg_pair;
|
||||
struct lua_fn_arg_pair *lua_fn_arg_pair_new(struct lua_module_manager *lua_mod_mgr, int fn_ref_id, int arg_ref_id);
|
||||
void lua_fn_arg_pair_free(struct lua_fn_arg_pair *pair);
|
||||
void lua_fn_arg_pair_insert(struct lua_fn_arg_pair *pair);
|
||||
int lua_fn_arg_pair_get_fn_ref_id(struct lua_fn_arg_pair *pair);
|
||||
int lua_fn_arg_pair_get_arg_ref_id(struct lua_fn_arg_pair *pair);
|
||||
struct lua_module_manager *lua_fn_arg_pair_get_lua_mod_mgr(struct lua_fn_arg_pair *pair);
|
||||
|
||||
struct lua_module_manager
|
||||
{
|
||||
struct stellar_module_manager *mod_mgr;
|
||||
|
||||
size_t state_num;
|
||||
struct lua_state **state_array;
|
||||
size_t load_script_num;
|
||||
// struct lua_module_spec *module_spec_array;
|
||||
struct lua_load_script *load_script_array;
|
||||
|
||||
struct lua_fn_arg_pair *mq_dispatch_list;
|
||||
struct lua_fn_arg_pair *mq_msg_free_list;
|
||||
struct lua_fn_arg_pair *mq_on_msg_list;
|
||||
|
||||
struct lua_fn_arg_pair *on_packet_stage_list;
|
||||
struct lua_fn_arg_pair *on_session_tcp_list;
|
||||
struct lua_fn_arg_pair *on_session_udp_list;
|
||||
struct lua_fn_arg_pair *on_session_control_packet_list;
|
||||
struct lua_fn_arg_pair *on_tcp_stream_list;
|
||||
/*
|
||||
struct lua_mq_dispatch_arg *mq_dispatch_list;
|
||||
struct lua_mq_msg_free_arg *mq_msg_free_list;
|
||||
struct lua_mq_on_msg_arg *mq_on_msg_list;
|
||||
|
||||
struct lua_on_packet_stage_callback_arg *on_packet_stage_list;
|
||||
struct lua_on_session_callback_arg *on_session_tcp_list;
|
||||
struct lua_on_session_callback_arg *on_session_udp_list;
|
||||
struct lua_on_session_callback_arg *on_session_control_packet_list;
|
||||
struct lua_on_tcp_stream_callback_arg * on_tcp_stream_list;
|
||||
*/
|
||||
};
|
||||
/* ***** ***** ***** ***** ***** ***** */
|
||||
struct lua_module_manager *lua_module_manager_new(struct stellar_module_manager *mod_mgr);
|
||||
void lua_module_manager_free(struct lua_module_manager *lua_mod_mgr);
|
||||
int lua_module_manager_state_create(struct lua_module_manager *lua_mod_mgr, struct lua_state_cbind_func_spec *bind_func_array, size_t bind_func_num, struct lua_state_cbind_data_spec *bind_data_array, size_t bind_data_num);
|
||||
int lua_module_manager_load_config(struct lua_module_manager *lua_mod_mgr, const char *conf_file_path);
|
||||
int lua_module_manager_call_init(struct lua_module_manager *lua_mod_mgr);
|
||||
int lua_module_manager_duplicate_state(struct lua_module_manager *lua_mod_mgr);
|
||||
void lua_module_manger_pair_list_insert(struct lua_module_manager *lua_mod_mgr, struct lua_fn_arg_pair *pair);
|
||||
int lua_module_manager_call_exit(struct lua_module_manager *lua_mod_mgr);
|
||||
struct lua_state *lua_module_manager_get_current_thread_state(struct lua_module_manager *lua_mod_mgr);
|
||||
struct stellar_module_manager *lua_module_manager_get_stellar_module_manager(struct lua_module_manager *lua_mod_mgr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user