/// <summary> /// 立即刷新服务器输出流数据 /// </summary> public virtual void Flush(bool hasError = false) { HasError = hasError; int respondContentType = (int)ReqContentType; byte[] respondData = Respond.ProtoBufSerialize <RespondBase>(); int respondDataLength = respondData.Length; string respondHeaderSig = string.Format("{0}{1}{2}", respondContentType, respondDataLength + 4, ReqChannel.GetChannelKey()).CreateMD5EncryptShort(); Context.Response.Headers["CT"] = respondContentType.ToString(); Context.Response.Headers["BL"] = (respondDataLength + 4).ToString(); Context.Response.Headers["HS"] = respondHeaderSig; uint adlChkNum = (uint)respondData.Adler32CheckSum(); Context.Response.BinaryWrite(BitConverter.GetBytes(adlChkNum).Reverse()); Context.Response.BinaryWrite(respondData); Context.Response.End(); //打印日志 if (Compiled.Debug) { Respond.Debug(string.Format("=== {0} 下行结构 ===", Command)); } }
/// <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); }