SuperSocket学习进展 下载本文

扩展服务器配置

关键字: 扩展配置, 自定义配置, 自定义属性, GetChildConfig, 读取配置,子节点 当你使用 SuperSocket 实现 Socket 服务器的时候,不可避免的需要在配置文件中定义一些参数。 SuperSocket 提供了非常简单的方法,让你在配置文件中定义这些参数,然后在你的代码中读取它们。

请看下面的配置代码:

serverType=\SuperSocket.Facility\ ip=\ receiveBufferSize=\ maxConnectionNumber=\ clearIdleSession=\ policyFile=\

在上面的配置中, 属性 \未在 SuperSocket 中定义, 不过你任然可以在你的 AppServer 类中读取它:

public class YourAppServer : AppServer {

private string m_PolicyFile;

protected override bool Setup(IRootConfig rootConfig, IServerConfig config) {

m_PolicyFile = config.Options.GetValue(\

if (string.IsNullOrEmpty(m_PolicyFile)) {

if(Logger.IsErrorEnabled)

Logger.Error(\ return false; }

return true; } }

你不仅可以在 server 节点定义属性, 而且你还能像下面的代码那样定义子节点:

serverTypeName=\ ip=\

下面是所需的节点对应的配置类: ///

/// SubProtocol configuration ///

public class SubProtocolConfig : ConfigurationElement {

//Configuration attributes }

///

/// SubProtocol configuation collection ///

[ConfigurationCollection(typeof(SubProtocolConfig))]

public class SubProtocolConfigCollection : ConfigurationElementCollection {

//Configuration attributes }

然后你就能在 AppServer 类中读取这个子节点了: public class YourAppServer : AppServer {

private SubProtocolConfigCollection m_SubProtocols;

protected override bool Setup(IRootConfig rootConfig, IServerConfig config) {

m_SubProtocols =

config.GetChildConfig(\

if (m_SubProtocols == null) {

if(Logger.IsErrorEnabled)

Logger.Error(\required!\

return false; }

return true; } }

服务器配置热更新

Keywords: 配置,热更新

此功能能够允许你在不重启服务器的前提下更新服务器实例的配置。 (仅限1.6.5及其以上版本)

支持热更新的服务器实例配置选项

SuperSocket 支持以下配置选项的热更新: * logCommand

* idleSessionTimeOut * maxRequestLength * logBasicSessionActivity * logAllSocketException

SuperSocket 支持所有自定义配置属性和自定义配置子节点的热更新。 下面的代码将演示如何让你的自定义配置支持热更新: public class PushServer : AppServer {

private int m_Interval;

protected override bool Setup(IRootConfig rootConfig, IServerConfig config) {

RegisterConfigHandler(config, \ {

// the code in this scope will be executed automatically // after the configuration attribute \

var interval = 0;

int.TryParse(value, out interval);

if (interval <= 0)

interval = 60;// 60 seconds by default

m_Interval = interval * 1000; return true; });

return true; }

/// Other code }

你可以在QuickStart中的 PushServer 项目中找到此更能的完整示例代码。

命令过滤器

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

SuperSocket 中的命令过滤器看起来有些像 ASP.NET MVC 中的 Action Filter,你可以用它来做命令执行的拦截,命令过滤器会在命令执行前和执行后被调用。 命令过滤器必须继承于 Attribute 类 CommandFilterAttribute: ///

/// Command filter attribute ///

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public abstract class CommandFilterAttribute : Attribute {

///

/// Gets or sets the execution order. ///

/// /// The order. ///

public int Order { get; set; } ///

/// Called when [command executing]. ///

///

public abstract void OnCommandExecuting(CommandExecutingContext commandContext); ///

/// Called when [command executed]. ///

///

public abstract void OnCommandExecuted(CommandExecutingContext commandContext); }

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

OnCommandExecuting: 此方法将在命令执行前被调用; OnCommandExecuted: 此方法将在命令执行后被调用; Order: 此属性用于设置多个命令过滤器的执行顺序;

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

public class LogTimeCommandFilter : CommandFilterAttribute {

public override void OnCommandExecuting(CommandExecutingContext commandContext) {

commandContext.Session.Items[\ }

public override void OnCommandExecuted(CommandExecutingContext commandContext)