当你用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>
在上面的server配置信息中,SuperSocket并没有定义“policyFile”属性,但你仍可以在AppServer类中读取它:
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结点添加自定义的属性,还可以添加自定义的子节点,如下:
<server name="SuperWebSocket"
serverTypeName="SuperWebSocket"
ip="Any" port="2011" mode="Tcp">
<subProtocols>
<!--你的配置信息-->
</subProtocols>
</server>
如果这样做,还需要一个ConfigurationElement类:
/// <summary>
/// SubProtocol configuration
/// </summary>
public class SubProtocolConfig : ConfigurationElement
{
//配置属性
}
/// <summary>
/// SubProtocol configuation collection
/// </summary>
[ConfigurationCollection(typeof(SubProtocolConfig))]
public class SubProtocolConfigCollection : ConfigurationElementCollection
{
//配置属性
}
然后你就可以在你的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;
}
}
没有评论:
发表评论