continuity/Server/WonderkingAuthServer.cs
2023-08-09 16:23:41 +02:00

70 lines
No EOL
1.9 KiB
C#

using System.Net;
using System.Net.Sockets;
using MassTransit;
using MassTransit.Mediator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NetCoreServer;
namespace Server;
public class WonderkingAuthServer : TcpServer, IHostedService
{
private readonly ILogger<WonderkingAuthServer> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly ILoggerFactory _loggerFactory;
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> logger,
IServiceProvider serviceProvider, ILoggerFactory loggerFactory) : base(address, port)
{
_logger = logger;
_serviceProvider = serviceProvider;
_loggerFactory = loggerFactory;
}
protected override TcpSession CreateSession() => new WonderkingSession(this,
_serviceProvider.GetService<IMediator>() ?? throw new InvalidOperationException(),
_loggerFactory.CreateLogger(nameof(WonderkingSession)));
protected override void OnStarting()
{
_logger.LogInformation("Starting");
base.OnStarting();
}
protected override void OnStarted()
{
_logger.LogInformation("Started");
base.OnStarted();
}
protected override void OnStopping()
{
_logger.LogInformation("Stopping");
base.OnStopping();
}
protected override void OnStopped()
{
_logger.LogInformation("Stopped");
base.OnStopped();
}
protected override void OnError(SocketError error)
{
_logger.LogError("An error has occured {Error}", error);
}
public Task StartAsync(CancellationToken cancellationToken)
{
Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Stop();
return Task.CompletedTask;
}
}