예제 #1
0
        /// <summary>
        /// 获取用户PUSH通道
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static PushPipe GetPipes(this int userId)
        {
            string cacheKey = string.Format("PIPE_{0}", userId);
            object userPipe = PipeCacheName.GetCache(cacheKey);

            if (userPipe == null)
            {
                return(null);
            }

            Tuple <PushPipe, DateTime> pipeTuple = userPipe as Tuple <PushPipe, DateTime>;

            if (DateTime.Now.Subtract(pipeTuple.Item2).TotalSeconds <= ExpiredConfigs.GetPipeExpired())
            {
                return(pipeTuple.Item1);
            }

            userId.RemovePipes();
            return(null);
        }
예제 #2
0
        /// <summary>
        /// 输出登录结果
        /// </summary>
        /// <param name="context">当前请求上下文应答实例</param>
        /// <param name="uVaildInfo">用户验证信息</param>
        internal static void FlushResult(DataContext context, UserVaildInfo uVaildInfo)
        {
            UserCacheInfo userCacheInfo = UserBiz.ReadUserCacheInfoFromDb(uVaildInfo.UserId);
            AuthResult    authResult    = new AuthResult
            {
                UserId        = uVaildInfo.UserId,
                DeviceChanged = context.DeviceId == uVaildInfo.LastLoginDeviceId,
                SessionId     = uVaildInfo.UserId.GetSessionId(),
                UserBase      = userCacheInfo.ToUserBase()
            };

            //缓存用户会话状态
            authResult.SessionId.SetSession(uVaildInfo.UserId, ExpiredConfigs.GetSessionExpired());

            //缓存用户信息
            UserBiz.SetUserCacheInfo(uVaildInfo.UserId, userCacheInfo);

            //用户登录事件
            SignInEvents(authResult, uVaildInfo);

            //输出请求响应
            context.Flush <AuthResult>(authResult);
        }
예제 #3
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);
        }