BootstrapFactory.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.SocketBase;
  6. using SuperSocket.SocketEngine.Configuration;
  7. using SuperSocket.SocketBase.Config;
  8. using System.Configuration;
  9. namespace SuperSocket.SocketEngine
  10. {
  11. /// <summary>
  12. /// Bootstrap Factory
  13. /// </summary>
  14. public static class BootstrapFactory
  15. {
  16. /// <summary>
  17. /// Creates the bootstrap.
  18. /// </summary>
  19. /// <param name="config">The config.</param>
  20. /// <returns></returns>
  21. public static IBootstrap CreateBootstrap(IConfigurationSource config)
  22. {
  23. if (config == null)
  24. throw new ArgumentNullException("config");
  25. IBootstrap bootstrap;
  26. if (config.Isolation == IsolationMode.AppDomain)
  27. bootstrap = new AppDomainBootstrap(config);
  28. else if (config.Isolation == IsolationMode.Process)
  29. bootstrap = new ProcessBootstrap(config);
  30. else
  31. bootstrap = new DefaultBootstrap(config);
  32. var section = config as ConfigurationSection;
  33. if (section != null)
  34. ConfigurationWatcher.Watch(section, bootstrap);
  35. return bootstrap;
  36. }
  37. /// <summary>
  38. /// Creates the bootstrap from app configuration's socketServer section.
  39. /// </summary>
  40. /// <returns></returns>
  41. public static IBootstrap CreateBootstrap()
  42. {
  43. var configSection = ConfigurationManager.GetSection("superSocket");
  44. if (configSection == null)//to keep compatible with old version
  45. configSection = ConfigurationManager.GetSection("socketServer");
  46. if(configSection == null)
  47. throw new ConfigurationErrorsException("Missing 'superSocket' or 'socketServer' configuration section.");
  48. var configSource = configSection as IConfigurationSource;
  49. if(configSource == null)
  50. throw new ConfigurationErrorsException("Invalid 'superSocket' or 'socketServer' configuration section.");
  51. return CreateBootstrap(configSource);
  52. }
  53. /// <summary>
  54. /// Creates the bootstrap.
  55. /// </summary>
  56. /// <param name="configSectionName">Name of the config section.</param>
  57. /// <returns></returns>
  58. public static IBootstrap CreateBootstrap(string configSectionName)
  59. {
  60. var configSource = ConfigurationManager.GetSection(configSectionName) as IConfigurationSource;
  61. if (configSource == null)
  62. throw new ArgumentException("Invalid section name.");
  63. return CreateBootstrap(configSource);
  64. }
  65. /// <summary>
  66. /// Creates the bootstrap from configuration file.
  67. /// </summary>
  68. /// <param name="configFile">The configuration file.</param>
  69. /// <returns></returns>
  70. public static IBootstrap CreateBootstrapFromConfigFile(string configFile)
  71. {
  72. ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
  73. fileMap.ExeConfigFilename = configFile;
  74. var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
  75. var configSection = config.GetSection("superSocket");
  76. if (configSection == null)
  77. configSection = config.GetSection("socketServer");
  78. return CreateBootstrap(configSection as IConfigurationSource);
  79. }
  80. }
  81. }