AppDomainBootstrap.cs 8.2 KB

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