This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
niubinghui-luapluginmanage/src/lua_plugin_binding.c

530 lines
17 KiB
C
Raw Normal View History

/*************************************************************************
> File Name: lua_plugin_binding.c
> Author:
> Created Time: 2024-08
> Encoding : UTF-8
************************************************************************/
/*************************************************************************
* version
* [ v0.1 ]
* 08 -02
* 1.
* int lua_cbinding_function;
* int lua_cbinding_function_remove;
* int lua_cbinding_data;
* int lua_cbinding_data_remove;
* int lua_cbinding_functions;
* int lua_cbinding_datas;
* int lua_plugin_manage_session_regist;
* ** lua_plugin_manage_session_regist还需要补充
************************************************************************/
#include "lua_plugin_manage_internal.h"
#include <stdlib.h>
#include <string.h>
/* 需要注册至lua中供lua调用的所有函数原型 */
int lua_plugin_manage_session_regist(lua_State * state);
/* 需要注册至状态机中的函数定义在链表中, 会依次完成注册 */
struct lua_binding_function lua_bind_functions[] = {
{lua_plugin_manage_session_regist, "register", "plugin_manage"},
{NULL, NULL, NULL},
};
/* 需要注册至状态机中的数据定义在链表中, 会依次完成注册 */
/* 为了实现注册过程的统一, 所有数据类型均使用string作为value格式 */
struct lua_binding_data lua_bind_datas[] = {
{DATATYPE_BOOL, "true", "test_data_bool", "plugin_manage"},
{DATATYPE_INT, "1000", "test_data_int", "plugin_manage"},
{DATATYPE_NUM, "100.001", "test_data_double", "plugin_manage"},
{DATATYPE_STRING, "this is a string test", "test_data_string", "plugin_manage"},
{DATATYPE_END, NULL, NULL, NULL},
};
/*
* Function: lua_cbinding_function
* Input: | lua_State * | state |
* | lua_CFunction | bind_function |
* | const char * | function_name |
* | const char * | space_name |
* Output:
* Return: | 0 |
* | -1 |
* | -2 | , table类型,
* | -3 | ,
* | -4 | ,
* Description: C函数注册至lua中
*/
int lua_cbinding_function(
lua_State *state,
lua_CFunction bind_function,
const char *function_name,
const char *space_name)
{
if (__glibc_unlikely(!state || !bind_function || !function_name))
return -1;
if (space_name)
{
/* 包含space_name, 调用时为 space_name.function_name */
lua_getglobal(state, space_name);
if (lua_gettop(state) == 0 || lua_type(state, -1) == LUA_TNIL)
{
/* 没有该命名空间, 创建一个 */
lua_newtable(state);
lua_setglobal(state, space_name);
lua_settop(state, 0);
lua_getglobal(state, space_name);
}
else
{
/* 该全局已经存在 */
if (lua_type(state, -1) != LUA_TTABLE)
{
/* spacename已经被占用, 并且不是table, 无法插入数据 */
lua_settop(state, 0);
return -2;
}
lua_getfield(state, -1, function_name);
if (lua_type(state, -1) != LUA_TNIL)
{
/* 在待插入的global table中存在与function_name重名的成员 */
lua_settop(state, 0);
return -3;
}
lua_pop(state, 1); /* 正确的情况下此时栈顶为nil, 弹出一个nil值 */
}
lua_pushcfunction(state, bind_function);
lua_setfield(state, -2, function_name);
}
else
{
lua_getglobal(state, function_name);
if (lua_type(state, -1) != LUA_TNIL)
{
/* 存在与function_name重名的全局成员 */
lua_settop(state, 0);
return -4;
}
lua_pop(state, 1);
/* 不包含space_name, 调用时直接使用function_name调用 */
lua_pushcfunction(state, bind_function);
lua_setglobal(state, function_name);
}
lua_settop(state, 0); /* 操作完成, 弹出所有元素 */
return 0;
}
/*
* Function: lua_cbinding_function_remove
* Input: | lua_State * | state |
* | const char * | function_name |
* | const char * | space_name |
* Output:
* Return: | 0 |
* | -1 |
* | -2 | table
* | -3 | ,
* | -4 |
* Description: C注册的函数, lua状态的引用置为nil
*/
int lua_cbinding_function_remove(
lua_State *state,
const char *function_name,
const char *space_name)
{
if (__glibc_unlikely(!state || !function_name))
return -1;
if (space_name)
{
/* 检查该命名空间是否存在 */
lua_getglobal(state, space_name);
if (lua_type(state, -1) != LUA_TTABLE)
{
/* 命名空间存在且不是table */
lua_settop(state, 0);
return -2;
}
/* 检查该命名空间内是否存在function_name */
lua_getfield(state, -1, function_name);
if (lua_type(state, -1) != LUA_TFUNCTION)
{
/* 命名空间存在内部元素名称为function_name, 但是类型不符 */
lua_settop(state, 0);
return -3;
}
/* 删除该函数 */
lua_pop(state, 1);
lua_pushnil(state);
lua_setfield(state, -2, function_name);
}
else
{
/* 获取全局数据, 检查function_name */
lua_getglobal(state, function_name);
if (lua_type(state, -1) != LUA_TFUNCTION)
{
/* 类型不符 */
lua_settop(state, 0);
return -4;
}
/* 删除该函数 */
lua_pop(state, 1);
lua_pushnil(state);
lua_setglobal(state, function_name);
}
lua_settop(state, 0); /* 操作完成, 弹出所有元素 */
return 0;
}
/*
* Function: lua_cbinding_data
* Input: | lua_State * | state |
* | struct lua_binding_data * | data |
* Output:
* Return: | 0 |
* | -1 |
* | -2 | , table类型,
* | -3 | ,
* | -4 | data的数据类型
* | -5 | ,
* Description: lua状态机中
*/
int lua_cbinding_data(
lua_State *state,
struct lua_binding_data *data)
{
if (__glibc_unlikely(!state || !data))
{
return -1;
}
enum DATATYPE data_type = data->binding_data_type;
char *value = data->binding_data_value;
char *data_name = data->binding_data_name;
char *space_name = data->binding_data_space_name;
if (__glibc_unlikely(!value || !data_name))
return -1;
if (space_name)
{
/* 包含space_name */
lua_getglobal(state, space_name);
if (lua_gettop(state) == 0 || lua_type(state, -1) == LUA_TNIL)
{
/* 没有该命名空间, 创建一个 */
lua_newtable(state);
lua_setglobal(state, space_name);
lua_settop(state, 0);
lua_getglobal(state, space_name);
}
else
{
/* 该全局已经存在 */
if (lua_type(state, -1) != LUA_TTABLE)
{
/* spacename已经被占用, 并且不是table, 无法插入数据 */
lua_settop(state, 0);
return -2;
}
lua_getfield(state, -1, data_name);
if (lua_type(state, -1) != LUA_TNIL)
{
/* 在待插入的global table中存在与function_name重名的成员 */
lua_settop(state, 0);
return -3;
}
lua_pop(state, 1); /* 正确的情况下此时栈顶为nil, 弹出一个nil值 */
}
/* 不同类型需要使用不同的入栈函数 */
switch (data_type)
{
case DATATYPE_BOOL:
if (strstr(value, "true") || strstr(value, "TRUE"))
lua_pushboolean(state, 1);
else if (strstr(value, "false") || strstr(value, "false"))
lua_pushboolean(state, 0);
break;
case DATATYPE_INT:
lua_pushinteger(state, atoi(value));
break;
case DATATYPE_NUM:
lua_pushnumber(state, (lua_Number)atof(value));
break;
case DATATYPE_STRING:
lua_pushstring(state, (const char *)value);
break;
default:
/* 未识别的类型 */
lua_settop(state, 0);
return -4;
}
lua_setfield(state, -2, data_name);
}
else
{
lua_getglobal(state, data_name);
if (lua_type(state, -1) != LUA_TNIL)
{
lua_settop(state, 0);
/* 存在与data_name重名的全局成员 */
return -5;
}
lua_pop(state, 1);
/* 不同类型需要使用不同的入栈函数 */
switch (data_type)
{
case DATATYPE_BOOL:
if (strstr(value, "true") || strstr(value, "TRUE"))
lua_pushboolean(state, 1);
else if (strstr(value, "false") || strstr(value, "false"))
lua_pushboolean(state, 0);
break;
case DATATYPE_INT:
lua_pushinteger(state, atoi(value));
break;
case DATATYPE_NUM:
lua_pushnumber(state, (lua_Number)atof(value));
break;
case DATATYPE_STRING:
lua_pushstring(state, (const char *)value);
break;
default:
/* 未识别的类型 */
lua_settop(state, 0);
return -4;
}
lua_setglobal(state, data_name);
}
lua_settop(state, 0); /* 操作完成, 弹出所有元素 */
return 0;
}
/*
* Function: lua_cbinding_data_remove
* Input: | lua_State * | state |
* | const char * | data_name |
* | const char * | space_name |
* Output:
* Return: | 0 |
* | -1 |
* | -2 | table
* | -3 | ,
* | -4 |
* Description: C注册的数据, lua状态的引用置为nil
*/
int lua_cbinding_data_remove(
lua_State *state,
const char *data_name,
const char *space_name)
{
if (__glibc_unlikely(!state || !data_name))
return -1;
if (space_name)
{
/* 检查该命名空间是否存在 */
lua_getglobal(state, space_name);
if (lua_type(state, -1) != LUA_TTABLE)
{
/* 命名空间存在且不是table */
lua_settop(state, 0);
return -2;
}
/* 检查该命名空间内是否存在data_name */
lua_getfield(state, -1, data_name);
int data_type = lua_type(state, -1);
if (data_type != LUA_TSTRING && data_type != LUA_TNUMBER && data_type != LUA_TBOOLEAN)
{
/* 命名空间存在内部元素名称为data_name, 但是类型不符 */
lua_settop(state, 0);
return -3;
}
else if (data_type == LUA_TNIL)
{
/* 没有该名称的元素 */
lua_settop(state, 0);
return 0;
}
/* 删除该函数 */
lua_pop(state, 1);
lua_pushnil(state);
lua_setfield(state, -2, data_name);
}
else
{
/* 获取全局数据, 检查function_name */
lua_getglobal(state, data_name);
int data_type = lua_type(state, -1);
if (data_type != LUA_TSTRING && data_type != LUA_TNUMBER && data_type != LUA_TBOOLEAN)
{
/* 类型不符 */
lua_settop(state, 0);
return -4;
}
else if (data_type == LUA_TNIL)
{
/* 没有该名称的元素 */
lua_settop(state, 0);
return 0;
}
/* 删除该函数 */
lua_pop(state, 1);
lua_pushnil(state);
lua_setglobal(state, data_name);
}
lua_settop(state, 0); /* 操作完成, 弹出所有元素 */
return 0;
}
/*
* Function: lua_cbinding_functions
* Input: | lua_State * | state |
* Output:
* Return: | -1 |
* | 0 |
* | >0 |
* Description: lua中调用的C函数
*/
int lua_cbinding_functions(lua_State *state)
{
if (__glibc_unlikely(!state))
return -1;
int bind_function_count = sizeof(lua_bind_functions) / sizeof(struct lua_binding_function);
int bind_ret = 0;
int failed_count = 0;
for (int i = 0; i < bind_function_count; ++i)
{
if (lua_bind_functions[i].binding_function && lua_bind_functions[i].binding_function_name)
{
bind_ret = lua_cbinding_function(state,
lua_bind_functions[i].binding_function,
(const char *)lua_bind_functions[i].binding_function_name,
(const char *)lua_bind_functions[i].binding_function_space_name);
if (bind_ret)
{
LOGERROR("binding function failed, ret is %d, function name is %s\n", bind_ret, lua_bind_functions[i].binding_function_name);
failed_count += 1;
}
}
}
return failed_count;
}
/*
* Function: lua_cbinding_datas
* Input: | lua_State * | state |
* Output:
* Return: | -1 |
* | 0 |
* | >0 |
* Description: data数据
*/
int lua_cbinding_datas(lua_State *state)
{
if (__glibc_unlikely(!state))
return -1;
int bind_data_count = sizeof(lua_bind_datas) / sizeof(struct lua_binding_data);
int bind_ret = 0;
int failed_count = 0;
for (int i = 0; i < bind_data_count; ++i)
{
if (lua_bind_datas[i].binding_data_value && lua_bind_datas[i].binding_data_name)
{
bind_ret = lua_cbinding_data(state, &lua_bind_datas[i]);
if (bind_ret)
{
LOGERROR("binding data failed, ret is %d, data is [%s]%s", bind_ret, lua_bind_datas[i].binding_data_name, lua_bind_datas[i].binding_data_value);
failed_count += 1;
}
}
}
return failed_count;
}
/*
* Function:
* Input:
* Output:
* Return:
* Description:
*/
int lua_plugin_manage_session_regist(lua_State *state)
{
/* 参数个数检查 */
if (lua_gettop(state) != 4)
{
lua_settop(state, 0);
return 0;
}
/* 参数类型检查 */
if (lua_type(state, -1) != LUA_TLIGHTUSERDATA || lua_type(state, -2) != LUA_TFUNCTION ||
lua_type(state, -3) != LUA_TFUNCTION || lua_type(state, -4) != LUA_TLIGHTUSERDATA)
{
lua_settop(state, 0);
return 0;
}
/* 取出处理第四个参数 */
struct lua_plugin_env *plugin_env = (struct lua_plugin_env *)lua_topointer(state, -1);
if (!plugin_env)
{
lua_settop(state, 0);
return 0;
}
lua_pop(state, 1);
/* 取出处理第三个参数 */
int ctx_free_id = luaL_ref(state, LUA_REGISTRYINDEX);
if (ctx_free_id == LUA_REFNIL)
{
lua_settop(state, 0);
return 0;
}
lua_pop(state, 1);
/* 取出处理第二个参数 */
int ctx_new_id = luaL_ref(state, LUA_REGISTRYINDEX);
if (ctx_new_id == LUA_REFNIL)
{
lua_settop(state, 0);
return 0;
}
lua_pop(state, 1);
#if 0
/* 取出处理第一个参数 */
struct stellar * st = (struct stellar *)lua_topointer(state, -1);
if ( !st ) {
lua_settop(state, 0);
return 0;
}
lua_pop(state, 1);
/* 在stellar中注册, 获取注册id */
int plugin_id = stellar_session_plugin_register(st, lpm_ctx_new_func, lpm_ctx_free_func, (void *)plugin_env);
/* 将注册完成的新插件插入到队列中 */
struct lua_session_plugin session_plugin;
memset(&session_plugin, 0, sizeof(session_plugin));
session_plugin_instance_init(&session_plugin, state, plugin_id, ctx_new_id, ctx_free_id);
utarray_push_back(plugin_env->lua_plugin_env_plugin_array, &session_plugin);
lua_pushinteger(state, plugin_id);
#endif
return 1;
}