Esempio n. 1
0
        public HttpResponse(HttpContext context)
        {
            if (context == null)
                throw new Exception("HttpContext cannot be null.");

            Context = context;
        }
Esempio n. 2
0
        public override void HandleRequest(ClientManager clmngr, HttpContext context)
        {
            if (clmngr == null || context == null || context.Request == null)
                return;

            HttpResponse response = context.Response;

            string result = "{\"Title\":\"Message\", \"Content\":\"Welcome!\"}";
            response.Write(result);
        }
Esempio n. 3
0
        public override void HandleRequest(ClientManager clmngr, HttpContext context)
        {
            if (clmngr == null || context == null || context.Request == null)
                return;

            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            int userId = 0;
            if (request.Parameters != null && request.Parameters.AllKeys.Contains("UserID"))
            {
                int.TryParse(request.Parameters["UserID"], out userId);
            }
            if (userId <= 0)
                throw new BussinessException("请输入用户ID。");
            string key = Guid.NewGuid().ToString();

            string result = "{\"UserID\":\"" + userId + "\", \"Key\":\"" + key + "\"}";
            response.Write(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 将本次KeepAlive的请求登记到处理队列中
        /// </summary>
        /// <param name="clmngr"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private HttpContext RegistKeepAliveRequestContext(ClientManager clmngr, HttpRequest request)
        {
            HttpContext context = null;

            if (clmngr == null || !clmngr.IsAuthedUser || !this._Clients.ContainsKey(clmngr.nSessionID) || request == null || request.IsRequestError)
                return context;

            clmngr.IsKeepAlive = true;

            //Context Table使用User ID作为Key
            int uid = clmngr.nUID;

            lock (_ContextTable.SyncRoot)
            {
                if (_ContextTable.ContainsKey(uid))
                {
                    context = _ContextTable[uid] as HttpContext;
                    //将请求添加到队列
                    context.AppendRequest(request);
                }
                else
                {
                    context = new HttpContext(this, clmngr, request);
                    //将请求添加到队列
                    _ContextTable[uid] = context;
                }
            }

            return context;
        }
Esempio n. 5
0
        private void HandleErrorRequest(ClientManager clmngr, HttpRequest request)
        {
            if (clmngr == null || request == null)
                return;

            HttpContext context = new HttpContext(this, clmngr, request);
            HttpResponse response = context.Response;
            response.Write(request.Error.Message);
        }
Esempio n. 6
0
        /// <summary>
        /// 处理用户请求
        /// </summary>
        /// <param name="clmngr"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public override bool HandleRequest(ClientManager clmngr, ISocketMessage message)
        {
            bool goRecieving = false;
            HttpContext context = null;
            try
            {
                _RequestTimes++;
                HttpRequest request = HttpRequest.ParseFromRawMessage(this, message.ToString());

                if (!request.IsRequestError)
                {
                    //检查主机头是否符合设置
                    if (!CheckRequestHost(request))
                    {
                        //shutdown client
                        ShutdownClient(clmngr);
                        return goRecieving;
                    }
                    //获取该request的handlers
                    CometCommandHandlerPipeline handlePipeline = GetRegistedCommandHandlers(request);

                    if (handlePipeline == null)
                    {
                        //shutdown client
                        ShutdownClient(clmngr);
                        return goRecieving;
                    }

                    context = new HttpContext(this, clmngr, request);

                    CometCommand cometCmd = handlePipeline.Command;
                    request.Command = cometCmd;

                    if (!clmngr.IsAuthedUser)
                    {
                        RegistUser(clmngr, request);
                    }
                    //如果要求长连接
                    if (cometCmd.RequireKeepAlive)
                    {
                        //登记本次请求事务
                        if (RegistKeepAliveRequestContext(clmngr, request) == null)
                        {
                            //shutdown client
                            ShutdownClient(clmngr);
                            return goRecieving;
                        }
                    }
                    //执行处理管道
                    if (handlePipeline.Count > 0)
                    {
                        foreach (Handler.IHttpHandler handler in handlePipeline.Handlers)
                        {
                            if (handler != null)
                            {
                                handler.HandleRequest(clmngr, context);
                            }
                        }
                    }
                }
                else
                {
                    HandleErrorRequest(clmngr, request);
                }
            }
            catch (Exception ex)
            {
                if (ex is BussinessException)
                {
                    ExceptionResult result = new ExceptionResult()
                    {
                        ErrorCode = "B10001",
                        ErrorMessage = ex.Message
                    };
                    context.Response.Write(Common.Utility.Json.JsonHelper.ObjToJson(result));
                }
                else
                {
                    ShutdownClient(clmngr);
                    Console.WriteLine(string.Format("Error occured when HandleRequest:{0} ", ex.Message));
                }
            }
            finally { }
            return goRecieving;
        }