class MessageBlock
{
...
///
int write_char(const char value);
int write_short(const short value);
int write_int(const int value);
int write_string(const char* ss);
char read_char();
short read_short();
int read_int();
char* read_string();
}
int MessageBlock::write_char(const char value)
{
(*this) << value;
return 0;
}
int MessageBlock::write_short(const short value)
{
(*this) << value;
return 0;
}
int MessageBlock::write_int(const int value)
{
(*this) << value;
return 0;
}
int MessageBlock::write_string(const char* ss)
{
(*this) << ss;
return 0;
}
char MessageBlock::read_char()
{
char a;
(*this) >> a;
return a;
}
short MessageBlock::read_short()
{
short s=0;
(*this) >> s;
return s;
}
int MessageBlock::read_int()
{
int i=0;
(*this) >> i;
return i;
}
char* MessageBlock::read_string()
{
static char ss[150];
(*this) >> ss;
return ss;
}
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
#include "lua_tinker.h"
int GameMonitor::lua_init()
{
this->L = lua_open();
luaopen_base(this->L); //
luaopen_string(this->L);
return 0;
}
int GameMonitor::lua_class()
{
//注冊MessageBlock 到lua
return 0;
}
int GameMonitor::lua_method()
{
return 0;
}
int GameMonitor::lua_member()
{
//如需要,注冊一些成員
return 0;
}
int GameMonitor::lua_batfile()
{
//運行腳本 進入主邏輯循環
lua_tinker::dofile(this->L,"LuaScript/MsgPrc.lua");
return 0;
}
--msg = MessageBlock(1024)
--
--msg:write_int(10)
--
--print (msg:read_int())
--
--a = "sdsdcds"
--
--msg:write_string(a);
--
--print (msg:read_string())
local s_msghead = {}
local msg
--主邏輯,死循環,用來處理消息
local function run_logic()
while true do
msg = peek_msg()
if msg == nil then
sleep(1)
else
process_msg(msg)
end
end
end
local function process_msg(msg)
msg_head = msg:read_int()
if s_msghead[msg_head] then
s_msghead[msg_head](msg)
end
end
--在此注冊消息處理邏輯
local function init_msghead()
s_msghead[10001] = process_10001
s_msghead[10002] = process_10002
end
local function process_10002(msg)
msg.read_int(a);
msg.read_string(b);
--process
remsg = MessageBlock(50);
remsg.write_int(100)
remsg.write_string("ok");
respond_to_client(remsg);
end
--process_10001
init_msghead()
run_logic()