125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
using CommandLine;
|
|
using ControlCenter.NamedPipe.Model;
|
|
using MatchServer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using ServerBase;
|
|
using ServerCore;
|
|
|
|
public class ServiceProviderHelper
|
|
{
|
|
public static IServiceProvider? Services { get; private set; }
|
|
|
|
public static void SetServiceProvider(IServiceProvider serviceProvider)
|
|
{
|
|
Services = serviceProvider;
|
|
}
|
|
}
|
|
|
|
public interface IServerInfoProvider
|
|
{
|
|
Task<List<ServerInfo>> GetServerInfosByServerType(ServerType serverType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 매칭 로직을 서비스로 실행한다.
|
|
/// </summary>
|
|
public class MatchServerService : IServerInfoProvider
|
|
{
|
|
private readonly NLog.Logger _logger;
|
|
|
|
public class CmdOptions
|
|
{
|
|
[Option("config", Default = "ServerConfig.json")]
|
|
public string Config { get; set; } = "ServerConfig.json";
|
|
}
|
|
|
|
private MatchServerLogic? _serverLogic;
|
|
private readonly CancellationTokenSource _onStartCts = new();
|
|
public MatchServerLogic MatchServerLogic => _serverLogic!;
|
|
|
|
public MatchServerService()
|
|
{
|
|
_logger = Log.getLogger();
|
|
_logger.info($"{nameof(MatchServerService)} Start" );
|
|
}
|
|
|
|
public async Task<List<ServerInfo>> GetServerInfosByServerType(ServerType serverType)
|
|
{
|
|
var (result, serverInfos) = await MatchServerLogic.getServerInfosByServerType(serverType);
|
|
return result.isFail() ? [] : serverInfos;
|
|
}
|
|
|
|
public async Task<int> Run(string[] args)
|
|
{
|
|
var result = new Result();
|
|
|
|
_logger.info($"GameServer Run");
|
|
|
|
var name = nameof(MatchServerService);
|
|
|
|
try
|
|
{
|
|
result = await Parser.Default.ParseArguments<CmdOptions>(args).MapResult(
|
|
async (CmdOptions opts) => await RunServer(opts), errors => Task.FromResult(result)
|
|
);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var err_msg = $"Exception !!!, {name}.runServer() : message:{e}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
}
|
|
finally
|
|
{
|
|
if (result.isFail())
|
|
{
|
|
Log.getLogger().error($"{name} finally error !!! - {result.toBasicString()}");
|
|
}
|
|
}
|
|
|
|
_logger.info($"{name} terminated");
|
|
Log.shutdown();
|
|
return 0;
|
|
}
|
|
|
|
private async Task<Result> RunServer(CmdOptions argCmdOptions)
|
|
{
|
|
var result = new Result();
|
|
|
|
var key_options = new Dictionary<string, string> { { "config", argCmdOptions.Config }, };
|
|
|
|
var configuration = new ConfigurationBuilder().AddInMemoryCollection(key_options!).Build();
|
|
NullReferenceCheckHelper.throwIfNull(configuration, () => $"configuration is null !!!");
|
|
var server_config = new ServerConfig();
|
|
|
|
_serverLogic = new MatchServerLogic(server_config);
|
|
_serverLogic.setServerType(nameof(ServerType.Match));
|
|
_serverLogic.setConfiguration(configuration);
|
|
_serverLogic.OnServerStart = () =>
|
|
{
|
|
_onStartCts.Cancel();
|
|
};
|
|
|
|
var namedOptionBuilder = new NamedPipeClientOptionBuilder();
|
|
namedOptionBuilder.setIP(NetworkHelper.getEthernetLocalIPv4())
|
|
.setPort(0)
|
|
.setType(nameof(ServerType.Match))
|
|
.setServiceCategory(nameof(ServiceCategory.Caliverse));
|
|
|
|
result = await _serverLogic.onRunServer(namedOptionBuilder);
|
|
if (result.isFail())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task WaitOnStart()
|
|
{
|
|
while (!_onStartCts.IsCancellationRequested)
|
|
{
|
|
await Task.Delay(10);
|
|
}
|
|
}
|
|
}
|