supersocket的app层源码解析

基础结构

1
0..N
1
0..N
«Interface»
ISessionBase
«Interface»
IAppSession
«Abstract»
AppSession
«Interface»
IAppServer
«Abstract»
AppServerBase
«Abstract»
AppServer
«Interface»
IReceiveFilterFactory
«Interfac»
IReceiveFilter
DefaultReceiveFilterFactory
PolicyReceiveFilterFactory
CountSpliterReceiveFilterFactory
WebSocketProtocol
TerminatorReceiveFilterFactory
CountSpliterReceiveFilter
«Abstract»
FixedSizeReceiveFilter
WebSocketDataFrameReceiveFilter
SwitchReceiveFilter
«Abstract»
BeginEndMarkReceiveFilter
«Abstract»
FixedHeaderReceiveFilter
PolicyReceiveFilter
«Abstract»
TerminatorReceiveFilter
«Abstract»
HttpReceiveFilterBase
HttpReceiveFilter
«interface»
IConnectionFilter
«intrface»
ICommandLoader
«Abstract»
CommandLoaderBase
ReflectCommandLoader
«Interface»
ICommand
«Abstract»
CommandBase
«Abstract»
StringCommandBase
ISocketServerFactory

ISessionBase:app层session接口类
IAppServer:app层应用接口类
IReceiveFilterFactory :接收过滤器工厂接口类
IReceiveFilter:接收过滤器接口类
IConnectionFilter:连接过滤器接口类
ICommandLoader:命令加载器接口类
ICommand:命令接口类

appsession请求处理流程

client AppSession IReceiveFilter IAppServer ICommand ProcessRequest FilterRequest Filter ExecuteCommand ExecuteCommand m_requestHandler alt [m_requestHandler = null] client AppSession IReceiveFilter IAppServer ICommand

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(int port)
Setup(string ip, int port, params object[] providers)
Setup(IServerConfig config, params object[] providers)
Setup(IRootConfig rootConfig, IServerConfig config, params object[] providers)

启动时序为

user AppServer ISocketServer Setup SetupBasic SetupMedium SetupAdvanced Setup SetupFinal Start Start OnStartup OnStarted user AppServer ISocketServer

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, IConnectionFilterICommandLoader

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:配置ReceiveFilterFactorySocketServer

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值