public void SerializeData(SendDataStruct data) { MemoryStream memStream = new MemoryStream(); ProtoBuf.Serializer.Serialize(memStream, data.instance); byte[] body = memStream.ToArray(); CMsg _nnn = new CMsg(); _nnn.dest = data.dest; _nnn.cmd = data.cmd; if (body != null && body.Length > 0) { _nnn.body = body; } //放在消息队列里面发 messageStruct smsg = new messageStruct(); smsg.dest = data.dest; smsg.cmd = data.cmd; smsg.body = PBParseManage.getSerialize <CMsg>(_nnn); NetWorkManage.getInstance().PushSendDataQueue(smsg); }
//解析协议信息 private void ParseData(Stream source) { CMsg cmsg = ProtoBuf.Serializer.Deserialize <CMsg>(source); int cmd = cmsg.cmd; int dest = cmsg.dest; long serverTime = cmsg.serverTime; byte[] body = cmsg.body; //消息号保存进队列 PBNO _pb = new PBNO(); _pb.msg = cmsg; _pb.pbInstance = null; //分发协议去解析 _pb.pbInstance = PBParseManage.getInstance().ParseData(dest, cmd, body); if (IsOftenData(dest, cmd)) { recDataScene.Enqueue(_pb); } else { recDataQueue.Enqueue(_pb); } Debug.Log("收到消息 dest=" + dest + ",cmd=" + cmd); }
public static PBParseManage getInstance() { if (s_PBParseManage == null) { s_PBParseManage = new PBParseManage(); s_PBParseManage.Init(); } return(s_PBParseManage); }
//发送协议数据到服务器 只传协议号,没有内容 public static void SendData(int dest, int cmd) { #if UNITY_EDITOR Debug.Log("要发送协议:dest=" + dest + ",cmd=" + cmd); #endif SendPBMsg smsg = new SendPBMsg(); smsg.dest = dest; smsg.cmd = cmd; CMsg _nnn = new CMsg(); _nnn.dest = dest; _nnn.cmd = cmd; smsg.sendBody = PBParseManage.getSerialize <CMsg>(_nnn); sendMemory.Add(smsg); }
public object DeserializeData(object source) { Debug.Log("DeserializeData"); if (source == null) { return(null); } Stream stream = (Stream)source; CMsg cmsg = ProtoBuf.Serializer.Deserialize <CMsg>(stream); int cmd = cmsg.cmd; int dest = cmsg.dest; long serverTime = cmsg.serverTime; byte[] body = cmsg.body; //消息号保存进队列 PBNO _pb = new PBNO(); _pb.msg = cmsg; //分发协议去解析 PBParseManage.getInstance().ParseData(dest, cmd, body); if (NetWorkManage.getInstance().IsOftenData(dest, cmd)) { NetWorkManage.getInstance().PushReceDataScene(_pb); } else { NetWorkManage.getInstance().PushReceDataQueue(_pb); } Debug.Log("收到消息 dest=" + dest + ",cmd=" + cmd); return(null); }
//发送协议数据到服务器 public static void SendData <T>(int dest, int cmd, T instance) where T : global::ProtoBuf.IExtensible { #if UNITY_EDITOR Debug.Log("要发送协议:dest=" + dest + ",cmd=" + cmd); #endif byte[] body = PBParseManage.getSerialize <T>(instance); CMsg _nnn = new CMsg(); _nnn.dest = dest; _nnn.cmd = cmd; if (body != null && body.Length > 0) { _nnn.body = body; } //放在消息队列里面发 SendPBMsg smsg = new SendPBMsg(); smsg.dest = dest; smsg.cmd = cmd; smsg.sendBody = PBParseManage.getSerialize <CMsg>(_nnn); sendMemory.Add(smsg); }
public void DeserializeData(ReceDataStruct source) { if (source == null) { return; } Stream stream = new MemoryStream(source.data, 0, source.datalen); CMsg cmsg = ProtoBuf.Serializer.Deserialize <CMsg>(stream); int cmd = cmsg.cmd; int dest = cmsg.dest; long serverTime = cmsg.serverTime; byte[] body = cmsg.body; //消息号保存进队列 messageStruct _pb = new messageStruct(); _pb.cmd = cmsg.cmd; _pb.dest = cmsg.cmd; _pb.body = cmsg.body; _pb.serverTime = cmsg.serverTime; //分发协议去解析 PBParseManage.getInstance().ParseData(dest, cmd, body); if (NetWorkManage.getInstance().IsOftenData(dest, cmd)) { NetWorkManage.getInstance().PushReceDataScene(_pb); } else { NetWorkManage.getInstance().PushReceDataQueue(_pb); } Debug.Log("收到消息 dest=" + dest + ",cmd=" + cmd); }