/// <summary> /// 发送协议且侦听回调 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmdId"></param> /// <param name="message"></param> /// <param name="callback"></param> /// <param name="displayTip"></param> /// <param name="timeoutCallback"></param> /// <param name="timeout"></param> /// <param name="connID"></param> /// <returns></returns> public bool Send <T>(uint cmdId , T message , NetworkMessageHandle callback , bool displayTip = false , TimerEntity.TimerEntityCallBack timeoutCallback = null , double timeout = 10 , int connID = -1 , string tag = "None") where T : IExtensible { #if UNITY_EDITOR && KHNET_DEBUG if (typeProvider_D.ContainsKey(cmdId)) { if (callback != null) { object mockresp = getRespByType(typeProvider_D[cmdId]); if (mockresp != null) { callback(mockresp); return(false); //模拟回包 } else { UnityEngine.Debuger.LogError("数据实体未定义"); } } } #endif return(SendMessage(cmdId, message, callback, displayTip, timeoutCallback, timeout, connID, tag)); }
protected List <object> ReadBody(ClientHead head, MemoryStream stream) { ///此处对读取 BODYSTEAM做了一些优化,如果有问题请注意排查 2015 08 21 //byte[] bodyBuffer = new byte[head.BodyLength]; //int bodyBufferOffset = 0; //stream.Read(bodyBuffer, 0, (int)head.BodyLength); byte[] bodyBuffer = stream.GetBuffer(); int bodyBufferOffset = (int)stream.Position; int bodyLen = (int)head.BodyLength; if (bodyLen < 0) { bodyLen = 0; } stream.Position = bodyBufferOffset + bodyLen; ///bodylen大于零才去读MESSAGE //if (bodyLen > 0) //去掉吧,有些协议的长度就是为零 { List <object> msgs = new List <object>(); if (head.Format == ClientHeadFormatType.Protobuf) { bool isLua = false; bool isCSharp = false; if (__Proxy.__LuaPBProcessor.ContainsKey(head.CmdId) && _router.getPBType(head.Serial) == PBTYPE.LuaPB || _router.getPBType(head.Serial) == PBTYPE.Both) { byte[] buff = new byte[bodyLen]; //stream.Write(buff, bodyBufferOffset, bodyLen); stream.Position = bodyBufferOffset; stream.Read(buff, 0, bodyLen); stream.Position = bodyBufferOffset + bodyLen; msgs.Add(buff); //return buff; isLua = true; } if (_typeProvider.ContainsKey(head.CmdId) && _router.getPBType(head.Serial) == PBTYPE.CSharpPB || _router.getPBType(head.Serial) == PBTYPE.Both) { ReceiveMessageStream.Write(bodyBuffer, bodyBufferOffset, bodyLen); ReceiveMessageStream.Position = 0; Type messageType; TypeProvider.TryGetValue(head.CmdId, out messageType); if (DefineExt.Net_Log_Level > 0) { Debuger.Log(string.Format("Received Package 0x{0}({4}), Body Length = {1}, Message Id = {2}, Serial Id = {3}.", _recentHead.CmdId.ToString("X2"), _recentHead.BodyLength.ToString(), _recentHead.MessageId, _recentHead.Serial, _recentHead.CmdId)); ///解析详细的字段 ///todo.. } if (messageType == null) { ReceiveMessageStream.SetLength(0); if (_router.getPBType(head.Serial) == PBTYPE.CSharpPB) { Debuger.LogWarning(@"返回cmdID消息类型未注册。请在NetworkManager.registerTypes()方法中添加cmdID到返回消息类型的映射。 CmdId = 0x" + _recentHead.CmdId.ToString("X2") + "(" + _recentHead.CmdId + ")"); //return null; } } else { object message = null; message = Serializer.NonGeneric.Deserialize(messageType, ReceiveMessageStream); ReceiveMessageStream.SetLength(0); if ((DefineExt.Net_Log_Level & DefineExt.LOG_FULL_RECV_PROTO_INFO) > 0) { Debuger.Log(String.Format("Receive ----> cmd={0}, serial={1}, proto={2}", head.CmdId, head.Serial, KHUtil.GetProtoStr(message, messageType.FullName))); } msgs.Add(message); isCSharp = true; } } // Update by Chicheng if (msgManager.IsActivate && msgManager.IsSerializeToLocal) { Type messageType; TypeProvider.TryGetValue(head.CmdId, out messageType); // Debug.LogWarning("把读到的消息包序列化到本地"); RemoteModel remoteModel = RemoteModel.Instance; ulong timeStamp = remoteModel.CurrentTime; if (isCSharp && isLua) { serializeToLocalWithType(msgs, messageType, head.CmdId, timeStamp, head.Serial, MessageSource.CSharpAndLua); } else if (isLua) { serializeToLocalWithType(msgs, null, head.CmdId, timeStamp, head.Serial, MessageSource.Lua); } else if (isCSharp) { if (messageType != null) { serializeToLocalWithType(msgs, messageType, head.CmdId, 0, head.Serial, MessageSource.CSharp); } else { Debug.LogWarning("丢弃" + head.Serial); } } } } return(msgs); } }