SuperSocket 一个轻量级可扩展 Socket 开发框架

写在前面

不用多说这是迄今为止用过最好用的.Net领域Socket开发框架,从Photon 脱坑之后,一直用它来搭建游戏服务器,相伴多年经历了多个游戏和工控项目的检验,简单易上手,功能应有尽有,强烈推荐。

SuperSocket 官网

可以通过NuGet进行安装 

 

代码实现

using SuperSocket.SocketBase.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperServer
{
    public class SuperBootstrap : BootstrapBase
    {
        public SuperBootstrap()
        {
            
        }

        public IConfigurationSource Setup()
        {
            return SetupBootstrap();
        }

        public void Start()
        {
            BootStrap.Start();
        }

        public void Stop()
        {
            BootStrap.Stop();
        }
    }
}

抽象基类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase;
using System.Configuration;
using System.IO;
using System.Threading;

namespace SuperServer
{
    public abstract class BootstrapBase
    {

        private IBootstrap mBootStrap;

        protected IBootstrap BootStrap
        {
            get { return mBootStrap; }
        }

        public void ClearBootstrap()
        {
            if (mBootStrap != null)
            {
                mBootStrap.Stop();
                (mBootStrap as IDisposable).Dispose();
                mBootStrap = null;
                OnBootstrapCleared();
                GC.Collect();
                GC.WaitForFullGCComplete();
            }
        }


        protected virtual void OnBootstrapCleared()
        {
            
        }
        protected IConfigurationSource CreateBootstrap()
        {
            var configSource = ConfigurationManager.GetSection("superSocket") as IConfigurationSource;
            mBootStrap = BootstrapFactory.CreateBootstrap(configSource);
            return configSource;
        }

        protected IConfigurationSource CreateBootstrap(string configFile)
        {
            IBootstrap newBootstrap;
            var configSrc = CreateBootstrap(configFile, out newBootstrap);
            mBootStrap = newBootstrap;
            return configSrc;
        }

        protected IConfigurationSource CreateBootstrap(string configFile, out IBootstrap newBootstrap)
        {
            var fileMap = new ExeConfigurationFileMap();
            var filePath = Path.Combine(@"Config", configFile);
            if (!File.Exists(filePath))
            {
                newBootstrap = null;
                return null;
            }

            fileMap.ExeConfigFilename = filePath;
            var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var configSource = config.GetSection("superSocket") as IConfigurationSource;

            newBootstrap = BootstrapFactory.CreateBootstrap(configSource);

            return configSource;
        }

        protected IConfigurationSource SetupBootstrap(string configFile, Func<IServerConfig, IServerConfig> configResolver)
        {
            var configSource = CreateBootstrap(configFile);

            if (configResolver != null)
                mBootStrap.Initialize(configResolver);
            else
                mBootStrap.Initialize();

            return configSource;
        }

        protected IConfigurationSource SetupBootstrap(string configFile)
        {
            return SetupBootstrap(configFile, null);
        }

        protected IConfigurationSource StartBootstrap(string configFile, Func<IServerConfig, IServerConfig> configResolver)
        {
            var configSource = SetupBootstrap(configFile, configResolver);
            var result = mBootStrap.Start();
            if (result == StartResult.Success)
                return configSource;
            return null;
        }

        protected IConfigurationSource StartBootstrap(string configFile)
        {
            return StartBootstrap(configFile, null);
        }

        protected IConfigurationSource SetupBootstrap()
        {
            var configSource = CreateBootstrap();
            mBootStrap.Initialize();
            return configSource;
        }
    }
}

App.config 

<configuration>
  <configSections>
    <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
</configSections>
  <superSocket isolation="AppDomain" disablePerformanceDataCollector="true">
    <servers>
      <server name="MasterServer" serverTypeName="MasterApplication"
              ip="Any" port="18888"
              startupOrder="1"
              clearIdleSession="true"
              maxConnectionNumber="1000">
      </server>
      <server name="GameServer_01" serverTypeName="GameApplication"
              ip="Any" port="18889"
              startupOrder="2"
              clearIdleSession="true"
              sendBufferSize="2048"
              maxConnectionNumber="1000">
      </server>
    </servers>
    <serverTypes>
      <add name="MasterApplication" type="SuperServer.MasterServer.MasterApplication, SuperServer"/>
      <add name="GameApplication" type="SuperServer.GameServer.GameApplication, SuperServer"/>
    </serverTypes>
  </superSocket>

调用示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值