Exemplo n.º 1
0
        public HttpHandlerBase(HttpServer server, CometCommand command)
        {
            if (server == null)
            {
                throw new Exception("HttpServer cannot be null.");
            }

            if (command == null)
            {
                throw new Exception("CometCommand cannot be null.");
            }

            Server  = server;
            Command = command;
        }
        /// <summary>
        /// 初始化服务器支持的协议
        /// </summary>
        /// <param name="commandList"></param>
        public void InitCommand(List <string> commandList)
        {
            string commandJson = string.Empty;

            ComandList = new List <CometCommand>(commandList.Count);
            for (int i = 0; i < commandList.Count; i++)
            {
                commandJson = commandList[i];
                try
                {
                    CometCommand command = JsonHelper.JsonToObj <CometCommand>(commandJson);
                    if (command != null)
                    {
                        ComandList.Add(command);
                    }
                }
                catch { }
            }
        }
Exemplo n.º 3
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;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 根据命令实例化管道处理命令类
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private IHttpHandler ParseCometCommand(CometCommand cmd)
        {
            string currAssemblyName = Process.GetCurrentProcess().MainModule.FileName;

            currAssemblyName = currAssemblyName.Substring(currAssemblyName.LastIndexOf("\\") + 1).Replace(".vshost", "");
            Assembly assembly = Assembly.LoadFrom(currAssemblyName);

            Type[] assemblyAllTypes = assembly.GetTypes();
            Type   currCommand      = null;

            foreach (Type type in assemblyAllTypes)
            {
                if (type.Name == cmd.ToString())
                {
                    currCommand = type;
                    break;
                }
            }
            IHttpHandler commandHandler = Activator.CreateInstance(currCommand) as IHttpHandler;

            return(commandHandler);
        }
Exemplo n.º 5
0
 public ValidationUserKey(HttpServer server, CometCommand command) : base(server, command)
 {
 }
Exemplo n.º 6
0
 public Default(HttpServer server, CometCommand command) : base(server, command)
 {
 }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 找到与Request匹配的处理管道
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private CometCommandHandlerPipeline GetRegistedCommandHandlers(HttpRequest request)
        {
            CometCommand command = CometCommand.ParseFromRequest(request);

            return(GetRegistedCommandHandlers(command.CommandID));
        }