Fork me on GitHub

SuperSocket 1.5 Documentation译文 10 ----- 扩展服务端配置

当你通过SuperSocket实现你的socket服务端, 不可避免的要在配置文件中定义一些参数.SuperSocket提供了一个非常简单的方法来存储您的配置文件中的参数然后读取并在AppServer中使用他们.

请看看下面的配置代码:

<server name="FlashPolicyServer"
        serverType="SuperSocket.Facility.PolicyServer.FlashPolicyServer, SuperSocket.Facility"
        ip="Any" port="843"
        receiveBufferSize="32"
        maxConnectionNumber="100"
        clearIdleSession="true"
        policyFile="Policy\flash.xml">
</server>

在上面的服务器配置,在SuperSocket中没有定义属性“policyFile”, 但你也可以阅读它在你的应用服务器类:

public class YourAppServer : AppServer
{
    private string m_PolicyFile;

    protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
    {  
        m_PolicyFile = config.Options.GetValue("policyFile");

        if (string.IsNullOrEmpty(m_PolicyFile))
        {
            if(Logger.IsErrorEnabled)
                Logger.Error("Configuration option policyFile is required!");
            return false;
        }

        return true;
    }
}

我们不仅可以添加自定义属性,在服务器节点,我们还可以象下面这样配置添加自定义的子节点:

<server name="SuperWebSocket"
        serverTypeName="SuperWebSocket"
        ip="Any" port="2011" mode="Tcp">
    <subProtocols>
        <!--你的配置信息-->
    </subProtocols>
</server>

配置元素类型是必需的:

/// <summary>
/// SubProtocol configuration
/// </summary>
public class SubProtocolConfig : ConfigurationElement
{
    //Configuration attributes
}
/// <summary>
/// SubProtocol configuation collection
/// </summary>
[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<SubProtocolConfigCollection>("subProtocols");

        if (m_SubProtocols == null)
        {
            if(Logger.IsErrorEnabled)
                Logger.Error("The child configuration node 'subProtocols' is required!");
            return false;
        }

        return true;
    }
}
posted @ 2013-03-13 23:40  逗豆豆  阅读(305)  评论(0编辑  收藏  举报