-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLua_C.cpp
277 lines (229 loc) · 5.46 KB
/
Lua_C.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "Lua_C.h"
#include "lua-seri.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <vector>
#include <sys/time.h>
#include "ServiceObj.h"
#include "Socket_Poll.h"
#include "Timer_Queue.h"
int traceback (lua_State *L) {
if(L == NULL)
return 0;
lua_getfield(L, LUA_REGISTRYINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1); /* pass error message */
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
return 1;
}
int _callback(lua_State *L)
{
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, 1);
lua_rawsetp(L, LUA_REGISTRYINDEX, "callback");
return 0;
}
int
llisten(lua_State *L)
{
int port = luaL_checkinteger(L, 1);
int backlog = luaL_optinteger(L, 2, 48);
lua_getfield(L, LUA_REGISTRYINDEX, "service_handle");
int handle = lua_tointeger(L,-1);
int id = do_listen(handle, port, backlog);
if (id < 0) {
return luaL_error(L, "Listen error");
}
lua_pushinteger(L, id);
return 1;
}
int
lconnect(lua_State *L) {
size_t sz = 0;
const char * addr = luaL_checklstring(L, 1, &sz);
int port = luaL_checkinteger(L, 2);
int sock_cli = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(addr);
if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("connect");
exit(1);
}
connection_st* ptc = (connection_st*)(Allocator_Manager::instance()->malloc(sizeof(connection_st)));
ptc->sock = sock_cli;
ptc->rbuf = new Stream_Base(10240);
ptc->type = T_CONNECT;
ptc->epfd = sock_cli%EPOLL_NUM;
lua_getfield(L, LUA_REGISTRYINDEX, "service_handle");
ptc->service_handle = lua_tointeger(L, -1);
printf("zone sock_cli %d\n", sock_cli);
SOCKET_SERVER.slot[sock_cli] = ptc;
struct epoll_event evReg;
evReg.events = EPOLLIN | EPOLLONESHOT;
evReg.data.ptr = ptc;
epoll_ctl(SOCKET_SERVER.epfd[sock_cli%EPOLL_NUM], EPOLL_CTL_ADD, sock_cli, &evReg);
lua_pushinteger(L, sock_cli);
return 1;
}
int
lclose(lua_State *L) {
int sock_cli = luaL_checkinteger(L, 1);
closeConnection(sock_cli);
return 0;
}
int
lsend(lua_State *L) {
static char* sbuf = NULL;
int id = luaL_checkinteger(L, 1);
struct block temp;
temp.next = NULL;
struct write_block wb;
wb_init(&wb, &temp);
pack_from(L, &wb, 1);
block* b = &temp;
int len = wb.len;
// TODO ·ÖÅäÄÚ´æ ¿ÉÓÅ»¯
char * buffer = (char*)malloc(len);
char * ptr = buffer;
int sz = len;
while (len > 0)
{
if (len >= BLOCK_SIZE)
{
memcpy(ptr, b->buffer, BLOCK_SIZE);
ptr += BLOCK_SIZE;
len -= BLOCK_SIZE;
b = b->next;
}
else
{
memcpy(ptr, b->buffer, len);
break;
}
}
wb_free(&wb);
lua_pushinteger(L, sendData(SOCKET_SERVER.slot[id], buffer, len));
free(buffer);
return 1;
}
int
lregtimer(lua_State *L)
{
int secs = luaL_checkinteger(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "service_handle");
int _handle = lua_tointeger(L, -1);
Time_Value tv;
tv.msec(secs);
lua_pushinteger(L, Timer_Queue::instance()->schedule_timer(_handle, tv));
return 1;
}
ServiceLua::ServiceLua(const char* path, int port) :m_path(path), m_port(port)
{}
ServiceLua::~ServiceLua()
{}
int lua_openmylib(lua_State *L)
{
luaL_Reg l[] = {
{ "unpack", _luaseri_unpack },
{ "callback", _callback },
{ "listen", llisten },
{ "connect", lconnect },
{ "close", lclose },
{ "send", lsend },
{ "schedule_timer", lregtimer },
{ NULL, NULL },
};
luaL_newlib(L, l);
return 1;
};
void ServiceLua::Init(int handle)
{
ServiceObj::Init(handle);
L = luaL_newstate();
luaL_openlibs(L);
lua_pushinteger(L, handle);
lua_setfield(L, LUA_REGISTRYINDEX, "service_handle");
SetGlb();
luaL_requiref(L, "c_fun", lua_openmylib, 0);
int status = luaL_dofile(L, m_path);
if (status != 0)
{
printf("Lua Error ======================================================\n");
printf("%s\n", lua_tostring(L, -1));
printf("========= ======================================================\n");
return;
}
}
void ServiceLua::Free()
{}
void ServiceLua::Dispatch(Message& msg)
{
int trace = 1;
int r;
int top = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_rawgetp(L, LUA_REGISTRYINDEX, "callback");
lua_pushinteger(L, msg.getFd());
lua_pushinteger(L, msg.getType());
if (msg.getMsg())
{
lua_pushlightuserdata(L, (void *)msg.getMsg()->get_base_ptr());
lua_pushinteger(L, msg.getMsg()->get_readable());
r = lua_pcall(L, 4, 0, trace);
}
else
{
r = lua_pcall(L, 2, 0, trace);
}
if (r != LUA_OK) {
switch (r) {
case LUA_ERRRUN:
break;
case LUA_ERRMEM:
break;
case LUA_ERRERR:
break;
case LUA_ERRGCMM:
break;
};
}
lua_pop(L, 1);
return;
}
void ServiceLua::SetGlb()
{
lua_newtable(L);
lua_pushnumber(L, m_port);
lua_setfield(L, -2, "port");
std::map<std::string, std::string> ms = ServiceMgr::instance()->GetServicesMap();
for (std::map<std::string, std::string>::iterator iter = ms.begin(); iter != ms.end(); ++iter)
{
lua_pushstring(L, iter->second.c_str());
lua_setfield(L, -2, iter->first.c_str());
}
lua_setglobal(L, "c_global");
}