Server.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Security.Authentication;
  7. using System.Text;
  8. using System.Xml;
  9. using System.Linq;
  10. using SuperSocket.Common;
  11. using SuperSocket.SocketBase;
  12. using SuperSocket.SocketBase.Config;
  13. namespace SuperSocket.SocketEngine.Configuration
  14. {
  15. /// <summary>
  16. /// Server configuration
  17. /// </summary>
  18. public partial class Server : ConfigurationElementBase, IServerConfig
  19. {
  20. /// <summary>
  21. /// Gets the name of the server type this appServer want to use.
  22. /// </summary>
  23. /// <value>
  24. /// The name of the server type.
  25. /// </value>
  26. [ConfigurationProperty("serverTypeName", IsRequired = false)]
  27. public string ServerTypeName
  28. {
  29. get { return this["serverTypeName"] as string; }
  30. }
  31. /// <summary>
  32. /// Gets the type definition of the appserver.
  33. /// </summary>
  34. /// <value>
  35. /// The type of the server.
  36. /// </value>
  37. [ConfigurationProperty("serverType", IsRequired = false)]
  38. public string ServerType
  39. {
  40. get { return this["serverType"] as string; }
  41. }
  42. /// <summary>
  43. /// Gets the Receive filter factory.
  44. /// </summary>
  45. [ConfigurationProperty("receiveFilterFactory", IsRequired = false)]
  46. public string ReceiveFilterFactory
  47. {
  48. get { return this["receiveFilterFactory"] as string; }
  49. }
  50. /// <summary>
  51. /// Gets the ip.
  52. /// </summary>
  53. [ConfigurationProperty("ip", IsRequired = false)]
  54. public string Ip
  55. {
  56. get { return this["ip"] as string; }
  57. }
  58. /// <summary>
  59. /// Gets the port.
  60. /// </summary>
  61. [ConfigurationProperty("port", IsRequired = false)]
  62. public int Port
  63. {
  64. get { return (int)this["port"]; }
  65. }
  66. /// <summary>
  67. /// Gets the mode.
  68. /// </summary>
  69. [ConfigurationProperty("mode", IsRequired = false, DefaultValue = "Tcp")]
  70. public SocketMode Mode
  71. {
  72. get { return (SocketMode)this["mode"]; }
  73. }
  74. /// <summary>
  75. /// Gets a value indicating whether this <see cref="IServerConfig"/> is disabled.
  76. /// </summary>
  77. /// <value>
  78. /// <c>true</c> if disabled; otherwise, <c>false</c>.
  79. /// </value>
  80. [ConfigurationProperty("disabled", DefaultValue = "false")]
  81. public bool Disabled
  82. {
  83. get { return (bool)this["disabled"]; }
  84. }
  85. /// <summary>
  86. /// Gets the send time out.
  87. /// </summary>
  88. [ConfigurationProperty("sendTimeOut", IsRequired = false, DefaultValue = ServerConfig.DefaultSendTimeout)]
  89. public int SendTimeOut
  90. {
  91. get { return (int)this["sendTimeOut"]; }
  92. }
  93. /// <summary>
  94. /// Gets the max connection number.
  95. /// </summary>
  96. [ConfigurationProperty("maxConnectionNumber", IsRequired = false, DefaultValue = ServerConfig.DefaultMaxConnectionNumber)]
  97. public int MaxConnectionNumber
  98. {
  99. get { return (int)this["maxConnectionNumber"]; }
  100. }
  101. /// <summary>
  102. /// Gets the size of the receive buffer.
  103. /// </summary>
  104. /// <value>
  105. /// The size of the receive buffer.
  106. /// </value>
  107. [ConfigurationProperty("receiveBufferSize", IsRequired = false, DefaultValue = ServerConfig.DefaultReceiveBufferSize)]
  108. public int ReceiveBufferSize
  109. {
  110. get { return (int)this["receiveBufferSize"]; }
  111. }
  112. /// <summary>
  113. /// Gets the size of the send buffer.
  114. /// </summary>
  115. /// <value>
  116. /// The size of the send buffer.
  117. /// </value>
  118. [ConfigurationProperty("sendBufferSize", IsRequired = false, DefaultValue = ServerConfig.DefaultSendBufferSize)]
  119. public int SendBufferSize
  120. {
  121. get { return (int)this["sendBufferSize"]; }
  122. }
  123. /// <summary>
  124. /// Gets a value indicating whether sending is in synchronous mode.
  125. /// </summary>
  126. /// <value>
  127. /// <c>true</c> if [sync send]; otherwise, <c>false</c>.
  128. /// </value>
  129. [ConfigurationProperty("syncSend", IsRequired = false, DefaultValue = false)]
  130. public bool SyncSend
  131. {
  132. get { return (bool)this["syncSend"]; }
  133. }
  134. /// <summary>
  135. /// Gets a value indicating whether log command in log file.
  136. /// </summary>
  137. /// <value><c>true</c> if log command; otherwise, <c>false</c>.</value>
  138. [ConfigurationProperty("logCommand", IsRequired = false, DefaultValue = false)]
  139. public bool LogCommand
  140. {
  141. get { return (bool)this["logCommand"]; }
  142. }
  143. /// <summary>
  144. /// Gets a value indicating whether [log basic session activity like connected and disconnected].
  145. /// </summary>
  146. /// <value>
  147. /// <c>true</c> if [log basic session activity]; otherwise, <c>false</c>.
  148. /// </value>
  149. [ConfigurationProperty("logBasicSessionActivity", IsRequired = false, DefaultValue = true)]
  150. public bool LogBasicSessionActivity
  151. {
  152. get { return (bool)this["logBasicSessionActivity"]; }
  153. }
  154. /// <summary>
  155. /// Gets a value indicating whether [log all socket exception].
  156. /// </summary>
  157. /// <value>
  158. /// <c>true</c> if [log all socket exception]; otherwise, <c>false</c>.
  159. /// </value>
  160. [ConfigurationProperty("logAllSocketException", IsRequired = false, DefaultValue = false)]
  161. public bool LogAllSocketException
  162. {
  163. get { return (bool)this["logAllSocketException"]; }
  164. }
  165. /// <summary>
  166. /// Gets a value indicating whether clear idle session.
  167. /// </summary>
  168. /// <value><c>true</c> if clear idle session; otherwise, <c>false</c>.</value>
  169. [ConfigurationProperty("clearIdleSession", IsRequired = false, DefaultValue = false)]
  170. public bool ClearIdleSession
  171. {
  172. get { return (bool)this["clearIdleSession"]; }
  173. }
  174. /// <summary>
  175. /// Gets the clear idle session interval, in seconds.
  176. /// </summary>
  177. /// <value>The clear idle session interval.</value>
  178. [ConfigurationProperty("clearIdleSessionInterval", IsRequired = false, DefaultValue = ServerConfig.DefaultClearIdleSessionInterval)]
  179. public int ClearIdleSessionInterval
  180. {
  181. get { return (int)this["clearIdleSessionInterval"]; }
  182. }
  183. /// <summary>
  184. /// Gets the idle session timeout time length, in seconds.
  185. /// </summary>
  186. /// <value>The idle session time out.</value>
  187. [ConfigurationProperty("idleSessionTimeOut", IsRequired = false, DefaultValue = ServerConfig.DefaultIdleSessionTimeOut)]
  188. public int IdleSessionTimeOut
  189. {
  190. get { return (int)this["idleSessionTimeOut"]; }
  191. }
  192. /// <summary>
  193. /// Gets the certificate config.
  194. /// </summary>
  195. /// <value>The certificate config.</value>
  196. [ConfigurationProperty("certificate", IsRequired = false)]
  197. public CertificateConfig CertificateConfig
  198. {
  199. get
  200. {
  201. return (CertificateConfig)this["certificate"];
  202. }
  203. }
  204. /// <summary>
  205. /// Gets X509Certificate configuration.
  206. /// </summary>
  207. /// <value>
  208. /// X509Certificate configuration.
  209. /// </value>
  210. public ICertificateConfig Certificate
  211. {
  212. get { return CertificateConfig; }
  213. }
  214. /// <summary>
  215. /// Gets the security protocol, X509 certificate.
  216. /// </summary>
  217. [ConfigurationProperty("security", IsRequired = false, DefaultValue = "None")]
  218. public string Security
  219. {
  220. get
  221. {
  222. return (string)this["security"];
  223. }
  224. }
  225. /// <summary>
  226. /// Gets the max allowed length of request.
  227. /// </summary>
  228. /// <value>
  229. /// The max allowed length of request.
  230. /// </value>
  231. [ConfigurationProperty("maxRequestLength", IsRequired = false, DefaultValue = ServerConfig.DefaultMaxRequestLength)]
  232. public int MaxRequestLength
  233. {
  234. get
  235. {
  236. return (int)this["maxRequestLength"];
  237. }
  238. }
  239. /// <summary>
  240. /// Gets a value indicating whether [disable session snapshot]
  241. /// </summary>
  242. [ConfigurationProperty("disableSessionSnapshot", IsRequired = false, DefaultValue = false)]
  243. public bool DisableSessionSnapshot
  244. {
  245. get
  246. {
  247. return (bool)this["disableSessionSnapshot"];
  248. }
  249. }
  250. /// <summary>
  251. /// Gets the interval to taking snapshot for all live sessions.
  252. /// </summary>
  253. [ConfigurationProperty("sessionSnapshotInterval", IsRequired = false, DefaultValue = ServerConfig.DefaultSessionSnapshotInterval)]
  254. public int SessionSnapshotInterval
  255. {
  256. get
  257. {
  258. return (int)this["sessionSnapshotInterval"];
  259. }
  260. }
  261. /// <summary>
  262. /// Gets the connection filters used by this server instance.
  263. /// </summary>
  264. /// <value>
  265. /// The connection filters's name list, seperated by comma
  266. /// </value>
  267. [ConfigurationProperty("connectionFilter", IsRequired = false)]
  268. public string ConnectionFilter
  269. {
  270. get
  271. {
  272. return (string)this["connectionFilter"];
  273. }
  274. }
  275. /// <summary>
  276. /// Gets the command loader, multiple values should be separated by comma.
  277. /// </summary>
  278. [ConfigurationProperty("commandLoader", IsRequired = false)]
  279. public string CommandLoader
  280. {
  281. get
  282. {
  283. return (string)this["commandLoader"];
  284. }
  285. }
  286. /// <summary>
  287. /// Gets the start keep alive time, in seconds
  288. /// </summary>
  289. [ConfigurationProperty("keepAliveTime", IsRequired = false, DefaultValue = ServerConfig.DefaultKeepAliveTime)]
  290. public int KeepAliveTime
  291. {
  292. get
  293. {
  294. return (int)this["keepAliveTime"];
  295. }
  296. }
  297. /// <summary>
  298. /// Gets the keep alive interval, in seconds.
  299. /// </summary>
  300. [ConfigurationProperty("keepAliveInterval", IsRequired = false, DefaultValue = ServerConfig.DefaultKeepAliveInterval)]
  301. public int KeepAliveInterval
  302. {
  303. get
  304. {
  305. return (int)this["keepAliveInterval"];
  306. }
  307. }
  308. /// <summary>
  309. /// Gets the backlog size of socket listening.
  310. /// </summary>
  311. [ConfigurationProperty("listenBacklog", IsRequired = false, DefaultValue = ServerConfig.DefaultListenBacklog)]
  312. public int ListenBacklog
  313. {
  314. get
  315. {
  316. return (int)this["listenBacklog"];
  317. }
  318. }
  319. /// <summary>
  320. /// Gets the startup order of the server instance.
  321. /// </summary>
  322. [ConfigurationProperty("startupOrder", IsRequired = false, DefaultValue = 0)]
  323. public int StartupOrder
  324. {
  325. get
  326. {
  327. return (int)this["startupOrder"];
  328. }
  329. }
  330. /// <summary>
  331. /// Gets/sets the size of the sending queue.
  332. /// </summary>
  333. /// <value>
  334. /// The size of the sending queue.
  335. /// </value>
  336. [ConfigurationProperty("sendingQueueSize", IsRequired = false, DefaultValue = ServerConfig.DefaultSendingQueueSize)]
  337. public int SendingQueueSize
  338. {
  339. get
  340. {
  341. return (int)this["sendingQueueSize"];
  342. }
  343. }
  344. /// <summary>
  345. /// Gets the logfactory name of the server instance.
  346. /// </summary>
  347. [ConfigurationProperty("logFactory", IsRequired = false, DefaultValue = "")]
  348. public string LogFactory
  349. {
  350. get
  351. {
  352. return (string)this["logFactory"];
  353. }
  354. }
  355. /// <summary>
  356. /// Gets the default text encoding.
  357. /// </summary>
  358. /// <value>
  359. /// The text encoding.
  360. /// </value>
  361. [ConfigurationProperty("textEncoding", IsRequired = false, DefaultValue = "")]
  362. public string TextEncoding
  363. {
  364. get
  365. {
  366. return (string)this["textEncoding"];
  367. }
  368. }
  369. /// <summary>
  370. /// Gets the listeners' configuration.
  371. /// </summary>
  372. [ConfigurationProperty("listeners", IsRequired = false)]
  373. public ListenerConfigCollection Listeners
  374. {
  375. get
  376. {
  377. return this["listeners"] as ListenerConfigCollection;
  378. }
  379. }
  380. /// <summary>
  381. /// Gets the listeners' configuration.
  382. /// </summary>
  383. IEnumerable<IListenerConfig> IServerConfig.Listeners
  384. {
  385. get
  386. {
  387. return this.Listeners;
  388. }
  389. }
  390. /// <summary>
  391. /// Gets the command assemblies configuration.
  392. /// </summary>
  393. /// <value>
  394. /// The command assemblies.
  395. /// </value>
  396. [ConfigurationProperty("commandAssemblies", IsRequired = false)]
  397. public CommandAssemblyCollection CommandAssemblies
  398. {
  399. get
  400. {
  401. return this["commandAssemblies"] as CommandAssemblyCollection;
  402. }
  403. }
  404. IEnumerable<ICommandAssemblyConfig> IServerConfig.CommandAssemblies
  405. {
  406. get { return this.CommandAssemblies; }
  407. }
  408. /// <summary>
  409. /// Gets the child config.
  410. /// </summary>
  411. /// <typeparam name="TConfig">The type of the config.</typeparam>
  412. /// <param name="childConfigName">Name of the child config.</param>
  413. /// <returns></returns>
  414. public TConfig GetChildConfig<TConfig>(string childConfigName)
  415. where TConfig : ConfigurationElement, new()
  416. {
  417. return this.OptionElements.GetChildConfig<TConfig>(childConfigName);
  418. }
  419. /// <summary>
  420. /// Gets a value indicating whether an unknown attribute is encountered during deserialization.
  421. /// To keep compatible with old configuration
  422. /// </summary>
  423. /// <param name="name">The name of the unrecognized attribute.</param>
  424. /// <param name="value">The value of the unrecognized attribute.</param>
  425. /// <returns>
  426. /// true when an unknown attribute is encountered while deserializing; otherwise, false.
  427. /// </returns>
  428. protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
  429. {
  430. //To keep compatible with old configuration
  431. if (!"serviceName".Equals(name, StringComparison.OrdinalIgnoreCase))
  432. return base.OnDeserializeUnrecognizedAttribute(name, value);
  433. this["serverTypeName"] = value;
  434. return true;
  435. }
  436. }
  437. }