第一范文网 - 专业文章范例文档资料分享平台

SuperSocket学习进展

来源:用户分享 时间:2025/5/19 12:02:46 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

扩展服务器配置

关键字: 扩展配置, 自定义配置, 自定义属性, 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)

搜索更多关于: SuperSocket学习进展 的文档
SuperSocket学习进展.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c0a9r10687u9y6yn8bcxl_5.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top