Ejemplo n.º 1
0
		private void UpdatePickMsg()
		{
			if (_channel != null)
			{
				NetReceivePackage package = (NetReceivePackage)_channel.PickMsg();
				if (package != null)
				{
					if (package.ProtoObj != null)
						MonoProtoCallback.Invoke(package);

					if (package.ProtoBodyData != null)
						HotfixProtoCallback.Invoke(package);
				}
			}
		}
        public override void Decode(List <System.Object> msgList)
        {
            // 循环解包
            while (true)
            {
                // 如果数据不够一个SIZE
                if (_receiveBuffer.ReadableBytes() < SIZE_BYTE_COUNT)
                {
                    break;
                }

                _receiveBuffer.MarkReaderIndex();

                // 读取Package长度
                ushort packageSize = _receiveBuffer.ReadUShort();

                // 如果剩余可读数据小于Package长度
                if (_receiveBuffer.ReadableBytes() < packageSize)
                {
                    _receiveBuffer.ResetReaderIndex();
                    break;                     //需要退出读够数据再解包
                }

                // 获取包头信息
                NetReceivePackage package = new NetReceivePackage();
                package.Type = _receiveBuffer.ReadUShort();

                // 如果包体超过最大长度
                int bodySize = packageSize - RECEIVE_PACKAGE_HEAD_SIZE;
                if (bodySize > NetDefine.PackageMaxSize)
                {
                    Channel.HandleError(true, $"The package {package.Type} size is exceeds max size.");
                    break;
                }

                // 获取包体数据
                byte[] bodyData = _receiveBuffer.ReadBytes(bodySize);

                // 正常解包
                try
                {
                    Type classType = NetProtoHandler.TryHandle(package.Type);
                    if (classType != null)
                    {
                        // 非热更协议
                        package.ProtoObj = ProtobufHelper.Decode(classType, bodyData);
                        if (package.ProtoObj != null)
                        {
                            msgList.Add(package);
                        }
                    }
                    else
                    {
                        // 热更协议
                        package.ProtoBodyData = bodyData;
                        msgList.Add(package);
                    }
                }
                catch (Exception ex)
                {
                    // 解包异常后继续解包
                    Channel.HandleError(false, $"The package {package.Type} decode error : {ex.ToString()}");
                }
            }             //while end

            // 注意:将剩余数据移至起始
            _receiveBuffer.DiscardReadBytes();
        }