ServerConfig.cs 12 KB

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