#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; } static const char lua_base64_encode_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; static int lua_base64_encode(const char *indata, int inlen, char *outdata, int *outlen) { if (indata == NULL || inlen <= 0) { return -1; } int i, j; unsigned char num = inlen % 3; if (outdata != NULL) { // 编码,3个字节一组,若数据总长度不是3的倍数,则跳过最后的 num 个字节数据 for (i = 0, j = 0; i < inlen - num; i += 3, j += 4) { outdata[j] = lua_base64_encode_table[(unsigned char)indata[i] >> 2]; outdata[j + 1] = lua_base64_encode_table[(((unsigned char)indata[i] & 0x03) << 4) | ((unsigned char)indata[i + 1] >> 4)]; outdata[j + 2] = lua_base64_encode_table[(((unsigned char)indata[i + 1] & 0x0f) << 2) | ((unsigned char)indata[i + 2] >> 6)]; outdata[j + 3] = lua_base64_encode_table[(unsigned char)indata[i + 2] & 0x3f]; } // 继续处理最后的 num 个字节的数据 if (num == 1) { // 余数为1,需补齐两个字节'=' outdata[j] = lua_base64_encode_table[(unsigned char)indata[inlen - 1] >> 2]; outdata[j + 1] = lua_base64_encode_table[((unsigned char)indata[inlen - 1] & 0x03) << 4]; outdata[j + 2] = '='; outdata[j + 3] = '='; } else if (num == 2) { // 余数为2,需补齐一个字节'=' outdata[j] = lua_base64_encode_table[(unsigned char)indata[inlen - 2] >> 2]; outdata[j + 1] = lua_base64_encode_table[(((unsigned char)indata[inlen - 2] & 0x03) << 4) | ((unsigned char)indata[inlen - 1] >> 4)]; outdata[j + 2] = lua_base64_encode_table[((unsigned char)indata[inlen - 1] & 0x0f) << 2]; outdata[j + 3] = '='; } } if (outlen != NULL) { *outlen = (inlen + (num == 0 ? 0 : 3 - num)) * 4 / 3; } return 0; } int lua_session_get0_current_payload(struct lua_state *state) { lua_State *L = (lua_State *)state; if (lua_gettop(L) != 1) goto err; struct session *sess = (struct session *)lua_topointer(L, -1); lua_settop(L, 0); size_t payload_len = 0; const char *payload = session_get0_current_payload(sess, &payload_len); #if 0 lua_pushlightuserdata(L, (void *)payload); lua_pushinteger(L, (lua_Integer)payload_len); return 2; err: lua_settop(L, 0); lua_pushlightuserdata(L, NULL); lua_pushboolean(L, 0); return 2; #else char *payload_base64 = (char *)calloc(2, payload_len); int payload_base64_len = 0; if (!lua_base64_encode(payload, payload_len, payload_base64, &payload_base64_len)) { lua_pushstring(L, payload_base64); lua_pushinteger(L, (lua_Integer)payload_len); if (payload_base64) free(payload_base64); return 2; } if (payload_base64) free(payload_base64); err: lua_settop(L, 0); lua_pushstring(L, NULL); lua_pushboolean(L, 0); return 2; #endif }