Esempio n. 1
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;
        }
Esempio n. 2
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);
        }