• SuperSocket中的Server的初始化和启动


    一、初始化的过程

    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);
    }  
  • 相关阅读:
    浮动
    关于在windows下同时安装两个mysql,并用Navicat工具同时连接
    关于Springboot项目打包部署运行,命令行运行jar提示没有主清单属性
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.(个人笔记)
    关于IDEA解决默认的javacompile以及Language level自动默认jdk1.5的问题
    你真的精通JavaWeb吗?
    Java8新特性(个人笔记待续)
    Netty(个人笔记待续)
    初识网络编程(1)
    走进shiro
  • 原文地址:https://www.cnblogs.com/chucklu/p/5266463.html
Copyright © 2020-2023  润新知