using System; using System.Reflection; using SuperSocket.SocketBase; using SuperSocket.SocketBase.Config; using SuperSocket.SocketBase.Provider; using System.IO; using SuperSocket.SocketBase.Metadata; namespace SuperSocket.SocketEngine { /// /// AppDomainAppServer /// partial class AppDomainAppServer : IsolationAppServer { private AppDomain m_HostDomain; /// /// Initializes a new instance of the class. /// /// Name of the server type. /// The server status metadata. public AppDomainAppServer(string serverTypeName, StatusInfoAttribute[] serverStatusMetadata) : base(serverTypeName, serverStatusMetadata) { } /// /// Starts this server instance. /// /// /// return true if start successfull, else false /// protected override IWorkItemBase Start() { IWorkItem appServer; try { m_HostDomain = CreateHostAppDomain(); m_HostDomain.SetData(typeof(IsolationMode).Name, IsolationMode.AppDomain); var marshalServerType = typeof(MarshalAppServer); appServer = (IWorkItem)m_HostDomain.CreateInstanceAndUnwrap(marshalServerType.Assembly.FullName, marshalServerType.FullName, true, BindingFlags.CreateInstance, null, new object[] { ServerTypeName }, null, new object[0]); if (!appServer.Setup(Bootstrap, Config, Factories)) { OnExceptionThrown(new Exception("Failed to setup MarshalAppServer")); return null; } if (!appServer.Start()) { OnExceptionThrown(new Exception("Failed to start MarshalAppServer")); return null; } m_HostDomain.DomainUnload += new EventHandler(m_HostDomain_DomainUnload); return appServer; } catch (Exception e) { if (m_HostDomain != null) { AppDomain.Unload(m_HostDomain); m_HostDomain = null; } OnExceptionThrown(e); return null; } } void m_HostDomain_DomainUnload(object sender, EventArgs e) { OnStopped(); } protected override void OnStopped() { base.OnStopped(); m_HostDomain = null; } protected override void Stop() { if (m_HostDomain != null) { AppDomain.Unload(m_HostDomain); } } } }