From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- .../src/Impostor.Server/Net/MatchmakerService.cs | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Impostor-dev/src/Impostor.Server/Net/MatchmakerService.cs (limited to 'Impostor-dev/src/Impostor.Server/Net/MatchmakerService.cs') diff --git a/Impostor-dev/src/Impostor.Server/Net/MatchmakerService.cs b/Impostor-dev/src/Impostor.Server/Net/MatchmakerService.cs new file mode 100644 index 0000000..bd9a855 --- /dev/null +++ b/Impostor-dev/src/Impostor.Server/Net/MatchmakerService.cs @@ -0,0 +1,57 @@ +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using Impostor.Server.Config; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Impostor.Server.Net +{ + internal class MatchmakerService : IHostedService + { + private readonly ILogger _logger; + private readonly ServerConfig _serverConfig; + private readonly ServerRedirectorConfig _redirectorConfig; + private readonly Matchmaker _matchmaker; + + public MatchmakerService( + ILogger logger, + IOptions serverConfig, + IOptions redirectorConfig, + Matchmaker matchmaker) + { + _logger = logger; + _serverConfig = serverConfig.Value; + _redirectorConfig = redirectorConfig.Value; + _matchmaker = matchmaker; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + var endpoint = new IPEndPoint(IPAddress.Parse(_serverConfig.ResolveListenIp()), _serverConfig.ListenPort); + + await _matchmaker.StartAsync(endpoint); + + _logger.LogInformation( + "Matchmaker is listening on {0}:{1}, the public server ip is {2}:{3}.", + endpoint.Address, + endpoint.Port, + _serverConfig.ResolvePublicIp(), + _serverConfig.PublicPort); + + if (_redirectorConfig.Enabled) + { + _logger.LogWarning(_redirectorConfig.Master + ? "Server redirection is enabled as master, this instance will redirect clients to other nodes." + : "Server redirection is enabled as node, this instance will accept clients."); + } + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + _logger.LogWarning("Matchmaker is shutting down!"); + await _matchmaker.StopAsync(); + } + } +} -- cgit v1.1-26-g67d0