/// <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) { packet = (Packet)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, ReferencePool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0); } 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); }
/// <summary> /// 关闭并清理网络频道辅助器。 /// </summary> public void Shutdown() { GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkConnected, OnNetworkConnected); GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkClosed, OnNetworkClosed); GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkSendPacket, OnNetworkSendPacket); GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkMissHeartBeat, OnNetworkMissHeartBeat); GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkError, OnNetworkError); GameEntry.Event.Unsubscribe(UnityGameFramework.Runtime.EventId.NetworkCustomError, OnNetworkCustomError); m_NetworkChannel = null; m_CachedPacketHeader = null; }