/// <summary> /// 反序列化消息包。 /// </summary> /// <param name="packetHeader">消息包头。</param> /// <param name="source">要反序列化的来源流。</param> /// <param name="customErrorData">用户自定义错误数据。</param> /// <returns>反序列化后的消息包。</returns> public Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData) { // 注意:此函数并不在主线程调用! customErrorData = null; SCPacketHeader scPacketHeader = packetHeader as SCPacketHeader; if (scPacketHeader == null) { Log.Warning("Packet header is invalid."); return(null); } Packet packet = null; if (scPacketHeader.IsValid) { Type packetType = GetServerToClientPacketType(scPacketHeader.Id); if (packetType != null && source is MemoryStream) { //packet = (Packet)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, ReferencePool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0); object instance = Activator.CreateInstance(packetType); //packet = (Packet)ProtobufHelper.FromStream(packetType, (MemoryStream)source); packet = (Packet)ProtobufHelper.FromStream(instance, (MemoryStream)source); } else { Log.Warning("Can not deserialize packet for packet id '{0}'.", scPacketHeader.Id.ToString()); } } else { Log.Warning("Packet header is invalid."); } ReferencePool.Release(scPacketHeader); return(packet); }