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

可以通过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>
调用示例


2497

被折叠的 条评论
为什么被折叠?



