summaryrefslogtreecommitdiff
path: root/src/lua_plugin_manage_internal.h
blob: b977a044b03738188c340295f11ec6496932491b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef LUA_PLUGIN_MANAGE_INTERNAL_H
#define LUA_PLUGIN_MANAGE_INTERNAL_H

#include <stddef.h>
#include "lua_plugin_manage.h"

struct lua_state;

enum LUA_PLUGIN_MANGE_ERR_NUM
{
    /* 状态机相关的错误码 */
    STATE_LOAD_FILE_ERR = -400, /* 状态机加载lua文件时出错, 可能原因是文件不存在或文件无法加载 */
    STATE_REF_TYPE_ERR,         /* 获取函数引用ID过程中数据类型有错, 无法正常执行 */
    STATE_CREATE_REF_FAIL,      /* 创建引用ID过程中失败, 无法创建refid */
    STATE_CALL_LOAD_FAILED,     /* 调用on_load函数过程中运行失败 */
    STATE_CALL_UNLOAD_FAILED,   /* 调用on_unload函数过程中运行失败 */

    STATE_CREATE_ENV_FAIL, /* 在状态机中创建新的plugin_env时出现错误, 无法创建或该模块之前加载过程中的引用ID与新创建ID不同 */
    STATE_GET_UNLOAD_FAIL, /* 状态机加载unload函数时出现错误, 该函数不存在或引用ID有问题 */
    /* lua代码块运行错误码 */
    CHUNK_TYPE_NOT_FUNCTION = -300, /* 获取得到的代码块类型并非可执行的lua语句, 无法执行 */
    CHUNK_RUN_CODE_FAIL,            /* 调用代码过程中运行失败, 失败的具体报错信息会保存在栈中, 通过日志输出 */

    CHUNK_RCOUNT_ERR, /* 尝试接收返回值, 但传入接收返回值的队列长度小于实际返回值个数 */
    /* lua与C之间数据转换或操作错误码 */
    DATA_TYPE_UNKNOWN = -200, /* 无法识别的struct lua_cdata数据类型, 或在该函数中操作了不支持的数据类型 */
    DATA_TYPE_ERR,            /* 获取到了错误的数据类型 */

    DATA_PUSHSTACK_ERR,   /* 数据入栈过程中遇到未知的问题 */
    DATA_POPSTACK_ERR,    /* 数据从状态机中出栈过程中遇到未知的问题 */
    DATA_POPSTACK_NODATA, /* 出栈过程中栈内无数据, 导致出栈失败 */
    /* lua函数或数据绑定错误码 */
    BIND_NAMESPACE_TYPE_ERR = -100, /* 绑定或删除过程中使用命名空间, 但命名空间数据类型不符 */
    BIND_FUNCTION_TYPE_ERR,         /* 绑定或删除函数过程中数据类型错误 */
    BIND_DATA_TYPE_ERR,             /* 绑定或删除全局变量过程中数据类型错误 */
    BIND_DATA_TYPE_UNKNOWN,         /* struct lua_binding_data格式中数据类型无法识别, 或在该功能中操作了不支持的数据类型 */
    /* 通用返回值 */
    PARAM_ERR = -1, /* 传入参数错误, 可能是指针为空或类型不符合 */
    SUCCESS = 0,    /* 运行成功 */
};

/* ***** ***** ***** ***** ***** ***** */
/* 需要注册至lua中的函数 */
typedef int (*lua_cbind_func)(struct lua_state *state);
struct lua_bind_function_spec
{
    lua_cbind_func function; /* 注册函数原型 */
    char *func_name;         /* 注册至lua中的函数名称 */
    char *space_name;        /* 注册至lua中的命名空间名称 */
};
int lua_cbinding_function(struct lua_state *state, struct lua_bind_function_spec bind_function[], size_t bind_func_num);

extern const char *context_magic_code;
struct lua_context
{
    const char *magic_code;
    struct lua_state *state;
    int lua_context_ref_id;
};
struct lua_context *lua_context_new(struct lua_state *state);
void lua_context_free(struct lua_context *context);
void lua_context_push_stack(struct lua_context *context);

enum DATATYPE
{
    DATATYPE_BEGIN = 0,
    DATATYPE_NIL,    /* nil类型 */
    DATATYPE_BOOL,   /* bool类型 */
    DATATYPE_INT,    /* int类型 */
    DATATYPE_NUM,    /* double类型 */
    DATATYPE_STRING, /* 字符串类型 */
    /* 以下类型不能用于全局变量注册 */
    DATATYPE_TABLE,     /* table类型 */
    DATATYPE_LUA_TABLE, /* 此类型用于在lua之间翻译传递数据 */
    DATATYPE_POINTER,   /* 指针类型 */
    DATATYPE_CONTEXT,   /* context上下文类型 */
    DATATYPE_FUNCTION,  /* 函数类型 */
    DATATYPE_END
};

/* 需要注册至lua状态机中的数据 */
struct lua_bind_data_spec
{
    enum DATATYPE data_type; /* 注册的数据类型 */
    char *data_value;        /* 注册数数据值 */
    char *data_name;         /* 注册的数据名称 */
    char *space_name;        /* 注册至lua中的命名空间名称 */
};
int lua_cbinding_data(struct lua_state *state, struct lua_bind_data_spec bind_data[], size_t bind_data_num);

struct lua_cdata;
struct lua_cnode;
struct lua_ctable;

struct lua_cdata
{
    enum DATATYPE type;
    size_t data_len; /* 只有在类型为function时使用此标识 */
    union
    {
        int bool;
        int integer;
        double number;
        char *string;

        int table;
        void *pointer;
        struct lua_context *context;
        void *function;
        struct lua_ctable *ctable;
    };
};

struct lua_cnode
{
    char *node_string_key;
    int node_integer_key;
    struct lua_cdata *node_data;
};

struct lua_ctable
{
    size_t array_size;
    size_t node_size;
    struct lua_cdata **array_data;
    struct lua_cnode **node_data;
};

struct lua_cdata *lua_cdata_new(void);
void lua_cdata_free(struct lua_cdata *cdata);
int lua_cdata_push_stack(struct lua_state *state, struct lua_cdata *cdata);
int lua_cdata_pop_stack(struct lua_state *state, struct lua_cdata *cdata);
struct lua_ctable *lua_ctable_new(void);
void lua_ctable_free(struct lua_ctable *ctable);
int lua_ctable_push_stack(struct lua_state *state, struct lua_ctable *ctable);
int lua_ctable_pop_stack(struct lua_state *state, struct lua_ctable *ctable);
struct lua_cnode *lua_cnode_new(void);
void lua_cnode_free(struct lua_cnode *cnode);

/* ***** ***** ***** ***** ***** ***** */
int lua_chunk_execute(struct lua_state *state, int fn_ref_id, struct lua_cdata param[], size_t param_num, struct lua_cdata returnvalue[], size_t r_num, char *errlog, size_t err_len);

/* ***** ***** ***** ***** ***** ***** */
struct lua_load_script
{
    int lua_load_fn_ref_id;
    int lua_unload_fn_ref_id;
    int lua_script_env_ref_id;
};

struct lua_session_plugin_env
{
    struct lua_session_plugin_env *next;
    struct lua_plugin_manage *plugin_manage;
    int session_plugin_id;
    int lua_ctx_new_fn_ref_id;
    int lua_ctx_free_fn_ref_id;
    int lua_plug_env_ref_id;
};

struct lua_packet_plugin_env
{
    struct lua_packet_plugin_env *next;
    struct lua_plugin_manage *plugin_manage;
    int packet_plugin_id;
    int lua_on_packet_fn_ref_id;
    int lua_plug_env_ref_id;
};

struct lua_message_free_arg
{
    struct lua_message_free_arg *next;
    struct lua_plugin_manage *plugin_manage;
    int topic_id;
    int lua_msg_free_fn_ref_id;
    int lua_msg_free_arg_ref_id;
};

struct lua_on_message_fn
{
    int hash_on_use;
    int topic_id;
    int plugin_id;
    int lua_on_msg_fn_ref_id;
};

#define HASH_MAX_NUM 1024
int calc_on_message_func_hash_key(int topic_id, int plugin_id);
struct lua_on_message_fn *hash_on_msg_fn_insert(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id);
struct lua_on_message_fn *hash_find_on_msg_fn(struct lua_on_message_fn msg_fn_hashlist[], int topic_id, int plugin_id);

#define LUA_GLOBAL_THREAD_ID_KEY "__global_thread_id"
#define LUA_GLOBAL_STELLAR_POINTER "__global_stellar_pointer"
#define LUA_GLOBAL_PLUGIN_MANGE_POINTER "__global_plugin_managee_pointer"

// int lua_state_get_thread_id(struct lua_state * state);
// struct stellar * lua_state_get_stellar(struct lua_state * state);
struct lua_plugin_manage *lua_state_get_plugin_manage(struct lua_state *state);

struct lua_plugin_manage
{
    struct stellar *st;

    size_t state_num;
    size_t load_script_num;
    struct lua_state **state_array;
    struct lua_load_script *load_script_array;

    struct lua_session_plugin_env *session_plugin_env_list;
    struct lua_packet_plugin_env *packet_plugin_env_list;
    struct lua_message_free_arg *message_free_arg_list;

    struct lua_on_message_fn on_session_message_hashlist[HASH_MAX_NUM];
    struct lua_on_message_fn on_packet_message_hashlist[HASH_MAX_NUM];
};

#endif