基础结构
ISessionBase:app层session接口类
IAppServer:app层应用接口类
IReceiveFilterFactory :接收过滤器工厂接口类
IReceiveFilter:接收过滤器接口类
IConnectionFilter:连接过滤器接口类
ICommandLoader:命令加载器接口类
ICommand:命令接口类
appsession请求处理流程
AppServer启动
分为两步
- Setup
- Start
Setup有好几个重载
public bool Setup(int port);
public bool Setup(IServerConfig config, ISocketServerFactory socketServerFactory = null, IReceiveFilterFactory<TRequestInfo> receiveFilterFactory = null, ILogFactory logFactory = null, IEnumerable<IConnectionFilter> connectionFilters = null, IEnumerable<ICommandLoader<ICommand<TAppSession, TRequestInfo>>> commandLoaders = null);
public bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory = null, IReceiveFilterFactory<TRequestInfo> receiveFilterFactory = null, ILogFactory logFactory = null, IEnumerable<IConnectionFilter> connectionFilters = null, IEnumerable<ICommandLoader<ICommand<TAppSession, TRequestInfo>>> commandLoaders = null);
public bool Setup(string ip, int port, ISocketServerFactory socketServerFactory = null, IReceiveFilterFactory<TRequestInfo> receiveFilterFactory = null, ILogFactory logFactory = null, IEnumerable<IConnectionFilter> connectionFilters = null, IEnumerable<ICommandLoader<ICommand<TAppSession, TRequestInfo>>> commandLoaders = null);
public bool Setup(IRootConfig rootConfig, IServerConfig config, params object[] providers);
public bool Setup(IServerConfig config, params object[] providers);
public bool Setup(string ip, int port, params object[] providers);
Setup(int port)
其调用关系为
启动时序为
Setup
SetupBasic
SetupBasic
:主要做下面三个工作
- 设置线程池参数
if (!m_ThreadPoolConfigured)
{
if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable<int>(),
rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable<int>(),
rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable<int>(),
rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable<int>()))
{
throw new Exception("Failed to configure thread pool!");
}
m_ThreadPoolConfigured = true;
}
- 创建ISocketServerFactory`
if (socketServerFactory == null)
{
var socketServerFactoryType =
Type.GetType("SuperSocket.SocketEngine.SocketServerFactory, SuperSocket.SocketEngine", true);
socketServerFactory = (ISocketServerFactory)Activator.CreateInstance(socketServerFactoryType);
}
m_SocketServerFactory = socketServerFactory;
- 配置编码
if (!string.IsNullOrEmpty(config.TextEncoding))
TextEncoding = Encoding.GetEncoding(config.TextEncoding);
else
TextEncoding = new ASCIIEncoding();
SetupMedium
SetupMedium
:主要配置IReceiveFilterFactory
, IConnectionFilter
和ICommandLoader
private bool SetupMedium(IReceiveFilterFactory<TRequestInfo> receiveFilterFactory, IEnumerable<IConnectionFilter> connectionFilters, IEnumerable<ICommandLoader<ICommand<TAppSession, TRequestInfo>>> commandLoaders)
{
if (receiveFilterFactory != null)
ReceiveFilterFactory = receiveFilterFactory;
if (connectionFilters != null && connectionFilters.Any())
{
if (m_ConnectionFilters == null)
m_ConnectionFilters = new List<IConnectionFilter>();
m_ConnectionFilters.AddRange(connectionFilters);
}
if (commandLoaders != null && commandLoaders.Any())
m_CommandLoaders.AddRange(commandLoaders);
return SetupCommandLoaders(m_CommandLoaders);
}
SetupAdvanced
SetupAdvanced
:主要是配置Security(SSL)以及Listener以及通过命令加载器,命令过滤器将命令添加到容器中
private bool SetupAdvanced(IServerConfig config)
{
if (!SetupSecurity(config))
return false;
if (!SetupListeners(config))
return false;
m_GlobalCommandFilters = GetCommandFilterAttributes(this.GetType());
var discoveredCommands = new Dictionary<string, ICommand<TAppSession, TRequestInfo>>(StringComparer.OrdinalIgnoreCase);
if (!SetupCommands(discoveredCommands))
return false;
OnCommandSetup(discoveredCommands);
return true;
}
private void OnCommandSetup(IDictionary<string, ICommand<TAppSession, TRequestInfo>> discoveredCommands)
{
var commandContainer = new Dictionary<string, CommandInfo<ICommand<TAppSession, TRequestInfo>>>(StringComparer.OrdinalIgnoreCase);
foreach (var command in discoveredCommands.Values)
{
commandContainer.Add(command.Name,
new CommandInfo<ICommand<TAppSession, TRequestInfo>>(command, m_GlobalCommandFilters));
}
Interlocked.Exchange(ref m_CommandContainer, commandContainer);
}
Setup
Setup
:自定义配置交由用户来处理
protected virtual bool Setup(IRootConfig rootConfig, IServerConfig config)
{
return true;
}
SetupFinal
SetupFinal
:配置ReceiveFilterFactory
和SocketServer
private bool SetupFinal()
{
//Check receiveFilterFactory
if (ReceiveFilterFactory == null)
{
ReceiveFilterFactory = CreateDefaultReceiveFilterFactory();
if (ReceiveFilterFactory == null)
{
if (Logger.IsErrorEnabled)
Logger.Error("receiveFilterFactory is required!");
return false;
}
}
var plainConfig = Config as ServerConfig;
if (plainConfig == null)
{
//Using plain config model instead of .NET configuration element to improve performance
plainConfig = new ServerConfig(Config);
if (string.IsNullOrEmpty(plainConfig.Name))
plainConfig.Name = Name;
Config = plainConfig;
}
try
{
m_ServerStatus = new StatusInfoCollection();
m_ServerStatus.Name = Name;
m_ServerStatus.Tag = Name;
m_ServerStatus[StatusInfoKeys.MaxConnectionNumber] = Config.MaxConnectionNumber;
m_ServerStatus[StatusInfoKeys.Listeners] = m_Listeners;
}
catch (Exception e)
{
if (Logger.IsErrorEnabled)
Logger.Error("Failed to create ServerSummary instance!", e);
return false;
}
return SetupSocketServer();
}
private bool SetupSocketServer()
{
try
{
m_SocketServer = m_SocketServerFactory.CreateSocketServer<TRequestInfo>(this, m_Listeners, Config);
return m_SocketServer != null;
}
catch (Exception e)
{
if (Logger.IsErrorEnabled)
Logger.Error(e);
return false;
}
}