关键字: 命令过滤器, 命令, 过滤器, 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启动才能使用连接过滤吗?

supersockets命令过滤器的更多相关文章

  1. supersocket为动态命令增加命令过滤器

    由于我们无法像 C# 中一样方便的将 CLR 属性添加到 Python 文件或者函数中,因此我们需要定义一个函数 "getFilters()" 用于将命令过滤器方会给 CLR 运行 ...

  2. supersockets接收过滤器(ReceiveFilter)

    接收过滤器(ReceiveFilter)用于将接收到的二进制数据转化成请求实例(RequestInfo). 实现一个接收过滤器(ReceiveFilter), 你需要实现接口 IReceiveFilt ...

  3. grep 命令操作

    linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...

  4. 由一条Linux的grep命令说起

    今天在开发的时候,看到同事使用了这样的一条linux命令 grep 'class YourClass' -rwi * |grep -v svn 想到了 grep命令的,几个参数. -r 明确要求搜索子 ...

  5. 图解“管道过滤器模式”应用实例:SOD框架的命令执行管道

    管道和过滤器 管道和过滤器是八种体系结构模式之一,这八种体系结构模式是:层.管道和过滤器.黑板.代理者.模型-视图-控制器(MVC) 表示-抽象-控制(PAC).微核.映像. 管道和过滤器适用于需要渐 ...

  6. 强大的grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  7. Grep 命令 用法大全

    查找x文件 find / -name "x*" -ls 查找文件中x所在的行数 grep -n "x" -r *find . -name "*.jav ...

  8. linux grep命令总结

    风生水起善战者,求之于势,不责于人,故能择人而任势. 博客园    首页    新随笔    联系    订阅    管理 posts - 791,  comments - 394,  trackba ...

  9. Linux Shell编程(23)——文本处理命令

    处理文本和文本文件的命令sort文件排序, 通常用在管道中当过滤器来使用. 这个命令可以依据指定的关键字或指定的字符位置, 对文件行进行排序. 使用 -m 选项, 它将会合并预排序的输入文件. 想了解 ...

随机推荐

  1. web前端学习(二)html学习笔记部分(4)--audio和video文件播放

    1.2.10  html5音频 1.2.10.1  HTML5音频播放 本课主要讲解HTML5播放音频 <!--<button onclick="clickA"> ...

  2. SAS信用评分之模型拟合以及验证的大坑

    SAS信用评分之模型拟合以及验证的大坑 今天的内容是来讲我这段时间被模型拟合和模型验证坑过的那些事.我也是千辛万苦终于是把模型给建出来了.此处应该有掌声.因为模型老是效果不好这件事,我躲在被窝里哭了好 ...

  3. Python 字符编码处理总结

    Python中经常遇到这样那样的字符编码问题,尤其在处理网页源码时(特别是爬虫中): UnicodeDecodeError: 'XXX' codec can't decode bytes in pos ...

  4. vs2015卸载、vs2008安装Visual Assist x

    卸载2015 参考博文 1. 手动卸载VS2015的主要部分: win10系统: 控制面板---程序和功能--Microsoft Visual Studio 2015---更改卸载 2. 下载工具并解 ...

  5. Sublime Text3安装教程,配置教程,常用插件安装等方法

    前言: sublimeText3的特点: 1.Sublime Text 是一款跨平台代码编辑器,在Linux.OS X和Windows下均可使用. 2.Sublime Text 是可扩展的,并包含大量 ...

  6. golang中包的初始化

    1.当一个go源程序被初始化时,首先去初始化所依赖的其他包,然后初始化该go源码文件的全局变量的初始化和执行初始化函数,其中该包所有的全局变量初始化在前,该包的初始化函数int在后.当所有包的初始化函 ...

  7. PHPCMS快速建站系列之邮箱验证

    1. 登录163邮箱,->设置,开启POP3服务->把SMTP服务器地址复制到PHPCMS后台. 2.开启客户端授权密码 3.填写相关信息,.可以在测试邮箱填入邮箱地址测试

  8. 邀您共赴数据库学术顶会ICDE 2019——阿里云专场 零距离接触达摩院数据库“最强大脑”

    摘要: 当学术大家遇到技术大拿,会碰撞出怎样的火花?为进一步加深产学研学术交流,阿里云将于ICDE 2019大会期间(4月9日)举办以“云时代的数据库”为主题的技术专场(Workshop) 作为全球数 ...

  9. PHP验证码文件类

    转自:http://www.blhere.com/1165.html 12345678910111213141516171819202122232425262728293031323334353637 ...

  10. 序列化类型为“System.Data.Entity.DynamicProxies..."对象时检测到循环引用

    这是因为EF外键引起的序列化问题. 解决方案: context.Configuration.ProxyCreationEnabled = false; 这里我用的是一个基类控制器用于被继承 返回EF实 ...