#include #include #include #include #include #include "lua_binding_function.h" /* ***** ***** ***** ***** ***** ***** */ int lua_get_worker_thread_num(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 1) { lua_settop(L, 0); return 0; } struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); int thread_num = stellar_get_worker_thread_num(st); lua_pushinteger(L, thread_num); return 1; } int lua_get_current_thread_id(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 1) { lua_settop(L, 0); return 0; } struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); int thread_id = stellar_get_current_thread_id(st); lua_pushinteger(L, thread_id); return 1; } int lua_get_stellar_pointer(struct lua_state *state) { lua_State *L = (lua_State *)state; lua_settop(L, 0); struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); struct stellar *st = plugin_manage->st; lua_pushlightuserdata(L, (void *)st); return 1; } /* int lua_get_plugin_manage_pointer(struct lua_state *state) { lua_settop((lua_State *)state, 0); struct lua_plugin_manage * plugin_manage = lua_state_get_plugin_manage(state); lua_pushlightuserdata((lua_State *)state, (void *)plugin_manage); return 1; } */ /* ***** ***** ***** ***** ***** ***** */ int lua_session_plugin_regist(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA) { lua_settop(L, 0); return 0; } /* stack -1 */ int plugin_env_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (plugin_env_ref_id == LUA_REFNIL) plugin_env_ref_id = 0; /* stack -2 */ int ctx_free_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) ctx_free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); /* stack -3 */ int ctx_new_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) ctx_new_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); /* stack -4 */ struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); struct lua_session_plugin_env *new_plugin_env = (struct lua_session_plugin_env *)calloc(1, sizeof(struct lua_session_plugin_env)); memset(new_plugin_env, 0, sizeof(struct lua_session_plugin_env)); new_plugin_env->lua_ctx_new_fn_ref_id = ctx_new_fn_ref_id; new_plugin_env->lua_ctx_free_fn_ref_id = ctx_free_fn_ref_id; new_plugin_env->lua_plug_env_ref_id = plugin_env_ref_id; int plugin_id = stellar_session_plugin_register(st, lpm_ctx_new_func, lpm_ctx_free_func, (void *)new_plugin_env); 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); return 1; } int lua_packet_plugin_regist(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA) { lua_settop(L, 0); return 0; } /* stack -1 */ int plugin_env_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (plugin_env_ref_id == LUA_REFNIL) plugin_env_ref_id = 0; /* stack -2 */ int on_packet_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) on_packet_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); /* stack -3 */ int ip_protocol = lua_tointeger(L, -1); lua_pop(L, 1); /* stack -4 */ struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); struct lua_packet_plugin_env *new_plugin_env = (struct lua_packet_plugin_env *)calloc(1, sizeof(struct lua_packet_plugin_env)); memset(new_plugin_env, 0, sizeof(struct lua_packet_plugin_env)); new_plugin_env->lua_plug_env_ref_id = plugin_env_ref_id; new_plugin_env->lua_on_packet_fn_ref_id = on_packet_fn_ref_id; int plugin_id = stellar_packet_plugin_register(st, (unsigned char)ip_protocol, lpm_on_packet_func, (void *)new_plugin_env); 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); return 1; } /* ***** ***** ***** ***** ***** ***** */ int lua_session_get_type(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 1 || lua_type(L, -1) != LUA_TLIGHTUSERDATA) { lua_settop(L, 0); return 0; } struct session *sess = (struct session *)lua_topointer(L, -1); lua_settop(L, 0); if (!sess) return 0; lua_pushinteger(L, (int)session_get_type(sess)); return 1; } /* ***** ***** ***** ***** ***** ***** */ #define MQ_TYPE_PACKET 1 #define MQ_TYPE_SESSION 2 static int lua_plugin_manage_msg_mq_create_topic(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4 || lua_type(L, -3) != LUA_TSTRING || lua_type(L, -4) != LUA_TLIGHTUSERDATA) { lua_settop(L, 0); return 0; } /* stack -1 */ int free_arg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (free_arg_ref_id == LUA_REFNIL) free_arg_ref_id = 0; /* stack -2 */ int free_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); /* stack -3 */ char *topic_name = strdup((char *)lua_tostring(L, -1)); lua_pop(L, 1); /* stack -4 */ struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); struct lua_message_free_arg *new_message_free_arg = (struct lua_message_free_arg *)calloc(1, sizeof(struct lua_message_free_arg)); memset(new_message_free_arg, 0, sizeof(struct lua_message_free_arg)); new_message_free_arg->lua_msg_free_fn_ref_id = free_fn_ref_id; new_message_free_arg->lua_msg_free_arg_ref_id = free_arg_ref_id; int topic_id = -1; if (type == MQ_TYPE_PACKET) 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) 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); LL_APPEND(plugin_manage->message_free_arg_list, new_message_free_arg); new_message_free_arg->topic_id = topic_id; new_message_free_arg->plugin_manage = plugin_manage; lua_pop(L, 1); } else { if (new_message_free_arg) free(new_message_free_arg); return 0; } lua_pushinteger(L, topic_id); return 1; } static int lua_plugin_manage_msg_mq_get_topic_id(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TSTRING || lua_type(L, -2) != LUA_TLIGHTUSERDATA) { lua_settop(L, 0); return 0; } char *topic_name = strdup(lua_tostring(L, -1)); lua_pop(L, 1); struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); int topic_id = -1; if (type == MQ_TYPE_PACKET) topic_id = stellar_packet_mq_get_topic_id(st, (const char *)topic_name); else if (type == MQ_TYPE_SESSION) topic_id = stellar_session_mq_get_topic_id(st, (const char *)topic_name); if (topic_name) free(topic_name); lua_pushinteger(L, topic_id); return 1; } static int lua_plugin_manage_msg_mq_update_topic(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4 || lua_type(L, -3) != LUA_TNUMBER || lua_type(L, -4) != LUA_TLIGHTUSERDATA) goto err; /* stack -1 */ int free_arg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (free_arg_ref_id == LUA_REFNIL) free_arg_ref_id = 0; /* stack -2 */ int free_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); /* stack -3 */ int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); /* stack -4 */ struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); struct lua_message_free_arg *new_message_free_arg = (struct lua_message_free_arg *)calloc(1, sizeof(struct lua_message_free_arg)); memset(new_message_free_arg, 0, sizeof(struct lua_message_free_arg)); new_message_free_arg->topic_id = topic_id; new_message_free_arg->lua_msg_free_fn_ref_id = free_fn_ref_id; new_message_free_arg->lua_msg_free_arg_ref_id = free_arg_ref_id; int update_result = -1; if (type == MQ_TYPE_PACKET) update_result = stellar_packet_mq_update_topic(st, topic_id, lpm_packet_message_free_func, new_message_free_arg); else if (type == MQ_TYPE_SESSION) update_result = stellar_session_mq_update_topic(st, topic_id, lpm_session_message_free_func, new_message_free_arg); if (update_result) { if (new_message_free_arg) free(new_message_free_arg); goto err; } struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); new_message_free_arg->plugin_manage = plugin_manage; LL_APPEND(plugin_manage->message_free_arg_list, new_message_free_arg); lua_settop(L, 0); lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } static int lua_plugin_manage_msg_mq_destory_topic(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 2) goto err; int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); int destory_result = -1; if (type == MQ_TYPE_PACKET) destory_result = stellar_packet_mq_destroy_topic(st, topic_id); else if (type == MQ_TYPE_SESSION) destory_result = stellar_session_mq_destroy_topic(st, topic_id); if (destory_result < 0) goto err; lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } static int lua_plugin_manage_msg_mq_subscribe_topic(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA) goto err; int plugin_id = lua_tointeger(L, -1); lua_pop(L, 1); int on_message_fn_ref_id = 0; if (lua_type(L, -1) == LUA_TFUNCTION) on_message_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); else lua_pop(L, 1); int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); struct stellar *st = (struct stellar *)lua_topointer(L, -1); lua_settop(L, 0); int subscribe_result = -1; if (type == MQ_TYPE_PACKET) subscribe_result = stellar_packet_mq_subscribe(st, topic_id, lpm_on_packet_msg_func, plugin_id); else if (type == MQ_TYPE_SESSION) subscribe_result = stellar_session_mq_subscribe(st, topic_id, lpm_on_session_msg_func, plugin_id); if (subscribe_result) goto err; struct lua_on_message_fn *on_message = NULL; struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state); if (type == MQ_TYPE_PACKET) on_message = hash_find_on_msg_fn(plugin_manage->on_packet_message_hashlist, topic_id, plugin_id); else if (type == MQ_TYPE_SESSION) on_message = hash_find_on_msg_fn(plugin_manage->on_session_message_hashlist, topic_id, plugin_id); if (on_message) { on_message->lua_on_msg_fn_ref_id = on_message_fn_ref_id; } else { if (type == MQ_TYPE_PACKET) on_message = hash_on_msg_fn_insert(plugin_manage->on_packet_message_hashlist, topic_id, plugin_id); else if (type == MQ_TYPE_SESSION) on_message = hash_on_msg_fn_insert(plugin_manage->on_session_message_hashlist, topic_id, plugin_id); if (!on_message) goto err; else on_message->lua_on_msg_fn_ref_id = on_message_fn_ref_id; } lua_settop(L, 0); lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } static int lua_plugin_manage_msg_mq_publish_message(struct lua_state *state, int type) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 3) goto err; int msg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX); if (msg_ref_id == LUA_REFNIL) msg_ref_id = 0; int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); void *p = (void *)lua_topointer(L, -1); lua_settop(L, 0); struct lua_context *new_context = lua_context_new(state); new_context->lua_context_ref_id = msg_ref_id; int publish_result = -1; if (type == MQ_TYPE_PACKET) publish_result = packet_mq_publish_message((struct packet *)p, topic_id, new_context); else if (type == MQ_TYPE_SESSION) publish_result = session_mq_publish_message((struct session *)p, topic_id, new_context); if (publish_result) { luaL_unref(L, LUA_REGISTRYINDEX, new_context->lua_context_ref_id); free(new_context); goto err; } lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } /* ***** ***** ***** ***** ***** ***** */ int lua_packet_mq_create_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_create_topic(state, MQ_TYPE_PACKET); } int lua_packet_mq_get_topic_id(struct lua_state *state) { return lua_plugin_manage_msg_mq_get_topic_id(state, MQ_TYPE_PACKET); } int lua_packet_mq_update_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_update_topic(state, MQ_TYPE_PACKET); } int lua_packet_mq_destory_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_destory_topic(state, MQ_TYPE_PACKET); } int lua_packet_mq_subscribe_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_subscribe_topic(state, MQ_TYPE_PACKET); } int lua_packet_mq_publish_message(struct lua_state *state) { return lua_plugin_manage_msg_mq_publish_message(state, MQ_TYPE_PACKET); } /* ***** ***** ***** ***** ***** ***** */ int lua_session_mq_create_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_create_topic(state, MQ_TYPE_SESSION); } int lua_session_mq_get_topic_id(struct lua_state *state) { return lua_plugin_manage_msg_mq_get_topic_id(state, MQ_TYPE_SESSION); } int lua_session_mq_update_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_update_topic(state, MQ_TYPE_SESSION); } int lua_session_mq_destory_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_destory_topic(state, MQ_TYPE_SESSION); } int lua_session_mq_subscribe_topic(struct lua_state *state) { return lua_plugin_manage_msg_mq_subscribe_topic(state, MQ_TYPE_SESSION); } int lua_session_mq_publish_message(struct lua_state *state) { return lua_plugin_manage_msg_mq_publish_message(state, MQ_TYPE_SESSION); } int lua_session_mq_ignore_message(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 3) goto err; int plugin_id = lua_tointeger(L, -1); lua_pop(L, 1); int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); struct session *sess = (struct session *)lua_topointer(L, -1); lua_settop(L, 0); if (session_mq_ignore_message(sess, topic_id, plugin_id)) goto err; lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } int lua_session_mq_unignore_message(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 4) goto err; int plugin_id = lua_tointeger(L, -1); lua_pop(L, 1); int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); struct session *sess = (struct session *)lua_topointer(L, -1); lua_settop(L, 0); if (session_mq_unignore_message(sess, topic_id, plugin_id)) goto err; lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; } int lua_session_mq_topic_is_active(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 2) goto err; int topic_id = lua_tointeger(L, -1); lua_pop(L, 1); struct session *sess = (struct session *)lua_topointer(L, -1); lua_settop(L, 0); /* 1 means active */ if (session_mq_topic_is_active(sess, topic_id) != 1) goto err; lua_pushboolean(L, 1); return 1; err: lua_settop(L, 0); lua_pushboolean(L, 0); return 1; }