DefaultBootstrap.Net40.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.Extensions.Logging;
  7. using SuperSocket.Common;
  8. using SuperSocket.SocketBase;
  9. using SuperSocket.SocketBase.Config;
  10. using SuperSocket.SocketEngine.Configuration;
  11. namespace SuperSocket.SocketEngine
  12. {
  13. public partial class DefaultBootstrap : IDynamicBootstrap
  14. {
  15. IWorkItem AddNewServer(IServerConfig config)
  16. {
  17. if (config == null)
  18. throw new ArgumentNullException("config");
  19. if (string.IsNullOrEmpty(config.Name))
  20. throw new ArgumentException("The new server's name cannot be empty.", "config");
  21. if (!m_Initialized)
  22. throw new Exception("The bootstrap must be initialized already!");
  23. if (m_AppServers.Any(s => config.Name.Equals(s.Name, StringComparison.OrdinalIgnoreCase)))
  24. {
  25. m_GlobalLog.LogError("The new server's name '{0}' has been taken by another server.", config.Name);
  26. return null;
  27. }
  28. var configSource = new ConfigurationSource(m_Config);
  29. configSource.Servers = new IServerConfig[] { new ServerConfig(config) };
  30. IEnumerable<WorkItemFactoryInfo> workItemFactories;
  31. using (var factoryInfoLoader = GetWorkItemFactoryInfoLoader(configSource, null))
  32. {
  33. try
  34. {
  35. workItemFactories = factoryInfoLoader.LoadResult((c) => c);
  36. }
  37. catch (Exception e)
  38. {
  39. m_GlobalLog.LogError(e,e.Message);
  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. }