Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
do
Network = {}
-- 回调
-- 客户端每发一个请求都会注册一个回调,回调的唯一的,并且仅触发一次。一般做法是生成一个唯一key来进行回调映射并且作为发送的packet.id值,服务器会在返回的packet中透传回客户端
local callbacks = {}
-- 监听器
-- 监听器的主要作用是,监听服务器主动推送的消息监听。便于我们处理代码业务
local listeners = {}
-- 解码器:
-- 之所有此处还需要一次解码,是因为在网络传输过程中所有的消息key是转为了hashcode进行传输,
-- 占用字节更少,所以需要把hashcode转回我们代码中使用的字符key。节约宽带提高传输效率
-- 如果是不传数字key时,可以删除解码器
decoders = {}
-- 结构体
structs = {}
-- 判断是否是数组
isArray = function(t)
if t == nil then
return false, 0;
end
local i = 0
local ret = true;
for _ in pairs(t) do
i = i + 1
if t[i] == nil then
ret = false
end
end
return ret, i
end
objectDecode = function(o)
local isTable = type(o)
if isTable == "table" then
local isArray, len = isArray(o)
if isArray then
return listDecode(o)
else
local struct = structs[o[183928832]]
if struct then
return struct.decode(o)
else
return mapDecode(o)
end
end
else
return o;
end
end
-- 解析List
listDecode = function(o)
local ret = {};
for i, v in pairs(o) do
table.insert(ret, objectDecode(v))
end
return ret
end
mapDecode = function(map)
local ret = {};
for key, value in pairs(map) do
ret[objectDecode(key)] = objectDecode(value)
end
return ret;
end
objectEncode = function(o)
local isTable = type(o)
if isTable == "table" then
local isArray, len = isArray(o)
if isArray then
return listEncode(o)
else
local struct = structs[o[183928832]]
if struct then
local r = struct.encode(o)
r[183928832] = o[183928832];
return r;
else
return mapEncode(o)
end
end
else
return o;
end
end
-- 解析List
listEncode = function(o)
local ret = {};
for i, v in pairs(o) do
table.insert(ret, objectEncode(v))
end
return ret
end
mapEncode = function(map)
local ret = {};
for key, value in pairs(map) do
ret[objectEncode(key)] = objectEncode(value)
end
return ret;
end
-- 注册回调(回调函数,上下文)
local registerCallback = function (callback, context)
if callback then
callbacks[callbackKey] = {callback, context}
end
end
-- 注册监听器(监听处理函数)
local registerListener = function (callback)
if callback then
listeners[callbackKey] = {callback}
end
end
-- 分发器、接收服务器消息的唯一入口,便于处理错误码等情况
Network.disp = function(packet)
-- 回调
local callback = callbacks[callbacks.Id];
-- 监听
local listener = listeners[callbacks.CommandId];
if packet.status ~= 0 then
-- 错误码提示:
else
-- 消息解码
local result = Network.decoder[callbacks.CommandId](packet.Body)
-- 执行回调
if callback then
pcall(callback[1], callback[2], result)
end
-- 执行监听
if listener then
pcall(listener[1], result)
end
end
-- 移除回调
if callback then
callbacks[callbacks.Id] = nil
end
end
-- 发送消息模块,类似RPC调用,调用者不必关心内部实现
Network.module = {
<%for(module in modules){%>
${module.name} = {
-- 请求
<%
for(send in module.protocolSends){
// 参数拼接
var values = "";
for(rp in send.requestParameter){
values = values + rp.key;
if(!rpLP.last){
values = values + ",";
}
}
%>
-- ${send.remark}
${send.requestName} = function(${values},callback,context)
-- 1.包封装
local packet = {
-- Head
[1] = {
[1] = os.time(),
[2] = ${ProtocolUtil.getCommandId(module.id,send.id)}
},
-- Body
[2] = {
<%
for(rp in send.requestParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
%>
[${hashCode}] = objectEncode(${rp.key});
<%}%>
}
}
-- 2.注册回调
registerCallback(callback,context)
-- 3.执行发送
socket.send(packet);
end,
<%}%>
-- 推送
<%
for(push in module.protocolPushs){
%>
-- ${push.remark}
${push.pushName} = ${ProtocolUtil.getCommandId(module.id,push.id)};
<%}%>
},
<%}%>
}
-- *************** 解码器 ***************
<%for(module in modules){%>
<%for(send in module.protocolSends){%>
-- ${send.remark}
decoders[${ProtocolUtil.getCommandId(module.id,send.id)}] = function(body)
local o = {}
<%
for(rp in send.responseParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
%>
o.${rp.key} = objectDecode(body[${hashCode}]);
<%}%>
return o;
end
<%}%>
<%for(push in module.protocolPushs){%>
-- ${push.remark}
decoders[${ProtocolUtil.getCommandId(module.id,push.id)}] = function(body)
local o = {}
<%
for(rp in push.pushParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
%>
o.${rp.key} = objectDecode(body[${hashCode}]);
<%}%>
return o;
end
<%}%>
<%}%>
-- *************** 结构体 ***************
<%
for(struct in structs){
var def_encode = "{}";
var def_decode = "{}";
if(strutil.isNotBlank(struct.extendsName)){
def_encode = struct.extendsName + ".encode(o)";
def_decode = struct.extendsName + ".decode(o)";
}
%>
-- ${struct.remark}
${struct.structName} = {
encode = function(o)
local r = ${def_encode};
<%
for(field in struct.fields){
var hashCode = HashUtil.javaDefaultHash(field.key.name);
%>
r[${hashCode}] = objectEncode(o.${field.key.name}); -- ${field.key.remark}
<%}%>
return r
end,
decode = function(o)
local r = ${def_decode};
<%
for(field in struct.fields){
var hashCode = HashUtil.javaDefaultHash(field.key.name);
%>
r.${field.key.name} = objectDecode(o[${hashCode}]); -- ${field.key.remark}
<%}%>
return r
end,
new = function()
return {[183928832] = ${HashUtil.javaDefaultHash(struct.structName)}};
end
}
<%}%>
<%
for(struct in structs){
var hashCode = HashUtil.javaDefaultHash(struct.structName);
%>
structs[${hashCode}] = ${struct.structName}
<%}%>
end