-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmy_ffi.lua
74 lines (57 loc) · 1.77 KB
/
my_ffi.lua
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
local ffi = require('ffi')
local C = ffi.C
require('tools.error_handle')
print (jit.version)
--setmetatable(_G, {__newindex = function(_,n) print('Attempt to write to undeclared variable '..n,2) end,
-- __index = function(_,n) print('Attempt to read undeclared variable '..n,2) end, })
local function load_c_def(file_name)
local f = io.open(file_name)
local s = f:read('*all')
s = s:gsub('#include', '//#include')
s = s:gsub('}//namespace', '//}//namespace')
s = s:gsub('namespace', '//namespace')
local chunk = 'require("ffi").cdef[[\n' .. s .. '\n]]'
assert(loadstring(chunk)) ()
end
local function load_all_c_defs()
load_c_def('c_def/define.h')
load_c_def('c_def/game_def.h')
load_c_def('c_def/data.h')
load_c_def('c_def/mq.h')
load_c_def('c_def/town.h')
load_c_def('c_def/broadcast.h')
print 'All C structors are ok!'
end
local function define_c_functions()
if ffi.os=='Windows' then
ffi.cdef[[ typedef void (__stdcall *TimerCallback)(int timer_id);]]
else
ffi.cdef[[typedef void ( *TimerCallback)(int timer_id);]]
end
ffi.cdef[[
void Send2Db(MqHead& head, void* data, int len);
void Send2Gate(MqHead& head, void* data, int len);
void Send2WorldWar(MqHead& head, void* data, int len);
void Send2GM( const MqHead& head, void* data, int len );
void Send2Interact( const MqHead& head, void* data, int len );
void* MovePtr( void* ptr, int offset );
enum NodeType
{
kServer,
kClient
};
int CreateTimer(TimerCallback cb, int seconds);
void StopTimer(int timer_id);
void ResetTimer(int timer_id, int seconds);
]]
print('c functions defined!')
end
load_all_c_defs()
define_c_functions()
function ffi.CreateTimer(cb, seconds)
local function CallBack()
xpcall(cb, ErrorHandle)
end
return ffi.C.CreateTimer(CallBack, seconds)
end
return ffi