Ejemplo n.º 1
0
        /// <summary>
        /// 解析并构建请求数据
        /// </summary>
        /// <returns></returns>
        public virtual bool Build()
        {
            NameValueCollection nvCollection = this.Context.Request.Headers;

            ReqChannel       = nvCollection["CN"] ?? string.Empty;
            ReqContentType   = (ContentType)(nvCollection["CT"] ?? "0").Parse <int>();
            ReqBodyLength    = (nvCollection["BL"] ?? "0").Parse <int>();
            ReqHeaderSig     = nvCollection["HS"] ?? string.Empty;
            Msid             = nvCollection["x-up-calling-line-id"] ?? string.Empty; //通过WAP代理网关获取手机号,基本已失效
            ReqContentLength = this.Context.Request.ContentLength;

            if (Compiled.Debug)
            {
                string.Format("CN:{0} | CT:{1} | BL:{2} | HS:{3} | CL:{4}", ReqChannel, ReqContentType, ReqBodyLength, ReqHeaderSig, ReqContentLength).Debug("=== 请求上行HEADER ===");
            }

            //基本判断
            if (ReqBodyLength != ReqContentLength || ReqContentLength < 4 || !ReqChannel.ChannelCodeExists() || ReqHeaderSig.Length != 16)
            {
                new Exception("=== 请求上行HEADER基本信息错误 ===").Error();
                return(false);
            }

            //HEADER层数据校验码确认
            if (!string.Format("{0}{1}{2}{3}", ReqChannel, (int)ReqContentType, ReqBodyLength, ReqChannel.GetChannelKey()).CreateMD5EncryptShort().Equals(ReqHeaderSig))
            {
                new Exception("=== 请求上行HEADER:[HS]校验码错误 ===").Error();
                return(false);
            }

            //BODY层数据解析及校验
            Stream inputStream = this.Context.Request.InputStream;

            if ((int)inputStream.Length != ReqBodyLength)
            {
                new Exception("=== 请求上行数据长度验码错误 ===").Error();
                return(false);
            }

            BinaryReader  reader        = new BinaryReader(inputStream);
            StreamContext streamContext = new StreamContext(reader, Encoding.UTF8);
            uint          adlerSum      = streamContext.ReadUInt32();

            byte[] buffer = streamContext.ReadBytes(ReqContentLength - 4);
            streamContext.Dispose();
            reader.Close();
            reader.Dispose();
            inputStream.Close();
            inputStream.Dispose();

            uint adlChkNum = (uint)buffer.Adler32CheckSum();

            //BODY数据校验
            if (adlChkNum != adlerSum)
            {
                new Exception("=== 请求上行BODY数据验码错误 ===").Error();
                return(false);
            }

            //请求上行基类
            RequestBase reqBase = buffer.ProtoBufDeserialize <RequestBase>();

            if (Compiled.Debug)
            {
                reqBase.Debug("=== 请求上行基类 : RequestBase ===");
            }

            UserId    = reqBase.UserId;
            DeviceId  = reqBase.DeviceId;
            Command   = reqBase.Command ?? string.Empty;
            CmdData   = reqBase.Data ?? new byte[0];
            SessionId = reqBase.SessionId ?? string.Empty;
            Respond   = new RespondBase {
                Command = Command, Status = Status.Succeed, Code = RespondCode.Success, Attach = reqBase.Attach, Data = null
            };

            //命令号不存在
            if (!Command.CmdExists())
            {
                Flush(RespondCode.CmdInvalid);
                return(false);
            }

            //当前命令必须设置DeviceId
            if (!Command.CmdIsExcepted(1) && DeviceId < 100000)
            {
                Flush(RespondCode.DeviceInvalid);
                return(false);
            }

            //登录SESSIONID
            if (!this.Command.CmdIsExcepted() && !SessionId.SessionIsValid(UserId, ExpiredConfigs.GetSessionExpired()))
            {
                Flush(RespondCode.SessionInvalid);
                return(false);
            }

            return(true);
        }