SuperSocket服务器架设(五):使用命令过滤器

来源:互联网 发布:一淘和淘宝客返利共存 编辑:程序博客网 时间:2024/05/07 13:08

效果:禁止未被授权的连接执行某些命令,这里使用验证登陆为例。

 

1.      在MySession中添加bool类型的变量isLogin:

 

2.      创建类MyCommandFilterAttribute,继承自CommandFilterAttribute,并重写方法:

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.Common;  
  6. using SuperSocket.SocketBase;  
  7. using SuperSocket.SocketBase.Protocol;  
  8.   
  9. namespace SuperSocketApp1  
  10. {  
  11.     public class MyCommandFilterAttribute : CommandFilterAttribute  
  12.     {  
  13.         /// <summary>  
  14.         /// 命令监视器-执行命令前调用  
  15.         /// </summary>  
  16.         /// <param name="commandContext"></param>  
  17.         public override void OnCommandExecuting(CommandExecutingContext commandContext)  
  18.         {  
  19.             //throw new NotImplementedException();  
  20.   
  21.             MySession session = commandContext.Session as MySession;  
  22.   
  23.             //判断是否已登录  
  24.             if (session != null && !session.isLogin)  
  25.             {  
  26.                 //判断当前命令是否为LOGIN  
  27.                 if (!commandContext.RequestInfo.Key.Equals("LOGIN"))  
  28.                 {  
  29.                     //取消执行当前命令  
  30.                     commandContext.Cancel = true;  
  31.                 }  
  32.             }  
  33.         }  
  34.   
  35.         /// <summary>  
  36.         /// 命令监视器-执行命令后调用  
  37.         /// </summary>  
  38.         /// <param name="commandContext"></param>  
  39.         public override void OnCommandExecuted(CommandExecutingContext commandContext)  
  40.         {  
  41.             //throw new NotImplementedException();  
  42.         }  
  43.    }  
  44. }

3.      创建类LOGIN,模仿登陆过程:

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.SocketBase;  
  6. using SuperSocket.SocketBase.Command;  
  7. using SuperSocket.SocketBase.Protocol;  
  8.   
  9. namespace SuperSocketApp1.Command  
  10. {  
  11.     class LOGIN : CommandBase<MySession, StringRequestInfo>  
  12.     {  
  13.         public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)  
  14.         {  
  15.             session.isLogin = true;  
  16.             session.Send("登陆成功。");  
  17.         }  
  18.     }  
  19. }  

4.      给要应用此过滤器的类添加属性

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. [MyCommandFilterAttribute]  
  2. public class SEND : CommandBase<MySession, StringRequestInfo>  
  3. {  
  4.     public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)  
  5.     {  
  6.        //此处省略N行代码  
  7.     }  
  8. }  

5.      若要给所有的命令类应用此过滤器,则给MyServer添加属性

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. [MyCommandFilter]  
  2. public class MyServer : AppServer<MySession>  
  3. {  
  4.       
  5. }  

0 0