DefaultBootstrap.Net40.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using SuperSocket.Common;
  7. using SuperSocket.SocketBase;
  8. using SuperSocket.SocketBase.Config;
  9. using SuperSocket.SocketEngine.Configuration;
  10. namespace SuperSocket.SocketEngine
  11. {
  12. public partial class DefaultBootstrap : IDynamicBootstrap
  13. {
  14. IWorkItem AddNewServer(IServerConfig config)
  15. {
  16. if (config == null)
  17. throw new ArgumentNullException("config");
  18. if (string.IsNullOrEmpty(config.Name))
  19. throw new ArgumentException("The new server's name cannot be empty.", "config");
  20. if (!m_Initialized)
  21. throw new Exception("The bootstrap must be initialized already!");
  22. if (m_AppServers.Any(s => config.Name.Equals(s.Name, StringComparison.OrdinalIgnoreCase)))
  23. {
  24. m_GlobalLog.ErrorFormat("The new server's name '{0}' has been taken by another server.", config.Name);
  25. return null;
  26. }
  27. var configSource = new ConfigurationSource(m_Config);
  28. configSource.Servers = new IServerConfig[] { new ServerConfig(config) };
  29. IEnumerable<WorkItemFactoryInfo> workItemFactories;
  30. using (var factoryInfoLoader = GetWorkItemFactoryInfoLoader(configSource, null))
  31. {
  32. try
  33. {
  34. workItemFactories = factoryInfoLoader.LoadResult((c) => c);
  35. }
  36. catch (Exception e)
  37. {
  38. if (m_GlobalLog.IsErrorEnabled)
  39. m_GlobalLog.Error(e);
  40. return null;
  41. }
  42. }
  43. var server = InitializeAndSetupWorkItem(workItemFactories.FirstOrDefault());
  44. if (server != null)
  45. {
  46. m_AppServers.Add(server);
  47. if (!m_Config.DisablePerformanceDataCollector)
  48. {
  49. ResetPerfMoniter();
  50. }
  51. var section = m_Config as SocketServiceConfig;
  52. if (section != null) //file configuration
  53. {
  54. var serverConfig = new Server();
  55. serverConfig.LoadFrom(config);
  56. section.Servers.AddNew(serverConfig);
  57. ConfigurationWatcher.Pause();
  58. section.GetCurrentConfiguration().Save(ConfigurationSaveMode.Minimal);
  59. ConfigurationWatcher.Resume();
  60. }
  61. }
  62. return server;
  63. }
  64. bool IDynamicBootstrap.Add(IServerConfig config)
  65. {
  66. var newWorkItem = AddNewServer(config);
  67. return newWorkItem != null;
  68. }
  69. bool IDynamicBootstrap.AddAndStart(IServerConfig config)
  70. {
  71. var newWorkItem = AddNewServer(config);
  72. if (newWorkItem == null)
  73. return false;
  74. return newWorkItem.Start();
  75. }
  76. void IDynamicBootstrap.Remove(string name)
  77. {
  78. if (string.IsNullOrEmpty(name))
  79. throw new ArgumentNullException("name");
  80. var server = m_AppServers.FirstOrDefault(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
  81. if (server == null)
  82. throw new Exception("The server is not found.");
  83. if (server.State != ServerState.NotStarted)
  84. throw new Exception("The server is running now, you cannot remove it. Please stop it at first.");
  85. m_AppServers.Remove(server);
  86. ResetPerfMoniter();
  87. var section = m_Config as SocketServiceConfig;
  88. if (section != null) //file configuration
  89. {
  90. section.Servers.Remove(name);
  91. ConfigurationWatcher.Pause();
  92. section.GetCurrentConfiguration().Save(ConfigurationSaveMode.Minimal);
  93. ConfigurationWatcher.Resume();
  94. }
  95. }
  96. }
  97. }