local NetProtoBufDispatcher = {} local S2C = require("Network/S2C.lua") local pb = require("pb") local protoc = require("Base/protoc.lua") local protoBufConfig = require("Protos/ProtoBufConfig.lua") local networkManager = require("Network/NetworkManager.lua") function NetProtoBufDispatcher.SendData(msgType, tblData) local proto = protoBufConfig.tblMsgTypeToProto[msgType] if proto == nil then CS.ZLog.Log.Error(string.format("MsgType not found %s", msgType)) return end local bytes = assert(pb.encode(proto, tblData)) local _, szTimeFormat = require("Base/Utils.lua"):TimeNow() print("----> C2S: protobuf: message: szTimeFormat:", szTimeFormat, msgType) PrintTable(tblData,"protobuf: ") local rpcId = CS.LuaSocketManager.Send(msgType, bytes, 1, 0) end function NetProtoBufDispatcher.RecvData(serverMessage) local proto = protoBufConfig.tblMsgTypeToProto[serverMessage.msgType] if proto == nil then CS.ZLog.Log.Error(string.format("MsgType not found %s", msgType)) return end cast(CS.System.Text.Encoding.UTF8, typeof(CS.System.Text.Encoding)) local stream = serverMessage.msgData local szMsgData = CS.System.Text.Encoding.UTF8:GetString(stream:GetBuffer(), stream.Position, stream.Length - stream.Position) local serverProtocol = pb.decode(proto, szMsgData) local callback = S2C[serverMessage.msgType] if not callback then CS.ZLog.Log.Error(string.format("NetworkManager Get callback failed for : %s\n%s" , serverMessage.msgType, serverMessage.msgData)) else callback(serverProtocol) end end local function LoadProto(szProtoFilename) local protoTextAsset = LoadResource(szProtoFilename) assert(protoc:load(protoTextAsset.text)) end for _,fname in ipairs(protoBufConfig.ArrProtoFiles) do LoadProto(fname) end networkManager:RegisterDispatcher(1, NetProtoBufDispatcher) return NetProtoBufDispatcher