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.
-- 监听器列表
local listeners = {}
-- 解码器
local decoders = {}
-- 消息唯一ID,服务器透传回客户端。以此验证是同一个请求
local messageId = 0;
local getMessageId = function ()
messageId = messageId + 1;
return messageId;
end
-- 注册监听(回调方法,寄存参数)
local registerListener = function(key,callback,deposit)
if(callback) then
listener[key] = {callback,deposit,os.time()}
end
end
-- 注册解码器(模块ID,方法ID)
local registerDecoder = function(moduleId,methodId,decoder)
local key = moduleId << 16 ~ methodId;
if(decoder) then
listener[key] = decoder;
end
end
-- 接收消息
local receive = function(data)
local buff = ByteArray.new()
buff.writeBuf(data)
local messageId = buff.readInt();
local commandId = buff.readInt();
local status = buff.readInt();
local decoder = decoders[commandId];
-- 执行解码
if(decoder) then
data = decoder(buff);
end
if(messageId > 0) then
-- 一次性监听器
local listener = listeners[messageId];
if(listener) then
pcall(listener[1],listener[2],data)
end
-- 移除监听
listener[messageId] = nil;
else
-- 常驻监听器
local listener = listeners[commandId];
if(listener) then
pcall(listener[1],listener[2],data)
end
end
end
--[[
********************* 协议常量 *********************
--]]
enums = {
<%for(enum in enums){%>
-- ${enum.remark}
${enum.enumName} = {
<%for(field in enum.fields){%>
<%if(!fieldLP.last){%>
${field.key} = ${field.value.name}, -- ${field.value.remark}
<%}else{%>
${field.key} = ${field.value.name}; -- ${field.value.remark}
<%}%>
<%}%>
},
<%}%>
}
--[[
********************* 协议对象 *********************
--]]
structs = {
<%for(struct in structs){%>
-- ${struct.remark}
${struct.structName} = {
pack = function(buff)
<%
for(field in struct.fields){
var v = "";
select(field.value.type){
case "short","java.lang.Short":
v = "buff.writeShort";
case "int","java.lang.Integer":
v = "buff.writeInt";
case "long","java.lang.Long":
v = "buff.writeLong";
case "float","java.lang.Float":
v = "buff.writeFloat";
case "double","java.lang.Double":
v = "buff.writeDouble";
case "String","java.lang.String":
v = "buff.writeString";
case "boolean","java.lang.Boolean":
v = "buff.writeBoolean";
case "Map","java.util.Map":
v = "buff.writeMap";
case "List","java.util.List":
v = "buff.writeCollection";
default:
v = "buff.write";
}
%>
${v}(m.${field.key.name}); -- ${field.key.remark}
<%}%>
return buff;
end,
unpack = function(buff)
local r = {}
<%
for(field in struct.fields){
var v = "";
select(field.value.type){
case "short","java.lang.Short":
v = "buff.readShort(nil)";
case "int","java.lang.Integer":
v = "buff.readInt(nil)";
case "long","java.lang.Long":
v = "buff.readLong(nil)";
case "float","java.lang.Float":
v = "buff.readFloat(nil)";
case "double","java.lang.Double":
v = "buff.readDouble(nil)";
case "String","java.lang.String":
v = "buff.readString(nil)";
case "boolean","java.lang.Boolean":
v = "buff.readBoolean(nil)";
case "Map","java.util.Map":
v = "buff.readMap(nil)";
case "List","java.util.List":
v = "buff.readCollection(nil)";
default:
v = "("+field.value.type+")buff.read()";
}
%>
r.${field.key.name} = ${v}; -- ${field.key.remark}
<%}%>
return r;
end,
},
<%}%>
--[[
********************* 系统模块 *********************
--]]
module = {
<%for(module in modules){%>
${module.name} = {
local _moduleId = ${module.id};
<%for(send in module.protocolSends){%>
local _${send.requestName} = ${send.id};
<%}%>
<%for(push in module.protocolPushs){%>
local _${push.pushName} = ${push.id};
<%}%>
<%for(send in module.protocolSends){%>
<%
var values = "";
for(rp in send.requestParameter){
values = values + rp.key + " ,";
}
%>
-- ${send.remark}
${send.requestName} = function(${values}callback)
local buff = ByteArray.new()
local messageId = string.pack("
${v}(${rp.key});
<%}%>
socket.send(buff.getPack())
end
<%}%>
-- 解码器
<%for(send in module.protocolSends){%>
registerDecoder(_moduleId,_${send.requestName},function(data){
local o = {}
<%
for(rp in send.responseParameter){
var v = "";
select(rp.value.type){
case "short","java.lang.Short":
v = "buff.readShort(nil)";
case "int","java.lang.Integer":
v = "buff.readInt(nil)";
case "long","java.lang.Long":
v = "buff.readLong(nil)";
case "float","java.lang.Float":
v = "buff.readFloat(nil)";
case "double","java.lang.Double":
v = "buff.readDouble(nil)";
case "String","java.lang.String":
v = "buff.readString(nil)";
case "boolean","java.lang.Boolean":
v = "buff.readBoolean(nil)";
case "Map","java.util.Map":
v = "buff.readMap(nil)";
case "List","java.util.List":
v = "buff.readCollection(nil)";
default:
v = rp.value.type+".unpack()";
}
%>
o.${rp.key} = ${v};
<%}%>
return o;
})
<%}%>
<%for(push in module.protocolPushs){%>
registerDecoder(_moduleId,_${push.pushName},function(data){
local o = {}
<%
for(rp in push.pushParameter){
var v = "";
select(rp.value.type){
case "short","java.lang.Short":
v = "buff.readShort(nil)";
case "int","java.lang.Integer":
v = "buff.readInt(nil)";
case "long","java.lang.Long":
v = "buff.readLong(nil)";
case "float","java.lang.Float":
v = "buff.readFloat(nil)";
case "double","java.lang.Double":
v = "buff.readDouble(nil)";
case "String","java.lang.String":
v = "buff.readString(nil)";
case "boolean","java.lang.Boolean":
v = "buff.readBoolean(nil)";
case "Map","java.util.Map":
v = "buff.readMap(nil)";
case "List","java.util.List":
v = "buff.readCollection(nil)";
default:
v = rp.value.type+".unpack()";
}
%>
o.${rp.key} = ${v};
<%}%>
return o;
})
<%}%>
}
<%}%>
}