--在这里添加网络消息定义
require("script/net/LoginNetDefine")
--初始化网络消息
local Logic2ClientPrtl = require("script/proto/Logic2Client")
assert(Logic2ClientPrtl,"Logic2Client.lua load failed!")
local Logic2ClientIndex = {}
--做成序列,并且检查是否有重复
for i,v in ipairs(Logic2ClientPrtl) do
--如果重复了。要报错。不能继续下去。
if( Logic2ClientIndex[ v[1] ] ) then
error(v[1] .. " repeat def in Logic2ClientPrtl")
end
Logic2ClientIndex[ v[1] ] = i;
end
local Client2LogicPtrl = require("script/proto/Client2Logic")
assert(Client2LogicPtrl,"Client2Logic.lua load failed!")
--服务器端不需要这个
--[[
local Client2LogicIndex = {}
--做成序列,并且检查是否有重复
for i,v in ipairs(Client2LogicPtrl) do
--如果重复了。要报错。不能继续下去。
if( Client2LogicIndex[ v[1] ] ) then
error(v[1] .. " repeat def in Client2LogicPrtl")
end
Client2LogicIndex[ v[1] ] = i;
end
--]]
--调用这个接口的。必须是可以发送消息的,也就是有定义 send(SendSerialBuf)
--要实现广播。这里得user可以是map。那就是地图广播,server,那就是全服广播,有条件得集合,那么要自己创建了
function Logic2Client(cmd,sck,...)
--查找是否有这个cmd
local index = Logic2ClientIndex[cmd]
assert(index,cmd .. " not define in Logic2Cient")
local args = {...}
local types = Logic2ClientPrtl[index][2]
assert(table.getn(args) == string.len(types),cmd .. " in Logic2ClientPrtl args and types not equal")
local buf = SendSerialBuf:new()
buf:beginMsg(index)
for i,arg in ipairs(args) do
local t = string.char(string.byte(types,i))
if t == 'b' then
assert( type(arg) == "boolean",cmd .. " in Logic2ClientPrtl args index " .. i .. " is not boolean!")
buf:pushBool(arg)
elseif t == 'i' then
assert( type(arg) == "number",cmd .. " in Logic2ClientPrtl args index " .. i .. " is not number!")
buf:pushInt(arg)
elseif t == 'u' then
assert( type(arg) == "number",cmd .. " in Logic2ClientPrtl args index " .. i .. " is not number!")
buf:pushUint(arg)
elseif t == 's' then
assert( type(arg) == "string",cmd .. " in Logic2ClientPrtl args index " .. i .. " is not string!")
buf:pushString(arg)
elseif t == 'a' then
assert( type(arg) == "table",cmd .. " in Logic2ClientPrtl args index " .. i .. " is not table!")
buf:pushArray(arg)
else
assert(false,cmd .. " in Logic2ClientPrtl args index " .. i .. " is not valid!")
end
end
sck:sendMsg(buf)
buf:delete()
end
function Client2Logic(user,buf)
local index = buf:beginMsg()
local proto = Client2LogicPtrl[index]
assert(proto,index .. " cmd is not declare in Client2LogicPtrl")
local func = _G[ proto[1] ]
assert(func, index .. " cmd is not def in logic")
local args = {}
local types = proto[2]
local sz = string.len(types)
for i = 1,sz do
local t = string.char(string.byte(types,i))
local value
if t == 'b' then
value = buf:getBool(i)
elseif t == 'i' then
value = buf:getInt(i)
elseif t == 'u' then
value = buf:getUint(i)
elseif t == 's' then
value = buf:getString(i)
elseif t == 'a' then
value = buf:getArray(i)
else
assert(false,index .. " cmd in Client2LogicPtrl args index " .. i .. " is not valid!")
end
table.insert(args,value)
end
func(user,unpack(args))
end