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启动才能使用连接过滤吗?
相关文章
- 终端下将 man 命令的结果输出到文件保存
- 第十九章 SQL命令 CREATE TABLE(六)
- 第三十二章 Caché 命令大全 WRITE 命令
- 第二十二章 Caché 命令大全 RETURN 命令
- 新手指南: Linux 新手应该知道的 26 个命令
- Linux - grep命令详解
- Redis 常用监控信息命令总结
- linux中的strip命令简介------给文件脱衣服
- windows下使用makecert命令生成自签名证书
- SuperSocket命令程序集定义
- git diff 命令详解
- awk命令简单学习
- GCC编译流程及常用编辑命令
- (linux shell)第二章--命令之乐(一)
- 【Linux】linux经常使用基本命令
- Linux学习笔记(4)磁盘分区(fdisk)、挂载与文件系统命令
- Proxmark3命令帮助