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); } }
public bool Write(uint cmdId , object message , uint serialNumber = 0 , int connID = -1) { bool result = false; IConnection conn = _defaultConn; if (connID != -1) { _connectionDict.TryGetValue(connID, out conn); } ///连接不为空或者尝试可以发送... if (conn != null && conn.TryDoSend(cmdId)) { clearStreams(); _send_head.CmdId = cmdId; _send_head.ZoneId = ZoneId; _send_head.MessageId = _messageId++; if (connectionHook != null && connectionHook.FilterCmd(cmdId, message, _messageId - 1, serialNumber)) { connectionHook.HookMessage(_send_head, message, WriteMSG); return(true); } if (message is byte[]) { byte[] buff = message as byte[]; if (buff != null) { sendMessageBodyStream.Write(buff, 0, buff.Length); } } else { ///序列化message且设置长度 Serializer.NonGeneric.Serialize(sendMessageBodyStream, message); if ((DefineExt.Net_Log_Level & DefineExt.LOG_FULL_SEND_PROTO_INFO) > 0) { Debuger.Log(String.Format("Send ----> cmd={0}, serial={1}, proto={2}", cmdId, serialNumber, KHUtil.GetProtoStr(message, message.GetType().FullName))); } } _send_head.BodyLength = (uint)sendMessageBodyStream.Length; _send_head.Serial = serialNumber; MessageManager msgManager = MessageManager.Instance; // 模拟服务器,从本地读包 if (!msgManager.IsActivate || msgManager.IsSerializeToLocal) { result = conn.DoSend(_send_head, message, sendMessageBodyStream); } if (!result) { _messageId--; } } return(result); }