AppDomainBootstrap.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.SocketBase.Provider;
  10. using SuperSocket.SocketEngine.Configuration;
  11. using SuperSocket.SocketBase.Metadata;
  12. using Microsoft.Extensions.Logging;
  13. namespace SuperSocket.SocketEngine
  14. {
  15. class AppDomainWorkItemFactoryInfoLoader : WorkItemFactoryInfoLoader
  16. {
  17. public AppDomainWorkItemFactoryInfoLoader(IConfigurationSource config, ILoggerFactory passedInLogFactory)
  18. : base(config, passedInLogFactory)
  19. {
  20. InitliazeValidationAppDomain();
  21. }
  22. public AppDomainWorkItemFactoryInfoLoader(IConfigurationSource config)
  23. : base(config)
  24. {
  25. InitliazeValidationAppDomain();
  26. }
  27. //private AppDomain m_ValidationAppDomain;
  28. //private TypeValidator m_Validator;
  29. private void InitliazeValidationAppDomain()
  30. {
  31. //m_ValidationAppDomain = AppDomain.CreateDomain("ValidationDomain");
  32. var validatorType = typeof(TypeValidator);
  33. //m_Validator = (TypeValidator)m_ValidationAppDomain.CreateInstanceAndUnwrap(validatorType.Assembly.FullName, validatorType.FullName);
  34. }
  35. protected override string ValidateProviderType(string typeName)
  36. {
  37. //if (!m_Validator.ValidateTypeName(typeName))
  38. // throw new Exception(string.Format("Failed to load type {0}!", typeName));
  39. return typeName;
  40. }
  41. protected override ServerTypeMetadata GetServerTypeMetadata(string typeName)
  42. {
  43. //return m_Validator.GetServerTypeMetadata(typeName);
  44. return null;
  45. }
  46. public override void Dispose()
  47. {
  48. //if (m_ValidationAppDomain != null)
  49. //{
  50. // AppDomain.Unload(m_ValidationAppDomain);
  51. // m_ValidationAppDomain = null;
  52. //}
  53. base.Dispose();
  54. }
  55. }
  56. class DefaultBootstrapAppDomainWrap : DefaultBootstrap
  57. {
  58. private IBootstrap m_Bootstrap;
  59. public DefaultBootstrapAppDomainWrap(IBootstrap bootstrap, IConfigurationSource config, string startupConfigFile)
  60. : base(config, startupConfigFile)
  61. {
  62. m_Bootstrap = bootstrap;
  63. }
  64. protected override IWorkItem CreateWorkItemInstance(string serviceTypeName, StatusInfoAttribute[] serverStatusMetadata)
  65. {
  66. return null;
  67. }
  68. internal override bool SetupWorkItemInstance(IWorkItem workItem, WorkItemFactoryInfo factoryInfo)
  69. {
  70. return workItem.Setup(m_Bootstrap, factoryInfo.Config, factoryInfo.ProviderFactories.ToArray());
  71. }
  72. internal override WorkItemFactoryInfoLoader GetWorkItemFactoryInfoLoader(IConfigurationSource config, ILoggerFactory logFactory)
  73. {
  74. return new AppDomainWorkItemFactoryInfoLoader(config, logFactory);
  75. }
  76. }
  77. /// <summary>
  78. /// AppDomainBootstrap
  79. /// </summary>
  80. partial class AppDomainBootstrap : MarshalByRefObject, SocketBase.ILoggerProvider, IBootstrap, IDisposable
  81. {
  82. private IBootstrap m_InnerBootstrap;
  83. /// <summary>
  84. /// Gets all the app servers running in this bootstrap
  85. /// </summary>
  86. public IEnumerable<IWorkItem> AppServers
  87. {
  88. get { return m_InnerBootstrap.AppServers; }
  89. }
  90. /// <summary>
  91. /// Gets the config.
  92. /// </summary>
  93. public IRootConfig Config
  94. {
  95. get { return m_InnerBootstrap.Config; }
  96. }
  97. /// <summary>
  98. /// Gets the bootstrap logger.
  99. /// </summary>
  100. ILogger SocketBase.ILoggerProvider.Logger
  101. {
  102. get
  103. {
  104. var loggerProvider = m_InnerBootstrap as SocketBase.ILoggerProvider;
  105. if (loggerProvider == null)
  106. return null;
  107. return loggerProvider.Logger;
  108. }
  109. }
  110. /// <summary>
  111. /// Gets the startup config file.
  112. /// </summary>
  113. public string StartupConfigFile
  114. {
  115. get { return m_InnerBootstrap.StartupConfigFile; }
  116. }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="AppDomainBootstrap"/> class.
  119. /// </summary>
  120. public AppDomainBootstrap(IConfigurationSource config)
  121. {
  122. string startupConfigFile = string.Empty;
  123. if (config == null)
  124. throw new ArgumentNullException("config");
  125. var configSectionSource = config as ConfigurationSection;
  126. if (configSectionSource != null)
  127. startupConfigFile = configSectionSource.GetConfigSource();
  128. //Keep serializable version of configuration
  129. if(!config.GetType().IsSerializable)
  130. config = new ConfigurationSource(config);
  131. //Still use raw configuration type to bootstrap
  132. m_InnerBootstrap = CreateBootstrapWrap(this, config, startupConfigFile);
  133. AppDomain.CurrentDomain.SetData("Bootstrap", this);
  134. }
  135. protected virtual IBootstrap CreateBootstrapWrap(IBootstrap bootstrap, IConfigurationSource config, string startupConfigFile)
  136. {
  137. return new DefaultBootstrapAppDomainWrap(this, config, startupConfigFile);
  138. }
  139. /// <summary>
  140. /// Initializes the bootstrap with the configuration
  141. /// </summary>
  142. /// <returns></returns>
  143. public bool Initialize()
  144. {
  145. return m_InnerBootstrap.Initialize();
  146. }
  147. /// <summary>
  148. /// Initializes the bootstrap with the configuration and config resolver.
  149. /// </summary>
  150. /// <param name="serverConfigResolver">The server config resolver.</param>
  151. /// <returns></returns>
  152. public bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver)
  153. {
  154. return m_InnerBootstrap.Initialize(serverConfigResolver);
  155. }
  156. /// <summary>
  157. /// Initializes the bootstrap with the configuration and config resolver.
  158. /// </summary>
  159. /// <param name="logFactory">The log factory.</param>
  160. /// <returns></returns>
  161. public bool Initialize(ILoggerFactory logFactory)
  162. {
  163. return m_InnerBootstrap.Initialize(logFactory);
  164. }
  165. /// <summary>
  166. /// Initializes the bootstrap with a listen endpoint replacement dictionary
  167. /// </summary>
  168. /// <param name="listenEndPointReplacement">The listen end point replacement.</param>
  169. /// <returns></returns>
  170. public bool Initialize(IDictionary<string, System.Net.IPEndPoint> listenEndPointReplacement)
  171. {
  172. return m_InnerBootstrap.Initialize(listenEndPointReplacement);
  173. }
  174. /// <summary>
  175. /// Initializes the bootstrap with the configuration
  176. /// </summary>
  177. /// <param name="serverConfigResolver">The server config resolver.</param>
  178. /// <param name="logFactory">The log factory.</param>
  179. /// <returns></returns>
  180. public bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver, ILoggerFactory logFactory)
  181. {
  182. if (logFactory != null)
  183. throw new Exception("You cannot pass in logFactory, if your isolation level is AppDomain!");
  184. return m_InnerBootstrap.Initialize(serverConfigResolver, logFactory);
  185. }
  186. /// <summary>
  187. /// Starts this bootstrap.
  188. /// </summary>
  189. /// <returns></returns>
  190. public StartResult Start()
  191. {
  192. return m_InnerBootstrap.Start();
  193. }
  194. /// <summary>
  195. /// Stops this bootstrap.
  196. /// </summary>
  197. public void Stop()
  198. {
  199. m_InnerBootstrap.Stop();
  200. }
  201. public string BaseDirectory
  202. {
  203. get { return m_InnerBootstrap.BaseDirectory; }
  204. }
  205. void IDisposable.Dispose()
  206. {
  207. var disposableBootstrap = m_InnerBootstrap as IDisposable;
  208. if (disposableBootstrap != null)
  209. disposableBootstrap.Dispose();
  210. }
  211. }
  212. }