summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorniubinghui <[email protected]>2024-08-28 18:09:26 +0800
committerniubinghui <[email protected]>2024-08-28 18:09:26 +0800
commitac857cc80ac26fa275c88df832157824aaf08531 (patch)
tree8b04cc63fce9b1db5cae83cbfc76f76558bf45b2
parentcfe51e7c9a7fd7c551eb574475798dd1375ce2b7 (diff)
【修改】完善说明文档
-rw-r--r--docs/summaryDesign.md472
-rw-r--r--src/lua_plugin_manage_internal.h9
2 files changed, 122 insertions, 359 deletions
diff --git a/docs/summaryDesign.md b/docs/summaryDesign.md
index c3169f0..0559f08 100644
--- a/docs/summaryDesign.md
+++ b/docs/summaryDesign.md
@@ -2,41 +2,44 @@
- [LuaPluginManage](#luapluginmanage)
- [总述](#总述)
- [整体功能](#整体功能)
- - [状态机](#状态机)
- - [函数](#函数)
- - [数据](#数据)
- - [插件](#插件)
+ - [状态机(state)](#状态机state)
+ - [函数(function)](#函数function)
+ - [数据(data)](#数据data)
+ - [插件(plugin)](#插件plugin)
+ - [模块(model)](#模块model)
+ - [代码块(chunk)](#代码块chunk)
- [数据转换\*](#数据转换)
- [接口](#接口)
- [外部接口](#外部接口)
- [内部接口](#内部接口)
- [C注册函数](#c注册函数)
+ - [代码块](#代码块)
- [Lua注册函数](#lua注册函数)
- [总体设计](#总体设计)
- [初始化](#初始化)
- [退出](#退出)
- [功能模块](#功能模块)
+ - [各功能结构之间关系](#各功能结构之间关系)
- [函数及全局变量注册](#函数及全局变量注册)
- [数据结构](#数据结构)
- - [函数](#函数-1)
+ - [函数](#函数)
- [Lua与C数据转换](#lua与c数据转换)
- [数据结构](#数据结构-1)
- [状态机管理](#状态机管理)
- - [数据结构](#数据结构-2)
- - [函数](#函数-2)
+ - [函数](#函数-1)
- [插件管理](#插件管理)
+ - [数据结构](#数据结构-2)
+ - [模块管理](#模块管理)
- [数据结构](#数据结构-3)
- - [函数](#函数-3)
- - [脚本管理](#脚本管理)
- - [数据结构](#数据结构-4)
- - [函数](#函数-4)
+ - [代码块管理](#代码块管理)
+ - [函数](#函数-2)
- [配置管理](#配置管理)
- - [数据结构](#数据结构-5)
- - [函数](#函数-5)
+ - [数据结构](#数据结构-4)
- [C注册函数](#c注册函数-1)
- [Lua注册函数](#lua注册函数-1)
- [安全性](#安全性)
- [重名的情况](#重名的情况)
+ - [使用限制](#使用限制)
## 总述
### 整体功能
@@ -46,25 +49,25 @@
3. 数据的管理功能:C将数据注册至Lua环境中,如全局使用的字符串、版本信息、运行环境等;
4. 插件的管理功能:将一个Lua插件加载至插件管理中,与C插件一致,将插件注册至stellar-plugin_manage及session_plugin_manage中;
5. 数据转化:可以将Lua中的数据与C中的数据进行安全的数据转换,用于在C中暂存Lua中保存的数据,并可以对该数据进行修改、维护;
-### 状态机
+### 状态机(state)
状态机用于保存Lua运行虚拟机在运行过程中所有的环境表、注册表、堆栈、数据段、上下文等数据。Lua状态机是Lua运行过程中对外暴露的唯一数据结构,其余数据结构均已经在Lua内部隐藏,所有对Lua的操作均需要通过改变状态机的状态来实现。一个Lua状态机可以理解为一个Lua的线程,该状态机和C的线程一样,拥有独立的内存空间。
创建:安全的新建一个空的状态机
删除:释放一个状态机,删除其内部所有数据占用的内存并释放一个状态机实例
初始化:状态机初始化过程中加载运行时必要的Lua组件,并根据配置文件将各个Lua插件加载在状态机中
获取运行错误*:获取状态机运行过程中的错误
获取运行状态*:获取状态机运行状态,包括运行时间、加载插件数量、运行耗时等
-### 函数
+### 函数(function)
向Lua内部注册一个函数,可供Lua程序在运行过程中调用。如向lua中传入数据结构时,可通过注册函数的方式向Lua提供数据的获取方法。或通过注册函数向Lua提供其他模块的外部接口。
注册:向Lua状态机中注册一个格式要求符合lpm_cbinding_function类型的函数,该函数可供Lua脚本调用
删除:从Lua状态机中删除一个C注册的函数
安全检查*:提供一个标准化的定义方式,在Lua插件编写过程中可通过该定义方式得知运行过程中存在的注册函数名称、参数类型等,防止在调用过程中出错;在Lua脚本加载前对其内部调用的C函数进行安全性检查,检查如传入参数个数、参数类型等
自动化函数翻译*:通过特定方法能够自动将一些C函数转换为Lua的注册函数格式
-### 数据
+### 数据(data)
向Lua内部注册一个函数,可供Lua脚本在运行过程中使用。如向Lua中传入运行程序版本、运行环境信息、系统信息等。
注册:向状态机中注册一个全局变量,变量类型可以为数字、字符串;如需注册一个数据结构需同时注册该数据结构的成员获取方法
删除:从状态机中删除一个全局变量
数据范式化*:将一段Lua数据范式化为一段json或其他特定格式,方便输出或在其他程序中调用
-### 插件
+### 插件(plugin)
插件为一整个Lua功能模块,与C中的插件含义相同。
插件加载:将一个插件加载至插件管理器中
插件卸载:从插件管理器中卸载一个插件
@@ -73,6 +76,13 @@
注册会话插件:此功能仅在lua端暴露,不在C接口中。在插件加载过程中可注册会话插件
订阅会话消息:此功能仅在lua端暴露,不在C接口中。在插件加载过程中可订阅一个会话中特定id的会话消息
处理会话消息:处理会话过程中的数据
+### 模块(model)
+模块为加载Lua的一个通用文件,模块与配置项一一对应,将一个配置项加载为一个插件管理器中的模块
+模块加载:向插件管理器中插入一个新的模块
+由于模块没有唯一标识,在模块插入后不支持单独卸载,在插件管理器整体退出过程中会卸载所有插入的模块
+### 代码块(chunk)
+代码块可以是一段Lua代码、一个Lua文件或一个Lua函数。在注册过程中,在状态机中加载一段lua代码,并将其通过引用方式确定其一个引用编号,调用过程中通过引用编号在状态机中调用一段代码。参数及返回值的传递通过数据模块进行。目前支持的数据类型包括数字、字符串、table、userdata。
+代码块执行:执行一段代码,并获取其执行结果
### 数据转换*
将Lua数据翻译为C中可读可写可持久化保存的数据,也可以将一个由Lua转换为C数据得到的序列化数据转换为Lua中的数据结构
Lua转换至C:将Lua数据序列化成为C中一段内存数据
@@ -82,8 +92,9 @@ C转换至Lua:将C中数据反序列化为Lua中一段数据
### 外部接口
| 函数名称 | 参数 | 返回值 | 功能 |
| --- | --- | --- | --- |
-| lua_plugin_manage_init | struct stellar * st<br>const char * config_file_path | 创建的lua插件管理器实例指针 | 根据配置文件创建一个lua插件管理器实例,并在函数内完成配置加载及初始化过程
+| lua_plugin_manage_init | struct stellar * st<br>int specific_count<br>struct lua_config_specific *specifics | 创建的lua插件管理器实例指针 | 根据配置文件创建一个lua插件管理器实例,并在函数内完成配置加载及初始化过程
| lua_plugin_manage_exit | struct lua_plugin_manage_schema * lua_plug_mgr | void | 清理一个lua插件管理器实例,清除内部所有数据
+| lua_plugin_manage_load_one_specific | struct lua_plugin_manage_schema *schema<br>struct lua_config_specific *specific | 0成功,其他失败 | 在lua插件管理器中根据配置注册一个新的插件
### 内部接口
| 函数名称 | 参数 | 返回值 | 功能 |
@@ -102,11 +113,28 @@ C转换至Lua:将C中数据反序列化为Lua中一段数据
| --- | --- | --- | --- |
| lpm_ctx_new_func | struct session *sess<br>void *plugin_env | 在该session中创建的context实例 | session创建时调用
| lpm_ctx_free_func | struct session *sess<br>void *sess_ctx<br>void *plugin_env | void | session销毁时调用
+| lpm_message_free_func | struct session *sess<br>void *msg<br>void *msg_free_arg | void | 在消息释放过程中调用的函数
+| lpm_on_session_msg_func | struct session *sess<br>int topic_id<br>const void *msg<br>void *sess_ctx<br>void *plugin_env | void | 触发消息时调用
+
+### 代码块
+| 函数名称 | 参数 | 返回值 | 功能 |
+| --- | --- | --- | --- |
+| lua_chunk_execute | lua_State *state<br>int ref_id<br>int pcount<br>struct lua_cdata *params<br>int rcount<br>struct lua_cdata *returns | int | 调用并执行一个Lua代码块,该代码段需要提前生成一个引用ID,通过该引用ID进行执行
### Lua注册函数
| 函数名称 | 参数 | 返回值 | 功能 |
| --- | --- | --- | --- |
| lua_plugin_manage_session_regist | lua_State * state | 0无需处理返回值,1需处理返回值 | lua中调用该函数注册插件,注册完成后调用方式为调用plugin_manage.register
+| lua_session_get_type | lua_State * state | 1有返回值 | 获取sessiontype
+| lua_mq_create_topic | lua_State *state | 1有返回值,0处理失败或无返回值 | 创建一个新的topic
+| lua_mq_get_topic_id | lua_State *state | 1有返回值,0处理失败或无返回值 | 根据topic名称获取某个topic的编号
+| lua_mq_update_topic | lua_State *state | 1有返回值,0处理失败或无返回值 | 更新一个topic,更新其释放函数及释放私有数据
+| lua_mq_destory_topic | lua_State *state | 1有返回值,0处理失败或无返回值 | 销毁一个topic
+| lua_mq_subscribe_topic | lua_State *state | 1有返回值,0处理失败或无返回值 | 订阅一个topic,根据plugin编号订阅
+| lua_mq_topic_is_active | lua_State *state | 1有返回值,0处理失败或无返回值 | 判断topic当前的活跃状态
+| lua_mq_publish_message | lua_State *state | 1有返回值,0处理失败或无返回值 | 在topic中发布一条消息
+| lua_mq_ignore_message | lua_State *state | 1有返回值,0处理失败或无返回值 | 在会话中忽略一个消息
+| lua_mq_unignore_message | lua_State *state | 1有返回值,0处理失败或无返回值 | 在会话中取消忽略一个消息
## 总体设计
### 初始化
@@ -117,21 +145,32 @@ title: lua_plugin_manage_init
flowchart TD;
start(["开始"]);
createschema["calloc新的schema"];
- loadconfig["从配置文件中加载配置"];
- configcheck{"检查返回值"};
- threadcount["获取线程数量"];
- loadthread["每个线程中创建新的状态机"];
- freeschema["清理schema"];
+ initmessagemq["初始化消息队列"];
+ getthreadnum["获取线程数量"];
+ createstate["根据线程数量创建对应状态机"];
+ getmodelnum["获取需要加载的模块数量"];
+ createmodel["根据模块数量预分配内存"];
+ finishload{"所有状态机完成初始化"};
+ initstate["初始化状态机"];
+ finishstate{"状态机完成所有模块加载"};
+ loadspecific["状态机加载一个模块"];
+ callload["调用该模块加载函数"];
finish(["结束"]);
start --> createschema
- createschema --> loadconfig
- loadconfig --> configcheck
- configcheck --> |<0|freeschema
- configcheck --> |>=0|threadcount;
- threadcount --> loadthread
- loadthread --> finish
- freeschema --> finish
+ createschema --> initmessagemq
+ initmessagemq --> getthreadnum
+ getthreadnum --> createstate
+ createstate --> getmodelnum
+ getmodelnum --> createmodel
+ createmodel --> finishload
+ finishload --> |N|initstate
+ finishload --> |Y|finish
+ initstate --> finishstate
+ finishstate --> |N|loadspecific
+ finishstate --> |Y|finishload
+ loadspecific --> callload
+ callload --> finishstate
```
1. 创建一个新的schema实例
2. 将配置文件中配置信息加载至schema实例中暂存
@@ -144,18 +183,35 @@ title: lua_plugin_manage_exit
---
flowchart TD;
start(["开始"]);
- freespecific["依次清理加载的配置信息"];
- freethread["依次关闭线程中的状态机"];
- freeschema["清理schema"];
+ freestate["依次释放所有状态机"];
+ freemodel["释放所有模块"];
+ freemessage["释放所有消息队列"];
finish(["结束"]);
- start --> freespecific
- freespecific --> freethread
- freethread --> freeschema
- freeschema --> finish
+ start --> freestate
+ freestate --> freemodel
+ freemodel --> freemessage
+ freemessage --> finish
```
## 功能模块
+### 各功能结构之间关系
+```mermaid
+---
+title: 各结构体之间依赖关系
+---
+erDiagram
+ lua_plugin_manage_schema {
+ stellar st
+ int state_count
+ lua_State[] state
+ int model_count
+ lua_model[] model
+ int mq_count
+ array message_mq_array
+ }
+```
+
### 函数及全局变量注册
1. 如果传入参数中包含space_name,则将函数在Lua中注册为space_name.func_name形式
2. 如果参数中没有space_name,则将函数在Lua中注册为func_name形式
@@ -360,63 +416,14 @@ struct lua_context
### 状态机管理
1. 每个线程中创建一个状态机,将该线程中加载的所有插件全部注册在该状态机中;
-#### 数据结构
-```C
-struct lua_thread_state
-{
- /* 创建状态机的线程ID */
- int thread_id;
- /* 已经插入的插件数量 */
- int thread_plugin_count;
- /* 生成的状态机 */
- lua_State *thread_state;
- // struct lua_pl_state_private * lua_thread_private;
- /* 该线程状态机中注册的插件列表 */
- UT_array *thread_env_array;
- /* 状态机的启动时间 */
- time_t thread_begin_time;
-};
-```
-
#### 函数
| 函数名称 | 参数 | 返回值 | 功能 |
| --- | --- | --- | --- |
-| lua_plugin_manage_state_load | struct lua_plugin_manage_schema *schema<br>int thread_count | >=0状态机创建成功的数量,<0加载失败 | 在schema中根据线程数量创建多个状态机并完成状态机的初始化
-| thread_state_instance_init | struct lua_thread_state *thread_state<br>int state_id | 0表示成功,其他表示失败 | 初始化一个状态机的实例,包括创建lua_State、绑定函数、绑定全局变量等
-| thread_state_instance_destory | void * elt | void | 销毁一个状态机,清空状态机中所有注册的插件并关闭状态机
-| thread_state_instance_load | struct lua_thread_state *thread_state<br>struct lua_plugin_manage_schema *schema | 0表示成功,其他表示失败 | 根据配置数量创建plugin_env,并依次调用插件加载函数完成插件的初始化
+| thread_state_init | int thread_id | 创建成功的状态机指针 | 创建一个状态机,并完成基础信息初始化
```mermaid
---
-title: lua_plugin_manage_state_load
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- initstatearray["初始化schema中状态机队列"];
- checkstatecount{"状态机数量与线程数量一致"};
- createstate["创建新的状态机"];
- insertstate["创建完成的新状态机插入至schema队列中"];
- checkstateinit{"状态机全部完成初始化"};
- initstate["初始化状态机"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|initstatearray
- paramcheck --> |N|finish
- initstatearray --> checkstatecount;
- checkstatecount --> |Y|checkstateinit;
- checkstatecount --> |N|createstate;
- createstate --> insertstate;
- insertstate --> checkstatecount;
- checkstateinit --> |Y|finish
- checkstateinit --> |N|initstate;
- initstate --> checkstateinit;
-```
-
-```mermaid
----
-title: thread_state_instance_init
+title: thread_state_init
---
flowchart TD;
start(["开始"]);
@@ -424,6 +431,7 @@ flowchart TD;
newstate["创建状态机"];
bindfunc["绑定函数"];
binddata["绑定全局变量"];
+ setthread["设置线程ID全局变量"];
finish(["结束"]);
start --> paramcheck
@@ -431,49 +439,8 @@ flowchart TD;
praamcheck --> |N|finish
newstate --> bindfunc
bindfunc --> binddata
- binddata --> finish
-```
-
-```mermaid
----
-title: thread_state_instance_destory
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- unloadplugin["依次卸载所有插件"];
- closestate["关闭状态机"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|unloadplugin
- paramcheck --> |N|finish
- unloadplugin --> closestate
- closestate --> finish
-```
-
-```mermaid
----
-title: thread_state_instance_load
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- plugincount{"完成所有插件初始化"};
- initpluginenv["初始化插件环境变量"];
- loadcount{"完成所有插件加载"};
- loadpluginenv["调用插件加载函数"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|plugincount
- paramcheck --> |N|finish
- plugincount --> |N|initpluginenv
- plugincount --> |Y|loadcount
- initpluginenv --> plugincount
- loadcount --> |Y|finish
- loadcount --> |N|loadpluginenv
- loadpluginenv --> loadcount
+ binddata --> setthread
+ setthread --> finish
```
### 插件管理
@@ -481,216 +448,38 @@ flowchart TD;
#### 数据结构
```C
-/* 每一个插件的函数信息 */
-struct lua_session_plugin
+/* 保存Lua插件信息 */
+struct lua_plugin
{
- /* 插件注册完成后得到的插件编号 */
+ /* 注册完成后得到的插件ID */
int plugin_id;
-
- /* ctx_new函数 */
- struct lua_script plugin_ctx_new_script;
- /* ctx_free函数 */
- struct lua_script plugin_ctx_free_script;
- /* on_message函数 */
- // struct lua_script plugin_on_message_script;
+ /* context_new函数在状态机中的引用值 */
+ int ctx_new_ref;
+ /* context_free函数在状态机中的引用值 */
+ int ctx_free_ref;
+ /* 该插件中订阅的topic */
+ UT_array *sub_topic_array;
};
-
-struct lua_plugin_env
-{
- /* 插件注册的状态机 */
- lua_State *plugin_env_state;
- /* 该环境数据中包含的所有插件列表 */
- UT_array *plugin_env_plugin_array;
-
- /* 加载插件的lua函数 */
- struct lua_script plugin_env_load_func;
- /* 卸载插件的lua函数 */
- struct lua_script plugin_env_unload_func;
-
- /* 该插件运行环境数据的名称, 在创建时同时在状态机中创建该名称的命名空间 */
- /* 插件申请lua内的全局变量可以保存在该命名空间内, 防止被其他内容覆盖 */
- // char *plugin_env_name;
-
- /* 在lua中保存运行数据的引用ID */
- int plugin_env_ref_id; /* plugin_env */
-};
-```
-#### 函数
-| 函数名称 | 参数 | 返回值 | 功能 |
-| --- | --- | --- | --- |
-| session_plugin_instance_init | struct lua_session_plugin *session_plugin<br>lua_State *state<br>int plugin_id<br>int new_refid<br>int free_refid | 0表示成功,其他表示失败 | 初始化一个运行在会话中的插件
-| session_plugin_instance_destory | void *elt | void | 销毁一个运行在会话中的插件
-| plugin_env_instance_init | struct lua_plugin_env *plugin_env<br>lua_State *state<br>struct lua_config_specific *specific | 0表示成功,其他表示失败 | 根据specific配置初始化一个plugin_env,在此过程中仅将函数加载完成,并未进行load函数调用,load函数在状态机加载完成所有插件后统一调用
-| plugin_env_instance_destory | void *elt | void | 销毁一个插件运行环境变量
-
-```mermaid
----
-title: session_plugin_instance_init
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- init["根据引用id分别初始化ctx_new及ctx_free"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|init
- paramcheck --> |N|finish
- init --> finish
```
-```mermaid
----
-title: session_plugin_instance_destory
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- delete["分别删除状态机中的引用"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|delete
- paramcheck --> |N|finish
- delete --> finish
-```
-
-```mermaid
----
-title: plugin_env_instance_init
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- init["根据名称分别初始化ctx_new及ctx_free"];
- createenv["创建该插件pluginenv"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|init
- paramcheck --> |N|finish
- init --> createenv
- createenv --> finish
-```
-
-```mermaid
----
-title: plugin_env_instance_destory
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- unload["调用unload函数"];
- unregist["卸载所有在该env中的插件"];
- deletefunc["删除load及unload函数在状态机中的引用"];
- deleteenv["删除创建的plugin_env"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|unload
- paramcheck --> |N|finish
- unload --> unregist
- unregist --> deletefunc
- deletefunc --> deleteenv
- deleteenv --> finish
-```
-
-### 脚本管理
+### 模块管理
#### 数据结构
-```C
-/* 一个可以运行的lua函数 */
-/* 加载过程中生成引用, 后续使用过程中使用引用编号进行调用 */
-struct lua_script
-{
- /* 该函数注册时注册至的状态机, 运行过程中使用该状态机调用函数 */
- lua_State *script_state;
- /* 该函数的引用ID */
- int script_ref_id;
-
- /* 运行成功次数 */
- int script_run_success;
- /* 运行失败次数 */
- int script_run_failed;
- /* 最后一次运行开始时间 */
- clock_t script_last_ms_start;
- /* 最后一次运行结束时间 */
- clock_t script_last_ms_end;
- /* 运行总用时 */
- clock_t script_total_ms;
-};
-```
+
+### 代码块管理
#### 函数
| 函数名称 | 参数 | 返回值 | 功能 |
| --- | --- | --- | --- |
-| script_instance_init_byname | struct lua_script *script<br>lua_State *state<br>const char *filepath<br>const char *funcname | 0表示成功,其他表示失败 | 根据文件名及函数名称从lua脚本中初始化一个脚本
-| script_instance_init_byrefid | struct lua_script *script<br>lua_State *state<br>int ref_id | 0表示成功,其他表示失败 | 根据一个已经在状态机中生成引用的引用编号初始化一个脚本
-| script_instance_clean | struct lua_script *script | void | 清理一个脚本实例
-| script_execute | struct lua_script *script<br>int pcount<br>struct lua_cdata *param<br>int rmaxcount<br>struct lua_cdata *rvalue | 0表示成功,其他表示失败 | 运行一个脚本
-
-```mermaid
----
-title: script_instance_init_byname
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- fileload["加载lua文件"];
- findfunc["在文件中寻找对应函数"];
- createref["创建引用"];
- initscript["初始化脚本实例"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|fileload
- paramcheck --> |N|finish
- fileload --> findfunc
- findfunc --> createref
- createref --> initscript
- initscript --> finish
-```
-
-```mermaid
----
-title: script_instance_init_byrefid
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- initscript["初始化脚本实例"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|initscript
- paramcheck --> |N|finish
- initscript --> finish
-```
-
-```mermaid
----
-title: script_instance_clean
----
-flowchart TD;
- start(["开始"]);
- paramcheck{"参数检查"};
- delete["状态机中删除该引用"];
- finish(["结束"]);
-
- start --> paramcheck
- paramcheck --> |Y|delete
- paramcheck --> |N|finish
- delete --> finish
-```
+| lua_chunk_execute | lua_State *state<br>int ref_id<br>int pcount<br>struct lua_cdata *params<br>int rcount<br>struct lua_cdata *returns | 0运行成功,其他表示运行失败 | 调用一个lua代码块
```mermaid
---
-title: script_execute
+title: lua_chunk_execute
---
flowchart TD;
start(["开始"]);
paramcheck{"参数检查"};
getref["根据refid加载函数"];
pushparam["传入参数"];
- logtime["记录时间"];
execute["调用函数"];
popreturn["取出参数"];
finish(["结束"]);
@@ -699,8 +488,7 @@ flowchart TD;
paramcheck --> |Y|getref
paramcheck --> |N|finish
getref --> pushparam
- pushparam --> logtime
- logtime --> execute
+ pushparam --> execute
execute --> popreturn
popreturn --> finish
```
@@ -721,37 +509,6 @@ struct lua_config_specific
char *config_specific_unload_func;
};
```
-#### 函数
-| 函数名称 | 参数 | 返回值 | 功能 |
-| --- | --- | --- | --- |
-| specific_instance_copy | void *dst<br>const void *src | void | 拷贝一个配置结构实例,拷贝过程为深拷贝
-| specific_instance_destory | void *elt | void | 销毁一个配置结构实例,释放内部元素占用的内存
-
-```mermaid
----
-title: specific_instance_copy
----
-flowchart TD;
- start(["开始"]);
- copy["依次拷贝内部成员"]
- finish(["结束"]);
-
- start --> copy
- copy --> finish
-```
-
-```mermaid
----
-title: specific_instance_destory
----
-flowchart TD;
- start(["开始"]);
- free["释放内部成员"]
- finish(["结束"]);
-
- start --> free
- free --> finish
-```
### C注册函数
| 函数名称 | 参数 | 返回值 | 功能 |
@@ -856,3 +613,6 @@ flowchart TD;
自定义数据与插件数据冲突
自定义数据优先级高于插件数据,插件数据注册过程中如果与已存在的数据冲突会注册失败
+
+## 使用限制
+1. 在加载的Lua插件中尽量不使用全局变量,否则可能出现由于加载顺序问题导致全局变量被覆盖,使结果不符合预期的情况;可以将需要全局保存的数据注册为状态机中的全局变量,或将需要全局保存的数据写入env变量中解决此问题。
diff --git a/src/lua_plugin_manage_internal.h b/src/lua_plugin_manage_internal.h
index 60e9f5a..ca55306 100644
--- a/src/lua_plugin_manage_internal.h
+++ b/src/lua_plugin_manage_internal.h
@@ -289,6 +289,8 @@ struct lua_plugin
/* 该插件中订阅的topic */
UT_array *sub_topic_array;
};
+void lua_plugin_destory(void *elt);
+struct lua_plugin *search_plugin_by_id(int plugin_id);
#define MODEL_MARK_INIT_DONE 0x0001
#define MODEL_MARK_LOAD_DONE 0x0002
@@ -310,6 +312,8 @@ struct lua_model
/* 加载的插件数量 */
unsigned short plugin_count;
};
+/* 在一个状态机中初始化一个模块 */
+int thread_state_load_specific(lua_State *state, struct lua_model *model, struct lua_config_specific *specific);
/* 由lua创建的topic结构, 该结构保存在schema中 */
struct lua_message_mq
@@ -322,6 +326,8 @@ struct lua_message_mq
int mq_private_ref;
// char * topic_name;
};
+/* 根据topic_id在schema中查找一个message对应的函数 */
+struct lua_message_mq *search_message_mq_by_id(int topic_id);
#define LUA_STATE_THREAD_ID_KEY "__thread_id"
struct lua_plugin_manage_schema
@@ -341,9 +347,6 @@ struct lua_plugin_manage_schema
UT_array *message_mq_array;
};
-struct lua_plugin *search_plugin_by_id(int plugin_id);
-struct lua_message_mq *search_message_mq_by_id(int topic_id);
-
#ifdef LUAPLUGIN_BASIC_UNITTEST
void debug_lua_state_stack(lua_State *state, int mod, const char *message);
void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema *schema);