1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2024-12-27 09:58:21 +01:00
antd-lua-plugin/lua-api.c

65 lines
1.4 KiB
C
Raw Normal View History

2019-11-19 17:13:39 +01:00
#define PLUGIN_IMPLEMENT 1
2020-01-01 21:17:46 +01:00
#include <dlfcn.h>
2020-01-02 17:51:08 +01:00
#include <antd/plugin.h>
2020-08-25 17:09:04 +02:00
#include <antd/scheduler.h>
#include <antd/dbhelper.h>
#include <sys/stat.h>
2020-01-02 17:51:08 +01:00
#include "lib/lualib.h"
2018-09-19 15:08:49 +02:00
2021-01-01 15:22:25 +01:00
#define LUA_HDL_FN "lua_handle"
static void* core = NULL;
static void* lua_handle = NULL;
static void *(*handle_fn)(void *, void*);
2018-09-19 15:08:49 +02:00
void init()
{
2021-01-01 15:22:25 +01:00
char* error;
2020-01-01 21:17:46 +01:00
char* path = __s("%s/lua/core.so", __plugin__.pdir);
2021-01-01 15:22:25 +01:00
core = dlopen(path, RTLD_NOW| RTLD_GLOBAL);
free(path);
if(!core)
2020-01-01 21:17:46 +01:00
{
2021-01-01 15:22:25 +01:00
ERROR("Cannot load Lua core: %s", dlerror());
return;
2020-01-01 21:17:46 +01:00
}
2021-01-01 15:22:25 +01:00
LOG("Lua core loaded");
// now load the handle
path = __s("%s/lua/handle.so", __plugin__.pdir);
lua_handle = dlopen(path, RTLD_LAZY);
free(path);
if(!lua_handle)
2020-01-01 21:17:46 +01:00
{
2021-01-01 15:22:25 +01:00
ERROR("Cannot load lua_handle: %s", dlerror());
return;
}
// find the fn
handle_fn = (void *(*)(void *))dlsym(lua_handle, LUA_HDL_FN);
if ((error = dlerror()) != NULL)
{
ERROR("Problem when finding %s method from handle : %s", LUA_HDL_FN, error);
handle_fn = NULL;
return;
2020-01-01 21:17:46 +01:00
}
2018-09-19 15:08:49 +02:00
}
2018-10-05 19:02:35 +02:00
void* handle(void* data)
2018-09-19 15:08:49 +02:00
{
2021-01-01 15:22:25 +01:00
plugin_header_t* meta_ptr = (void*)meta();
antd_request_t *rq = (antd_request_t *)data;
// find the handle function and execute it
if(!handle_fn)
2018-09-19 15:08:49 +02:00
{
2021-01-01 15:22:25 +01:00
antd_error(rq->client, 503, "Requested service not found");
return antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
2018-09-19 15:08:49 +02:00
}
2021-01-01 15:22:25 +01:00
return handle_fn(data, meta_ptr);
2018-09-19 15:08:49 +02:00
}
2018-10-05 19:02:35 +02:00
void destroy()
2018-09-19 15:08:49 +02:00
{
2021-01-01 15:22:25 +01:00
if(core)
(void)dlclose(core);
if(lua_handle)
(void)dlclose(lua_handle);
2020-01-01 21:17:46 +01:00
LOG("Exit LUA Handle");
2018-09-19 15:08:49 +02:00
}