SuperSocket源码解析之配置系统

一 继承Net配置系统

   Net应用程序配置机制跟程序集引用大致类似,均具有继承性,如iis几乎每个应用程序都会有一个Web.config,比如我们使用vs2012以上版本创建一个web应用程序会自带一个web.config配置文件,这个配置文件属于整个应用程序全局配置文件,再有某些个别目录下也会存在web.config这样的配置文件,连名字都跟应用程序配置文件相同,那么他们看起来重复出现,到底是为何?

  Net的配置具有继承性,怎么理解?比如当前文件夹所拥有的配置则作用范围仅为当前文件夹,如果没有那么他们直接寻找应用程序根目录下config文件并当作自己的配置文件直到应用程序根目录,其查找顺序由下往上,由近及远,与dll引用一样本地找不到就去GAC找直到找不到;其中IIS中各个Web.Config继承性就非常的典型,所以这里也不再举例

  SuperSocket使用了Net配置系统,而不是我们平常的一个对象序列化成一个xml文件的私有配置,对比起来减少了配置文件读写次数,且net配置系统本就如此强大 

二 SuperSocket私有配置扩展

2.1 TypeProvider  

SuperSocket对net配置元素进行一次继承,其代码如下

 1 public class TypeProvider : ConfigurationElement, ITypeProvider
 2     {
 3         /// <summary>
 4         /// Gets the name.
 5         /// </summary>
 6         [ConfigurationProperty("name", IsRequired = true)]
 7         public string Name
 8         {
 9             get { return this["name"] as string; }
10         }
11 
12         /// <summary>
13         /// Gets the type.
14         /// </summary>
15         [ConfigurationProperty("type", IsRequired = true)]
16         public string Type
17         {
18             get { return this["type"] as string; }
19         }
20     }
View Code

扩展了Name和Type两个属性,这与SuperSocket类型提供工厂类完全对应;

 1  /// <summary>
 2     /// Provider factory infomation
 3     /// </summary>
 4     [Serializable]
 5     public class ProviderFactoryInfo
 6     {
 7         /// <summary>
 8         /// Gets the key.
 9         /// </summary>
10         public ProviderKey Key { get; set; }
11 
12         /// <summary>
13         /// Gets or sets the name.
14         /// </summary>
15         /// <value>
16         /// The name.
17         /// </value>
18         public string Name { get; set; }
19 
20 
21         /// <summary>
22         /// Gets or sets the export factory.
23         /// </summary>
24         /// <value>
25         /// The export factory.
26         /// </value>
27         public ExportFactory ExportFactory { get; set; }
28 
29         /// <summary>
30         /// Initializes a new instance of the <see cref="ProviderFactoryInfo"/> class.
31         /// </summary>
32         public ProviderFactoryInfo()
33         {
34 
35         }
36 
37         /// <summary>
38         /// Initializes a new instance of the <see cref="ProviderFactoryInfo"/> class.
39         /// </summary>
40         /// <param name="key">The key.</param>
41         /// <param name="name">The name.</param>
42         /// <param name="instance">The instance.</param>
43         public ProviderFactoryInfo(ProviderKey key, string name, object instance)
44         {
45             Key = key;
46             Name = name;
47             ExportFactory = new ExportFactory(instance);
48         }
49 
50         /// <summary>
51         /// Initializes a new instance of the <see cref="ProviderFactoryInfo"/> class.
52         /// </summary>
53         /// <param name="key">The key.</param>
54         /// <param name="name">The name.</param>
55         /// <param name="typeName">Name of the type.</param>
56         public ProviderFactoryInfo(ProviderKey key, string name, string typeName)
57         {
58             Key = key;
59             Name = name;
60             ExportFactory = new ExportFactory(typeName);
61         }
62 
63         /// <summary>
64         /// Initializes a new instance of the <see cref="ProviderFactoryInfo"/> class.
65         /// </summary>
66         /// <param name="key">The key.</param>
67         /// <param name="name">The name.</param>
68         /// <param name="type">The type.</param>
69         public ProviderFactoryInfo(ProviderKey key, string name, Type type)
70             : this(key, name, type.AssemblyQualifiedName)
71         {
72             
73         }
74     }
View Code

从代码上可以看出只要有Name和Type这2个参数即可构造一个ProviderFactoryInfo类的实例

2.2 TypeProviderCollection 扩展的TypeProvider类型集合

 1 [ConfigurationCollection(typeof(TypeProvider))]
 2     public class TypeProviderCollection : ConfigurationElementCollection, IEnumerable<ITypeProvider>
 3     {
 4         /// <summary>
 5         /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
 6         /// </summary>
 7         /// <returns>
 8         /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
 9         /// </returns>
10         protected override ConfigurationElement CreateNewElement()
11         {
12             return new TypeProvider() as ConfigurationElement;
13         }
14 
15         /// <summary>
16         /// Gets the element key for a specified configuration element when overridden in a derived class.
17         /// </summary>
18         /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
19         /// <returns>
20         /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
21         /// </returns>
22         protected override object GetElementKey(ConfigurationElement element)
23         {
24             var provider = element as TypeProvider;
25 
26             if (provider == null)
27                 return null;
28 
29             return provider.Name;
30         }
31 
32         /// <summary>
33         /// Returns an enumerator that iterates through the collection.
34         /// </summary>
35         /// <returns>
36         /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
37         /// </returns>
38         public new IEnumerator<ITypeProvider> GetEnumerator()
39         {
40             int count = base.Count;
41 
42             for (int i = 0; i < count; i++)
43             {
44                 yield return (ITypeProvider)base.BaseGet(i);
45             }
46         }
47     }
View Code

三 SuperSocket配置内容

3.1 全局配置

如图红色部分所示,SuperSocket的所有配置均包裹在SocketServiceConfig这类型中,且成为App.config的一个配置节点section,那么SocketServiceConfig又包含了什么呢?

  1 public partial class SocketServiceConfig : ConfigurationSection, IConfigurationSource
  2     {
  3         /// <summary>
  4         /// Gets all the server configurations
  5         /// </summary>
  6         [ConfigurationProperty("servers")]
  7         public ServerCollection Servers
  8         {
  9             get
 10             {
 11                 return this["servers"] as ServerCollection;
 12             }
 13         }
 14 
 15         /// <summary>
 16         /// Gets the service configurations
 17         /// </summary>
 18         [ConfigurationProperty("serverTypes")]
 19         public TypeProviderCollection ServerTypes
 20         {
 21             get
 22             {
 23                 return this["serverTypes"] as TypeProviderCollection;
 24             }
 25         }
 26 
 27         /// <summary>
 28         /// Gets all the connection filter configurations.
 29         /// </summary>
 30         [ConfigurationProperty("connectionFilters", IsRequired = false)]
 31         public TypeProviderCollection ConnectionFilters
 32         {
 33             get
 34             {
 35                 return this["connectionFilters"] as TypeProviderCollection;
 36             }
 37         }
 38 
 39         /// <summary>
 40         /// Gets the defined log factory types.
 41         /// </summary>
 42         [ConfigurationProperty("logFactories", IsRequired = false)]
 43         public TypeProviderCollection LogFactories
 44         {
 45             get
 46             {
 47                 return this["logFactories"] as TypeProviderCollection;
 48             }
 49         }
 50 
 51         /// <summary>
 52         /// Gets the logfactory name of the bootstrap.
 53         /// </summary>
 54         [ConfigurationProperty("receiveFilterFactories", IsRequired = false)]
 55         public TypeProviderCollection ReceiveFilterFactories
 56         {
 57             get
 58             {
 59                 return this["receiveFilterFactories"] as TypeProviderCollection;
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// Gets the command loaders definition.
 65         /// </summary>
 66         [ConfigurationProperty("commandLoaders", IsRequired = false)]
 67         public TypeProviderCollection CommandLoaders
 68         {
 69             get
 70             {
 71                 return this["commandLoaders"] as TypeProviderCollection;
 72             }
 73         }
 74 
 75         /// <summary>
 76         /// Gets the max working threads.
 77         /// </summary>
 78         [ConfigurationProperty("maxWorkingThreads", IsRequired = false, DefaultValue = -1)]
 79         public int MaxWorkingThreads
 80         {
 81             get
 82             {
 83                 return (int)this["maxWorkingThreads"];
 84             }
 85         }
 86 
 87         /// <summary>
 88         /// Gets the min working threads.
 89         /// </summary>
 90         [ConfigurationProperty("minWorkingThreads", IsRequired = false, DefaultValue = -1)]
 91         public int MinWorkingThreads
 92         {
 93             get
 94             {
 95                 return (int)this["minWorkingThreads"];
 96             }
 97         }
 98 
 99         /// <summary>
100         /// Gets the max completion port threads.
101         /// </summary>
102         [ConfigurationProperty("maxCompletionPortThreads", IsRequired = false, DefaultValue = -1)]
103         public int MaxCompletionPortThreads
104         {
105             get
106             {
107                 return (int)this["maxCompletionPortThreads"];
108             }
109         }
110 
111         /// <summary>
112         /// Gets the min completion port threads.
113         /// </summary>
114         [ConfigurationProperty("minCompletionPortThreads", IsRequired = false, DefaultValue = -1)]
115         public int MinCompletionPortThreads
116         {
117             get
118             {
119                 return (int)this["minCompletionPortThreads"];
120             }
121         }
122 
123         /// <summary>
124         /// Gets the performance data collect interval, in seconds.
125         /// </summary>
126         [ConfigurationProperty("performanceDataCollectInterval", IsRequired = false, DefaultValue = 60)]
127         public int PerformanceDataCollectInterval
128         {
129             get
130             {
131                 return (int)this["performanceDataCollectInterval"];
132             }
133         }
134 
135         /// <summary>
136         /// Gets a value indicating whether [disable performance data collector].
137         /// </summary>
138         /// <value>
139         ///     <c>true</c> if [disable performance data collector]; otherwise, <c>false</c>.
140         /// </value>
141         [ConfigurationProperty("disablePerformanceDataCollector", IsRequired = false, DefaultValue = false)]
142         public bool DisablePerformanceDataCollector
143         {
144             get
145             {
146                 return (bool)this["disablePerformanceDataCollector"];
147             }
148         }
149 
150         /// <summary>
151         /// Gets the isolation mode.
152         /// </summary>
153         [ConfigurationProperty("isolation", IsRequired = false, DefaultValue = IsolationMode.None)]
154         public IsolationMode Isolation
155         {
156             get { return (IsolationMode)this["isolation"]; }
157         }
158 
159         /// <summary>
160         /// Gets the logfactory name of the bootstrap.
161         /// </summary>
162         [ConfigurationProperty("logFactory", IsRequired = false, DefaultValue = "")]
163         public string LogFactory
164         {
165             get
166             {
167                 return (string)this["logFactory"];
168             }
169         }
170 
171         /// <summary>
172         /// Gets the option elements.
173         /// </summary>
174         public NameValueCollection OptionElements { get; private set; }
175 
176         /// <summary>
177         /// Gets a value indicating whether an unknown element is encountered during deserialization.
178         /// To keep compatible with old configuration
179         /// </summary>
180         /// <param name="elementName">The name of the unknown subelement.</param>
181         /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> being used for deserialization.</param>
182         /// <returns>
183         /// true when an unknown element is encountered while deserializing; otherwise, false.
184         /// </returns>
185         /// <exception cref="T:System.Configuration.ConfigurationErrorsException">The element identified by <paramref name="elementName"/> is locked.- or -One or more of the element's attributes is locked.- or -<paramref name="elementName"/> is unrecognized, or the element has an unrecognized attribute.- or -The element has a Boolean attribute with an invalid value.- or -An attempt was made to deserialize a property more than once.- or -An attempt was made to deserialize a property that is not a valid member of the element.- or -The element cannot contain a CDATA or text element.</exception>
186         protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
187         {
188             //To keep compatible with old configuration
189             if (!"services".Equals(elementName, StringComparison.OrdinalIgnoreCase))
190             {
191                 if (OptionElements == null)
192                     OptionElements = new NameValueCollection();
193 
194                 OptionElements.Add(elementName, reader.ReadOuterXml());
195                 return true;
196             }
197 
198             var serverTypes = new TypeProviderCollection();
199             reader.Read();
200             serverTypes.Deserialize(reader);
201 
202             this["serverTypes"] = serverTypes;
203 
204             return true;
205         }
206 
207         /// <summary>
208         /// Gets a value indicating whether an unknown attribute is encountered during deserialization.
209         /// </summary>
210         /// <param name="name">The name of the unrecognized attribute.</param>
211         /// <param name="value">The value of the unrecognized attribute.</param>
212         /// <returns>
213         /// true when an unknown attribute is encountered while deserializing; otherwise, false.
214         /// </returns>
215         protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
216         {
217             const string xmlns = "xmlns";
218             const string xmlnsPrefix = "xmlns:";
219             const string xsiPrefix = "xsi:";
220 
221             //for configuration intellisense, allow these unrecognized attributes: xmlns, xmlns:*, xsi:*
222             if (name.Equals(xmlns) || name.StartsWith(xmlnsPrefix) || name.StartsWith(xsiPrefix))
223                 return true;
224 
225             return false;
226         }
227 
228         /// <summary>
229         /// Gets the child config.
230         /// </summary>
231         /// <typeparam name="TConfig">The type of the config.</typeparam>
232         /// <param name="childConfigName">Name of the child config.</param>
233         /// <returns></returns>
234         public TConfig GetChildConfig<TConfig>(string childConfigName)
235             where TConfig : ConfigurationElement, new()
236         {
237             return this.OptionElements.GetChildConfig<TConfig>(childConfigName);
238         }
239 
240         IEnumerable<IServerConfig> IConfigurationSource.Servers
241         {
242             get
243             {
244                 return this.Servers;
245             }
246         }
247 
248         IEnumerable<ITypeProvider> IConfigurationSource.ServerTypes
249         {
250             get
251             {
252                 return this.ServerTypes;
253             }
254         }
255 
256         IEnumerable<ITypeProvider> IConfigurationSource.ConnectionFilters
257         {
258             get
259             {
260                 return this.ConnectionFilters;
261             }
262         }
263 
264         IEnumerable<ITypeProvider> IConfigurationSource.LogFactories
265         {
266             get
267             {
268                 return this.LogFactories;
269             }
270         }
271 
272         IEnumerable<ITypeProvider> IConfigurationSource.ReceiveFilterFactories
273         {
274             get
275             {
276                 return this.ReceiveFilterFactories;
277             }
278         }
279 
280 
281         IEnumerable<ITypeProvider> IConfigurationSource.CommandLoaders
282         {
283             get
284             {
285                 return this.CommandLoaders;
286             }
287         }
288     }
View Code

1)Servers服务实例;

  集合:AppServer有多少appserver实例

2)ServerTypes服务类型ServerTypes

  集合:继承AppServer的子类型

3)ConnectionFilters

  集合:说明了有多少个连接过滤工厂类型

4)LogFactories

     集合:说明了有多少个日志工厂类型

5)ReceiveFilterFactories

  集合:说明了有多少个协议解析工厂类型

6)CommandLoaders

  集合:说明了有多少个命令加载工厂类型

7)MaxWorkingThreads

  app最大工作线程数,主要用于配置ThreadPool

 等等,这里可以详细参考官方文档SuperSocket配置:http://docs.supersocket.net/v1-6/zh-CN/SuperSocket-Basic-Configuration

其中类型的配置示例

 <serverTypes>

   <add name="TelnetServer" type="SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_StartByConfig"/
</serverTypes>

均是TypeProvider,只需指定Name和type即可

 3.2 服务配置

与之对应的类型ServerConfig,每个详细的配置均有默认值

  1 public partial class ServerConfig : IServerConfig
  2     {
  3         /// <summary>
  4         /// Default ReceiveBufferSize
  5         /// </summary>
  6         public const int DefaultReceiveBufferSize = 4096;
  7 
  8         /// <summary>
  9         /// Default MaxConnectionNumber
 10         /// </summary>
 11         public const int DefaultMaxConnectionNumber = 100;
 12 
 13 
 14         /// <summary>
 15         /// Default sending queue size
 16         /// </summary>
 17         public const int DefaultSendingQueueSize = 5;
 18 
 19         /// <summary>
 20         /// Default MaxRequestLength
 21         /// </summary>
 22         public const int DefaultMaxRequestLength = 1024;
 23 
 24 
 25         /// <summary>
 26         /// Default send timeout value, in milliseconds
 27         /// </summary>
 28         public const int DefaultSendTimeout = 5000;
 29 
 30 
 31         /// <summary>
 32         /// Default clear idle session interval
 33         /// </summary>
 34         public const int DefaultClearIdleSessionInterval = 120;
 35 
 36 
 37         /// <summary>
 38         /// Default idle session timeout
 39         /// </summary>
 40         public const int DefaultIdleSessionTimeOut = 300;
 41 
 42 
 43         /// <summary>
 44         /// The default send buffer size
 45         /// </summary>
 46         public const int DefaultSendBufferSize = 2048;
 47 
 48 
 49         /// <summary>
 50         /// The default session snapshot interval
 51         /// </summary>
 52         public const int DefaultSessionSnapshotInterval = 5;
 53 
 54         /// <summary>
 55         /// The default keep alive time
 56         /// </summary>
 57         public const int DefaultKeepAliveTime = 600; // 60 * 10 = 10 minutes
 58 
 59 
 60         /// <summary>
 61         /// The default keep alive interval
 62         /// </summary>
 63         public const int DefaultKeepAliveInterval = 60; // 60 seconds
 64 
 65 
 66         /// <summary>
 67         /// The default listen backlog
 68         /// </summary>
 69         public const int DefaultListenBacklog = 100;
 70 
 71 
 72         /// <summary>
 73         /// Initializes a new instance of the <see cref="ServerConfig"/> class.
 74         /// </summary>
 75         /// <param name="serverConfig">The server config.</param>
 76         public ServerConfig(IServerConfig serverConfig)
 77         {
 78             serverConfig.CopyPropertiesTo(this);
 79             
 80             this.Options = serverConfig.Options;
 81             this.OptionElements = serverConfig.OptionElements;
 82 
 83             if (serverConfig.Certificate != null)
 84                 this.Certificate = serverConfig.Certificate.CopyPropertiesTo(new CertificateConfig());
 85 
 86             if (serverConfig.Listeners != null && serverConfig.Listeners.Any())
 87             {
 88                 this.Listeners = serverConfig.Listeners.Select(l => l.CopyPropertiesTo(new ListenerConfig())).OfType<ListenerConfig>().ToArray();
 89             }
 90 
 91             if (serverConfig.CommandAssemblies != null && serverConfig.CommandAssemblies.Any())
 92             {
 93                 this.CommandAssemblies = serverConfig.CommandAssemblies.Select(c => c.CopyPropertiesTo(new CommandAssemblyConfig())).OfType<CommandAssemblyConfig>().ToArray();
 94             }
 95         }
 96 
 97         /// <summary>
 98         /// Initializes a new instance of the <see cref="ServerConfig"/> class.
 99         /// </summary>
100         public ServerConfig()
101         {
102             Security = "None";
103             MaxConnectionNumber = DefaultMaxConnectionNumber;
104             Mode = SocketMode.Tcp;
105             MaxRequestLength = DefaultMaxRequestLength;
106             KeepAliveTime = DefaultKeepAliveTime;
107             KeepAliveInterval = DefaultKeepAliveInterval;
108             ListenBacklog = DefaultListenBacklog;
109             ReceiveBufferSize = DefaultReceiveBufferSize;
110             SendingQueueSize = DefaultSendingQueueSize;
111             SendTimeOut = DefaultSendTimeout;
112             ClearIdleSessionInterval = DefaultClearIdleSessionInterval;
113             IdleSessionTimeOut = DefaultIdleSessionTimeOut;
114             SendBufferSize = DefaultSendBufferSize;
115             LogBasicSessionActivity = true;
116             SessionSnapshotInterval = DefaultSessionSnapshotInterval;
117         }
118 
119         #region IServerConfig Members
120 
121         /// <summary>
122         /// Gets/sets the name of the server type of this appServer want to use.
123         /// </summary>
124         /// <value>
125         /// The name of the server type.
126         /// </value>
127         public string ServerTypeName { get; set; }
128 
129 
130         /// <summary>
131         /// Gets/sets the type definition of the appserver.
132         /// </summary>
133         /// <value>
134         /// The type of the server.
135         /// </value>
136         public string ServerType { get; set; }
137 
138         /// <summary>
139         /// Gets/sets the Receive filter factory.
140         /// </summary>
141         public string ReceiveFilterFactory { get; set; }
142 
143         /// <summary>
144         /// Gets/sets the ip.
145         /// </summary>
146         public string Ip { get; set; }
147 
148         /// <summary>
149         /// Gets/sets the port.
150         /// </summary>
151         public int Port { get; set; }
152 
153         /// <summary>
154         /// Gets/sets the options.
155         /// </summary>
156         public NameValueCollection Options { get; set; }
157 
158         /// <summary>
159         /// Gets the option elements.
160         /// </summary>
161         public NameValueCollection OptionElements { get; set; }
162 
163         /// <summary>
164         /// Gets/sets a value indicating whether this <see cref="IServerConfig"/> is disabled.
165         /// </summary>
166         /// <value>
167         ///   <c>true</c> if disabled; otherwise, <c>false</c>.
168         /// </value>
169         public bool Disabled { get; set; }
170 
171         /// <summary>
172         /// Gets the name.
173         /// </summary>
174         public string Name { get; set; }
175 
176         /// <summary>
177         /// Gets/sets the mode.
178         /// </summary>
179         public SocketMode Mode { get; set; }
180 
181         /// <summary>
182         /// Gets/sets the send time out.
183         /// </summary>
184         public int SendTimeOut { get; set; }
185 
186         /// <summary>
187         /// Gets the max connection number.
188         /// </summary>
189         public int MaxConnectionNumber { get; set; }
190 
191         /// <summary>
192         /// Gets the size of the receive buffer.
193         /// </summary>
194         /// <value>
195         /// The size of the receive buffer.
196         /// </value>
197         public int ReceiveBufferSize { get; set; }
198 
199         /// <summary>
200         /// Gets the size of the send buffer.
201         /// </summary>
202         /// <value>
203         /// The size of the send buffer.
204         /// </value>
205         public int SendBufferSize { get; set; }
206 
207 
208         /// <summary>
209         /// Gets a value indicating whether sending is in synchronous mode.
210         /// </summary>
211         /// <value>
212         ///   <c>true</c> if [sync send]; otherwise, <c>false</c>.
213         /// </value>
214         public bool SyncSend { get; set; }
215 
216         /// <summary>
217         /// Gets/sets a value indicating whether log command in log file.
218         /// </summary>
219         /// <value>
220         ///   <c>true</c> if log command; otherwise, <c>false</c>.
221         /// </value>
222         public bool LogCommand { get; set; }
223 
224         /// <summary>
225         /// Gets/sets a value indicating whether clear idle session.
226         /// </summary>
227         /// <value>
228         ///   <c>true</c> if clear idle session; otherwise, <c>false</c>.
229         /// </value>
230         public bool ClearIdleSession { get; set; }
231 
232         /// <summary>
233         /// Gets/sets the clear idle session interval, in seconds.
234         /// </summary>
235         /// <value>
236         /// The clear idle session interval.
237         /// </value>
238         public int ClearIdleSessionInterval { get; set; }
239 
240         /// <summary>
241         /// Gets/sets the idle session timeout time length, in seconds.
242         /// </summary>
243         /// <value>
244         /// The idle session time out.
245         /// </value>
246         public int IdleSessionTimeOut { get; set; }
247 
248         /// <summary>
249         /// Gets/sets X509Certificate configuration.
250         /// </summary>
251         /// <value>
252         /// X509Certificate configuration.
253         /// </value>
254         public ICertificateConfig Certificate { get; set; }
255 
256         /// <summary>
257         /// Gets/sets the security protocol, X509 certificate.
258         /// </summary>
259         public string Security { get; set; }
260 
261         /// <summary>
262         /// Gets/sets the length of the max request.
263         /// </summary>
264         /// <value>
265         /// The length of the max request.
266         /// </value>
267         public int MaxRequestLength { get; set; }
268 
269         /// <summary>
270         /// Gets/sets a value indicating whether [disable session snapshot].
271         /// </summary>
272         /// <value>
273         ///     <c>true</c> if [disable session snapshot]; otherwise, <c>false</c>.
274         /// </value>
275         public bool DisableSessionSnapshot { get; set; }
276 
277         /// <summary>
278         /// Gets/sets the interval to taking snapshot for all live sessions.
279         /// </summary>
280         public int SessionSnapshotInterval { get; set; }
281 
282         /// <summary>
283         /// Gets/sets the connection filters used by this server instance.
284         /// </summary>
285         /// <value>
286         /// The connection filter's name list, seperated by comma
287         /// </value>
288         public string ConnectionFilter { get; set; }
289 
290         /// <summary>
291         /// Gets the command loader, multiple values should be separated by comma.
292         /// </summary>
293         public string CommandLoader { get; set; }
294 
295         /// <summary>
296         /// Gets/sets the start keep alive time, in seconds
297         /// </summary>
298         public int KeepAliveTime { get; set; }
299 
300         /// <summary>
301         /// Gets/sets the keep alive interval, in seconds.
302         /// </summary>
303         public int KeepAliveInterval { get; set; }
304 
305         /// <summary>
306         /// Gets the backlog size of socket listening.
307         /// </summary>
308         public int ListenBacklog { get; set; }
309 
310         /// <summary>
311         /// Gets/sets the startup order of the server instance.
312         /// </summary>
313         public int StartupOrder { get; set; }
314 
315         /// <summary>
316         /// Gets the child config.
317         /// </summary>
318         /// <typeparam name="TConfig">The type of the config.</typeparam>
319         /// <param name="childConfigName">Name of the child config.</param>
320         /// <returns></returns>
321         public virtual TConfig GetChildConfig<TConfig>(string childConfigName)
322             where TConfig : ConfigurationElement, new()
323         {
324             return this.OptionElements.GetChildConfig<TConfig>(childConfigName);
325         }
326 
327         /// <summary>
328         /// Gets and sets the listeners' configuration.
329         /// </summary>
330         public IEnumerable<IListenerConfig> Listeners { get; set; }
331 
332         /// <summary>
333         /// Gets/sets the log factory name.
334         /// </summary>
335         public string LogFactory { get; set; }
336 
337         /// <summary>
338         /// Gets/sets the size of the sending queue.
339         /// </summary>
340         /// <value>
341         /// The size of the sending queue.
342         /// </value>
343         public int SendingQueueSize { get; set; }
344 
345         /// <summary>
346         /// Gets a value indicating whether [log basic session activity like connected and disconnected].
347         /// </summary>
348         /// <value>
349         ///     <c>true</c> if [log basic session activity]; otherwise, <c>false</c>.
350         /// </value>
351         public bool LogBasicSessionActivity { get; set; }
352 
353         /// <summary>
354         /// Gets/sets a value indicating whether [log all socket exception].
355         /// </summary>
356         /// <value>
357         /// <c>true</c> if [log all socket exception]; otherwise, <c>false</c>.
358         /// </value>
359         public bool LogAllSocketException { get; set; }
360 
361         /// <summary>
362         /// Gets/sets the default text encoding.
363         /// </summary>
364         /// <value>
365         /// The text encoding.
366         /// </value>
367         public string TextEncoding { get; set; }
368 
369         /// <summary>
370         /// Gets the command assemblies configuration.
371         /// </summary>
372         /// <value>
373         /// The command assemblies.
374         /// </value>
375         public IEnumerable<ICommandAssemblyConfig> CommandAssemblies { get; set; }
376 
377         #endregion
378     }
View Code

其详细可参考代码和文档:

http://docs.supersocket.net/v1-6/zh-CN/Start-SuperSocket-by-Configuration:重配置启动

http://docs.supersocket.net/v1-6/zh-CN/SuperSocket-Basic-Configuration:服务实例配置

3.3 IRootConfig

 这是最顶层的配置,从代码可以看出是对全局进行基础性配置,同时也是大部分配置实现了,如SocketServiceConfig

 1 public partial interface IRootConfig
 2     {
 3         /// <summary>
 4         /// Gets the max working threads.
 5         /// </summary>
 6         int MaxWorkingThreads { get; }
 7 
 8         /// <summary>
 9         /// Gets the min working threads.
10         /// </summary>
11         int MinWorkingThreads { get; }
12 
13         /// <summary>
14         /// Gets the max completion port threads.
15         /// </summary>
16         int MaxCompletionPortThreads { get; }
17 
18         /// <summary>
19         /// Gets the min completion port threads.
20         /// </summary>
21         int MinCompletionPortThreads { get; }
22 
23 
24         /// <summary>
25         /// Gets a value indicating whether [disable performance data collector].
26         /// </summary>
27         /// <value>
28         ///     <c>true</c> if [disable performance data collector]; otherwise, <c>false</c>.
29         /// </value>
30         bool DisablePerformanceDataCollector { get; }
31 
32         /// <summary>
33         /// Gets the performance data collect interval, in seconds.
34         /// </summary>
35         int PerformanceDataCollectInterval { get; }
36 
37 
38         /// <summary>
39         /// Gets the log factory name.
40         /// </summary>
41         /// <value>
42         /// The log factory.
43         /// </value>
44         string LogFactory { get; }
45 
46 
47         /// <summary>
48         /// Gets the isolation mode.
49         /// </summary>
50         IsolationMode Isolation { get; }
51 
52 
53         /// <summary>
54         /// Gets the option elements.
55         /// </summary>
56         NameValueCollection OptionElements { get; }
57 
58         /// <summary>
59         /// Gets the child config.
60         /// </summary>
61         /// <typeparam name="TConfig">The type of the config.</typeparam>
62         /// <param name="childConfigName">Name of the child config.</param>
63         /// <returns></returns>
64         TConfig GetChildConfig<TConfig>(string childConfigName)
65             where TConfig : ConfigurationElement, new();
66     }
View Code

四 配置如何使用

诚然net的配置是非常方便的只需一句代码即可

var configSection = ConfigurationManager.GetSection("superSocket");即可拿到所有Supersocket的配置

五 根据配置启动

  在所提供的代码中大多数都是以配置启动服务器,将SuperSocket直接作为app,那么其启动入口则有BootstrapFactory开始

5.1 引导程序开始

public static IBootstrap CreateBootstrap()
{
  var configSection = ConfigurationManager.GetSection("superSocket");

  if (configSection == null)//to keep compatible with old version
    configSection = ConfigurationManager.GetSection("socketServer");

  if(configSection == null)
    throw new ConfigurationErrorsException("Missing 'superSocket' or 'socketServer' configuration section.");

  var configSource = configSection as IConfigurationSource;
  if(configSource == null)
    throw new ConfigurationErrorsException("Invalid 'superSocket' or 'socketServer' configuration section.");

  return CreateBootstrap(configSource);
}

5.2 appserver工厂代理加载器

public List<WorkItemFactoryInfo> LoadResult(Func<IServerConfig, IServerConfig> serverConfigResolver)
通过代理直接将所配置的类型提供信息解析成 ProviderFactoryInfo他们只是封装了name和type但是可以导出各类工厂如LogFactory,这里就直接转到了工厂设计模式频道了

然后将所有这些ProviderFactoryInfo成为WorkItemFactoryInfo的配套也就是说每个AppServer的配套,AppServer所需要的如Log,SocketServer均由ProviderFactoryInfo所导出的与之相应的LogFactory,SocketServerFactory创建,并且将配置作为这些对象初始化参数

5.3 启动所有Server

六 默认配置

6.1 默认全局日志

由Log4NetFactory创建,代码位置

6.2 AppServer 默认日志

Log4NetFactory

6.3 默认的SocketServerFactroy

workItemFactory.SocketServerFactory = new ProviderFactoryInfo(ProviderKey.SocketServerFactory, string.Empty, typeof(SocketServerFactory));

6.4 默认的协议

且使用换行符作为结束标志,如发送一条消息 echo xxd ccc;那么解析后key:echo,body:xxd ccc ,params:xxd,ccc,其中参数以空格为分割

6.5 连接过滤

没有默认

6.6 命令加载器

ReflectCommandLoader

 

七 配置改变

http://docs.supersocket.net/v1-6/zh-CN/Server-Configuration-Hot-Update

原文地址:https://www.cnblogs.com/rjjs/p/5618773.html