-
Notifications
You must be signed in to change notification settings - Fork 11
/
tdlua.cpp
205 lines (189 loc) · 5.28 KB
/
tdlua.cpp
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
/**
* @author Giuseppe Marino
* ©Giuseppe Marino 2018 - 2018
* This file is under GPLv3 license see LICENCE
*/
#include "tdlua.h"
#include "luajson.h"
static int tdclient_new(lua_State *L)
{
luaL_newmetatable(L, "tdclient");
lua_pushstring(L, "__index");
luaL_newlib(L, mt);
lua_newtable(L);
lua_pushstring(L, "__index");
lua_pushcfunction(L, tdclient_call);
lua_settable(L, -3);
lua_setmetatable(L, -2);
lua_settable(L, -3);
lua_pushstring(L, "__gc");
lua_pushcfunction(L, tdclient_unload);
lua_settable(L, -3);
TDLua **client = (TDLua**)(lua_newuserdata(L, sizeof(void*)));
*client = new TDLua();
luaL_setmetatable(L, "tdclient");
return 1;
}
static int tdclient_receive(lua_State *L)
{
TDLua* td = getTD(L);
if (!td->empty()) {
std::string res = td->pop();
lua_pushjson(L, res);
return 1;
}
lua_Number timeout = 10;
if(lua_type(L, 2) == LUA_TNUMBER) {
timeout = lua_tonumber(L, 2);
}
std::string result = td->receive(timeout);
if(result.empty()) {
lua_pushnil(L);
} else {
lua_pushjson(L, result);
}
return 1;
}
static int tdclient_send(lua_State *L)
{
TDLua *td = getTD(L);
if(lua_type(L, 2) == LUA_TSTRING) {
td->send(lua_tostring(L, 2));
} else if(lua_type(L, 2) == LUA_TTABLE) {
std::string j;
lua_getjson(L, j);
td->send(j);
}
return 0;
}
static int tdclient_execute(lua_State *L)
{
if(lua_type(L, 2) == LUA_TSTRING) { // [client, json]
std::string j = lua_tostring(L, -1); // decode json string to lua table
lua_pop(L, 1); // [client]
lua_pushjson(L, j); // [client, table]
}
if (lua_type(L, 2) == LUA_TTABLE) { // [client, table]
int nonce = rand();
lua_pushstring(L, "@extra"); // [client, table, "@extra"]
lua_pushinteger(L, nonce); // [client, table, "@extra", nonce]
lua_settable(L, -3); // [client, table]
tdclient_send(L); // [client, table]
TDLua *td = getTD(L);
while (true) {
lua_checkstack(L, 3);
std::string res = td->receive(1);
lua_pushjson(L, res); // [client, table, restable]
if (lua_type(L, -1) != LUA_TTABLE) { // nil
continue;
}
lua_pushstring(L, "@extra"); // [client, table, restable, "@extra"]
lua_gettable(L, -2); // [client, table, restable, restable["@extra"]]
if (lua_type(L, -1) == LUA_TNUMBER && lua_tointeger(L, -1) == nonce) {
lua_pop(L, 1); // [client, table, restable]
return 1;
} else {
lua_pop(L, 1); // [client, table, restable]
td->push(res);
}
}
}
return 0;
}
static int call(lua_State *L)
{
auto f = tdclient_execute;
if (lua_type(L, -1) == LUA_TBOOLEAN) {
if (lua_toboolean(L, -1))
f = tdclient_send;
lua_pop(L, 1);
}
if (lua_type(L, -1) != LUA_TTABLE) lua_newtable(L);
lua_pushstring(L, "@type");
lua_pushstring(L, lua_tostring(L, lua_upvalueindex(1)));
lua_settable(L, -3);
return f(L);
}
static int tdclient_call(lua_State *L)
{
lua_pushcclosure(L, call, 1);
return 1;
}
static int tdclient_rawexecute(lua_State *L)
{
TDLua *td = getTD(L);
if(lua_type(L, 2) == LUA_TSTRING) {
std::string result = td->execute(lua_tostring(L, 2));
lua_pushstring(L, result.c_str());
return 1;
} else if(lua_type(L, 2) == LUA_TTABLE) {
std::string j;
lua_getjson(L, j);
std::string result = td->execute(j);
if(result.empty()) {
lua_pushnil(L);
} else {
lua_pushjson(L, result);
}
return 1;
}
return 0;
}
static int tdclient_unload(lua_State *L)
{
TDLua *td = getTD(L);
delete td;
return 0;
}
static void tdclient_fatalerrorcb(const char * error)
{
std::cerr << "[TDLUA FATAL ERROR] " << error << std::endl;
}
static int tdclient_setlogpath(lua_State *L)
{
if(lua_type(L, 1) == LUA_TSTRING) {
lua_pushboolean(L, td_set_log_file_path(lua_tostring(L, 1)));
} else {
lua_pushboolean(L, 0);
}
return 1;
}
static int tdclient_setlogmaxsize(lua_State *L)
{
if(lua_type(L, 1) == LUA_TNUMBER) {
td_set_log_max_file_size(lua_tointeger(L, 1));
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
return 1;
}
static int tdclient_setlogverbosity(lua_State *L)
{
if(lua_isinteger(L, 1)) {
td_set_log_verbosity_level(static_cast<int>(lua_tointeger(L, 1)));
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
}
//Open Lib
static luaL_Reg tdlua[] = {
{"new", tdclient_new},
{"setLogPath", tdclient_setlogpath},
{"setLogMaxSize", tdclient_setlogmaxsize},
{"setLogLevel", tdclient_setlogverbosity},
{nullptr, nullptr}
};
extern "C" {
LUALIB_API int luaopen_tdlua(lua_State *L) {
luaL_newmetatable(L, "tdlua");
lua_pushstring(L, "__call");
lua_pushcfunction(L, tdclient_new);
lua_settable(L, -3);
luaL_newlib(L, tdlua);
luaL_setmetatable(L, "tdlua");
td_set_log_fatal_error_callback(tdclient_fatalerrorcb);
return 1;
}
}