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_cfunc.c

127 lines
4.0 KiB
C

/*************************************************************************
> File Name: lua_plugin_cfunc.c
> Author:
> Created Time: 2024-08
> Encoding : UTF-8
************************************************************************/
/*************************************************************************
* version
* [ v0.1 ]
* 08-06
* 1. 实现函数
* void *lpm_ctx_new_func;
* void lpm_ctx_free_func;
************************************************************************/
#include "lua_plugin_manage_internal.h"
/*
* Function: lpm_ctx_new_func
* Input: | struct session * | sess | 会话信息
* | void * | plugin_env | 插件运行环境
* Output:
* Return: | NULL | 运行函数错误
* | pointer | 运行插件上的ctx_new函数成功, 返回context
* Description: 与C插件管理器保持一致的ctx_new_func
*/
void *lpm_ctx_new_func(
struct session *sess,
void *plugin_env)
{
if (__glibc_unlikely(!sess || !plugin_env))
return NULL;
struct lua_model *env = (struct lua_model *)plugin_env;
/* 获取插件ID并找到该插件 */
int plugin_id = session_get_pluginid(sess);
// int plugin_id = 1;
struct lua_plugin *plugin = NULL;
while ((plugin = utarray_next(env->plugin_array, plugin)))
{
if (plugin->plugin_id == plugin_id)
break;
}
// LOGDEBUG("%d call plugin id %d, ref %d\n", plugin_id, plugin->plugin_id, plugin->ctx_new_ref);
if (!plugin || plugin->plugin_id != plugin_id)
/* 未找到该插件 */
return NULL;
/* 获取当前的线程id并找到该线程对应的state */
int thread_id = session_get_threadid(sess);
if ( thread_id > global_schema->state_count )
return NULL;
lua_State * state = global_schema->thread_state[thread_id - 1];
struct lua_context *new_context = lua_context_new(state);
if (__glibc_unlikely(!new_context))
/* 创建新的context失败 */
return NULL;
struct lua_cdata param[3] = {0};
param[0].cdata_type = DATATYPE_POINTER;
param[0].cdata_pointer = sess;
param[1].cdata_type = DATATYPE_TABLE;
param[1].cdata_table = env->private_env_ref;
param[2].cdata_type = DATATYPE_CONTEXT;
param[2].cdata_context = new_context;
if (lua_chunk_execute(state, plugin->ctx_new_ref, 3, param, 0, NULL))
{
/* 脚本执行失败 */
free(new_context);
return NULL;
}
return (void *)new_context;
}
/*
* Function: lpm_ctx_free_func
* Input: | struct session * | sess | 会话信息
* | void * | sess_ctx | 会话中的私有数据
* | void * | plugin_env | 插件运行环境
* Output:
* Return:
* Description: 与C插件管理器保持一致的ctx_free_func
*/
void lpm_ctx_free_func(
struct session *sess,
void *sess_ctx,
void *plugin_env)
{
if (__glibc_unlikely(!sess || !sess_ctx || !plugin_env))
return;
struct lua_context *context = (struct lua_context *)sess_ctx;
struct lua_model *env = (struct lua_model *)plugin_env;
/* 获取插件ID并找到该插件 */
int plugin_id = session_get_pluginid(sess);
// int plugin_id = 1;
struct lua_plugin *plugin = NULL;
while ((plugin = utarray_next(env->plugin_array, plugin)))
{
if (plugin->plugin_id == plugin_id)
break;
}
if (!plugin || plugin->plugin_id != plugin_id)
/* 未找到该插件 */
return;
int thread_id = session_get_threadid(sess);
if ( thread_id > global_schema->state_count )
return;
lua_State * state = global_schema->thread_state[thread_id];
struct lua_cdata param[3] = {0};
param[0].cdata_type = DATATYPE_POINTER;
param[0].cdata_pointer = sess;
param[1].cdata_type = DATATYPE_CONTEXT;
param[1].cdata_context = context;
param[2].cdata_type = DATATYPE_TABLE;
param[2].cdata_table = env->private_env_ref;
lua_chunk_execute(state, plugin->ctx_free_ref, 3, param, 0, NULL);
lua_context_free(state, context);
return;
}