一、初始化的过程
static void Main(string[] args) { var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) { Console.WriteLine("Failed to initialize!"); Console.ReadKey(); return; } }
supersocketsocketenginedefaultbootstrap.cs
DefaultBootStrap.cs public virtual bool Initialize() { return Initialize(c => c); } public virtual bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver) { return Initialize(serverConfigResolver, null); } public virtual bool Initialize(Func<IServerConfig, IServerConfig> serverConfigResolver, ILogFactory logFactory) { foreach (var factoryInfo in workItemFactories) { IWorkItem appServer = InitializeAndSetupWorkItem(factoryInfo); } } private IWorkItem InitializeAndSetupWorkItem(WorkItemFactoryInfo factoryInfo) { setupResult = SetupWorkItemInstance(appServer, factoryInfo); } internal virtual bool SetupWorkItemInstance(IWorkItem workItem, WorkItemFactoryInfo factoryInfo) { return workItem.Setup(this, factoryInfo.Config, factoryInfo.ProviderFactories.ToArray()); }
supersocketsocketbaseappserverbase.cs
bool IWorkItem.Setup(IBootstrap bootstrap, IServerConfig config, ProviderFactoryInfo[] factories) { Bootstrap = bootstrap; if (!SetupFinal()) return false; } private bool SetupFinal() { return SetupSocketServer(); } private bool SetupSocketServer() { m_SocketServer = m_SocketServerFactory.CreateSocketServer<TRequestInfo>(this, m_Listeners, Config); }
supersocketsocketenginesocketserverfactory.cs
public ISocketServer CreateSocketServer<TRequestInfo>(IAppServer appServer, ListenerInfo[] listeners, IServerConfig config) where TRequestInfo : IRequestInfo { case(SocketMode.Tcp): return new AsyncSocketServer(appServer, listeners); }
二、启动的过程
var bootstrap = BootstrapFactory.CreateBootstrap(); var result = bootstrap.Start();
supersocketsocketenginedefaultbootstrap.cs
//Initialize servers
foreach (var factoryInfo in workItemFactories)
{
IWorkItem appServer = InitializeAndSetupWorkItem(factoryInfo);
if (appServer == null)
return false;
if (factoryInfo.IsServerManager)
serverManager = appServer;
else if (!(appServer is IsolationAppServer))//No isolation
{
//In isolation mode, cannot check whether is server manager in the factory info loader
if (TypeValidator.IsServerManagerType(appServer.GetType()))
serverManager = appServer;
}
m_AppServers.Add(appServer);
}
public StartResult Start() { foreach (var server in m_AppServers) { if (!server.Start()) {} } }
supersocketsocketbaseappserver.cs
public override bool Start() { if (!base.Start()) return false; }
supersocketsocketbaseappserverbase.cs
m_SocketServer = m_SocketServerFactory.CreateSocketServer<TRequestInfo>(this, m_Listeners, Config);
public virtual bool Start() { if (!m_SocketServer.Start()) { m_StateCode = ServerStateConst.NotStarted; return false; } }
SuperSocketSocketEngineAsyncSocketServer.cs
public override bool Start() { if (!base.Start()) return false; }
supersocketsocketenginesocketserverbase.cs
var listener = CreateListener(ListenerInfos[i]);
d:sourcecodegithubsupersocketsocketengine cpsocketserverbase.cs
protected override ISocketListener CreateListener(ListenerInfo listenerInfo)
{
return new TcpAsyncSocketListener(listenerInfo);
}
d:sourcecodegithubsupersocketsocketengine cpasyncsocketlistener.cs
public TcpAsyncSocketListener(ListenerInfo info)
: base(info)
{
m_ListenBackLog = info.BackLog;
}
d:sourcecodegithubsupersocketsocketenginesocketlistenerbase.cs
protected SocketListenerBase(ListenerInfo info)
{
Info = info;
}
public SocketServerBase(IAppServer appServer, ListenerInfo[] listeners)
{
AppServer = appServer;
IsRunning = false;
ListenerInfos = listeners;
Listeners = new List<ISocketListener>(listeners.Length);
}
public TcpSocketServerBase(IAppServer appServer, ListenerInfo[] listeners)
: base(appServer, listeners)
public virtual bool Start() { if (listener.Start(AppServer.Config)) { } }
SuperSocketSocketEngineTcpAsyncSocketListener.cs
d:sourcecodegithubsupersocketsocketenginesocketlistenerbase.cs
public ListenerInfo Info { get; private set; }
protected SocketListenerBase(ListenerInfo info)
{
Info = info;
}
public override bool Start(IServerConfig config) { m_ListenSocket = new Socket(this.Info.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (!m_ListenSocket.AcceptAsync(acceptEventArg)) ProcessAccept(acceptEventArg); }