Beispiel #1
0
        /// <summary>
        /// 从Request中解析command
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private CometCommand GetCommandFromRequest(HttpRequest request)
        {
            CometCommandHandlerPipeline handlers = GetRegistedCommandHandlers(request);

            if (handlers != null)
            {
                return(handlers.Command);
            }
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// 注册Handler
        /// </summary>
        /// <param name="command"></param>
        /// <param name="cmdHandler"></param>
        public void RegistCommandHandler(CometCommand command, Handler.IHttpHandler cmdHandler)
        {
            if (command == null || cmdHandler == null || _CometCommands == null)
            {
                return;
            }

            CometCommandHandlerPipeline handlers = null;

            if (_CometCommands.ContainsKey(command.CommandID))
            {
                handlers = _CometCommands[command.CommandID];
                handlers.RegistHandler(cmdHandler);
            }
            else
            {
                handlers = new CometCommandHandlerPipeline(this, command);
                handlers.RegistHandler(cmdHandler);
                _CometCommands[command.CommandID] = handlers;
            }
        }
Beispiel #3
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);
        }