zoukankan      html  css  js  c++  java
  • supersockets命令过滤器

    关键字: 命令过滤器, 命令, 过滤器, OnCommandExecuting, OnCommandExecuted

    SuperSocket 中的命令过滤器看起来有些像 ASP.NET MVC 中的 Action Filter,你可以用它来做命令执行的拦截,命令过滤器会在命令执行前和执行后被调用。

    命令过滤器必须继承于 Attribute 类 CommandFilterAttribute:

     /// <summary>

    /// Command filter attribute

    /// </summary>

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]

    public abstract class CommandFilterAttribute : Attribute

    {

        /// <summary>

        /// Gets or sets the execution order.

        /// </summary>

        /// <value>

        /// The order.

        /// </value>

        public int Order { get; set; }

        /// <summary>

        /// Called when [command executing].

        /// </summary>

        /// <param name="commandContext">The command context.</param>

        public abstract void OnCommandExecuting(CommandExecutingContext commandContext);

        /// <summary>

        /// Called when [command executed].

        /// </summary>

        /// <param name="commandContext">The command context.</param>

        public abstract void OnCommandExecuted(CommandExecutingContext commandContext);

    }

    你需要为你的命令过滤器实现下面两个方法:

    OnCommandExecuting: 此方法将在命令执行前被调用;

    OnCommandExecuted: 此方法将在命令执行后被调用;

    Order: 此属性用于设置多个命令过滤器的执行顺序;

    下面的代码定义了一个命令过滤器 LogTimeCommandFilterAttribute 用于记录执行时间超过5秒钟的命令:

    public class LogTimeCommandFilter : CommandFilterAttribute

    {

        public override void OnCommandExecuting(CommandExecutingContext commandContext)

        {

            commandContext.Session.Items["StartTime"] = DateTime.Now;

        }

        public override void OnCommandExecuted(CommandExecutingContext commandContext)

        {

            var session = commandContext.Session;

            var startTime = session.Items.GetValue<DateTime>("StartTime");

            var ts = DateTime.Now.Subtract(startTime);

            if (ts.TotalSeconds > 5 && session.Logger.IsInfoEnabled)

            {

                session.Logger.InfoFormat("A command '{0}' took {1} seconds!", commandContext.CurrentCommand.Name, ts.ToString());

            }

        }

    }

    然后通过增加属性的方法给命令 "QUERY" 应用此过滤器:

    [LogTimeCommandFilter]

    public class QUERY : StringCommandBase<TestSession>

    {

        public override void ExecuteCommand(TestSession session, StringCommandInfo commandData)

        {

            //Your code

        }

    }

    如果你想应用次过滤器给所有的命令, 你可以向下面的代码这样将此命令过滤器的属性加到你的 AppServer 类上面去:

    [LogTimeCommandFilter]

    public class TestServer : AppServer<TestSession>

    {

    }

    你可以通过将 commandContext's 的 Cancel 属性设为 True 来取消当前命令的执行:

    public class LoggedInValidationFilter : CommandFilterAttribute

    {

        public override void OnCommandExecuting(CommandExecutingContext commandContext)

        {

            var session = commandContext.Session as MyAppSession;

            //If the session is not logged in, cancel the executing of the command

            if (!session.IsLoggedIn)

                commandContext.Cancel = true;

        }

        public override void OnCommandExecuted(CommandExecutingContext commandContext)

        {

        }

    }

    ServerConfig配置内没有对ipRange的配置,只有ConnectionFilter属性,但完全不起作用,难道只能使用BootstrapFactory启动才能使用连接过滤吗?

  • 相关阅读:
    05-Selenium的三种等待
    04-selenium 八大元素定位方法
    03-Selenium简单操作
    python-利用json模块处理json数据几个函数总结
    python-利用pymysql获取数据简单使用总结
    python-利用faker模块生成测试数据
    python-利用random模块生成测试数据封装方法总结
    python-利用shutil模块rmtree方法可以将文件及其文件夹下的内容删除
    pytest--配置用例执行顺序(pytest_ordering插件介绍)
    pytest--mark基本使用(主要通过pytest.ini文件注册标签名,对用例进行标记分组)
  • 原文地址:https://www.cnblogs.com/fanweisheng/p/11126958.html
Copyright © 2011-2022 走看看