示例#1
0
        /// <summary>
        /// 为接收网络消息包作准备
        /// </summary>
        public void PrepareForPacket(PacketHeaderBase packetHeader)
        {
            if (packetHeader == null)
            {
                Debug.LogError("网络消息包头为空");
            }

            Reset(packetHeader.PacketLength, packetHeader);
        }
示例#2
0
        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="targetLength">内存流的目标长度</param>
        /// <param name="packetHeader">网络消息包头</param>
        private void Reset(int targetLength, PacketHeaderBase packetHeader)
        {
            if (targetLength < 0)
            {
                Debug.LogError("内存流的目标长度小于0");
            }

            Stream.Position = 0L;
            Stream.SetLength(targetLength);
            PacketHeader = packetHeader;
        }
        /// <summary>
        /// 处理网络消息包头
        /// </summary>
        /// <returns></returns>
        private bool ProcessPacketHeader()
        {
            try
            {
                //反序列化消息包头
                object           customErrorData = null;
                PacketHeaderBase packetHeader    = m_NetworkChannelHelper.DeserializePacketHeader(m_ReceiveState.Stream, out customErrorData);

                if (customErrorData != null && NetworkChannelCustomError != null)
                {
                    NetworkChannelCustomError(this, customErrorData);
                }

                if (packetHeader == null)
                {
                    string errorMessage = "网络消息包头为空";
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.DeserializePacketHeaderError, errorMessage);
                        Debug.LogError(errorMessage);
                        return(false);
                    }
                }

                //为接收网络消息包作准备
                m_ReceiveState.PrepareForPacket(packetHeader);
                if (packetHeader.PacketLength <= 0)
                {
                    ProcessPacket();
                }
            }
            catch (Exception exception)
            {
                m_Active = false;
                if (NetworkChannelError != null)
                {
                    NetworkChannelError(this, NetworkErrorCode.DeserializePacketHeaderError, exception.ToString());
                    return(false);
                }

                throw;
            }

            return(true);
        }
        /// <summary>
        /// 反序列化消息包
        /// </summary>
        /// <param name="packetHeader">消息包头</param>
        /// <param name="source">要反序列化的来源流</param>
        /// <param name="customErrorData">用户自定义错误数据</param>
        public override PacketBase DeserializePacket(PacketHeaderBase packetHeader, Stream source, out object customErrorData)
        {
            // 注意:此方法并不在主线程调用!
            customErrorData = null;

            SCPacketHeader scPacketHeader = packetHeader as SCPacketHeader;

            if (scPacketHeader == null)
            {
                Debug.LogError("要反序列化的消息包的包头不合法");
                return(null);
            }

            PacketBase packet = null;

            if (scPacketHeader.IsValid)
            {
                Type packetType = GetServerToClientPacketType(scPacketHeader.Id);
                if (packetType != null)
                {
                    //反序列化消息包
                    packet = (PacketBase)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, ReferencePool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0);
                }
                else
                {
                    Debug.LogError(string.Format("找不到要反序列化的消息包的Type: {0}", scPacketHeader.Id.ToString()));
                }
            }
            else
            {
                Debug.LogError("要反序列化的消息包不合法");
            }

            ReferencePool.Release(scPacketHeader);
            return(packet);
        }
示例#5
0
 public ReceiveState()
 {
     Stream       = new MemoryStream(DefaultBufferLength);
     PacketHeader = null;
 }
 /// <summary>
 /// 反序列化消息包
 /// </summary>
 /// <param name="packetHeader">消息包头</param>
 /// <param name="source">要反序列化的来源流</param>
 /// <param name="customErrorData">用户自定义错误数据</param>
 /// <returns>反序列化后的消息包</returns>
 public abstract PacketBase DeserializePacket(PacketHeaderBase packetHeader, Stream source, out object customErrorData);