2023-11-21 20:37:50 +00:00
|
|
|
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
|
2023-11-20 18:58:30 +00:00
|
|
|
|
2023-08-12 21:02:59 +00:00
|
|
|
using System.Net;
|
2023-08-09 14:23:41 +00:00
|
|
|
using System.Net.Sockets;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NetCoreServer;
|
|
|
|
|
2024-01-29 07:39:18 +00:00
|
|
|
namespace Continuity.AuthServer.Services;
|
2023-11-19 16:07:28 +00:00
|
|
|
|
2023-08-09 14:23:41 +00:00
|
|
|
public class WonderkingAuthServer : TcpServer, IHostedService
|
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly ILogger<WonderkingAuthServer> _logger;
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2023-08-09 14:23:41 +00:00
|
|
|
|
|
|
|
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> logger,
|
2023-08-10 08:47:35 +00:00
|
|
|
IServiceProvider serviceProvider) : base(address, port)
|
2023-08-09 14:23:41 +00:00
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger = logger;
|
|
|
|
_serviceProvider = serviceProvider;
|
2023-08-12 21:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
Start();
|
2023-08-12 21:02:59 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
DisconnectAll();
|
|
|
|
Stop();
|
2023-08-12 21:02:59 +00:00
|
|
|
return Task.CompletedTask;
|
2023-08-09 14:23:41 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 16:07:28 +00:00
|
|
|
protected override TcpSession CreateSession()
|
|
|
|
{
|
|
|
|
return ActivatorUtilities.CreateInstance<AuthSession>(_serviceProvider, this);
|
|
|
|
}
|
2023-08-09 14:23:41 +00:00
|
|
|
|
|
|
|
protected override void OnStarting()
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Starting");
|
2023-08-09 14:23:41 +00:00
|
|
|
base.OnStarting();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStarted()
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Started");
|
2023-08-09 14:23:41 +00:00
|
|
|
base.OnStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStopping()
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Stopping");
|
2023-08-09 14:23:41 +00:00
|
|
|
base.OnStopping();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStopped()
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Stopped");
|
2023-08-09 14:23:41 +00:00
|
|
|
base.OnStopped();
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:51:30 +00:00
|
|
|
protected override void OnConnected(TcpSession session)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Client connected {Session}", session.Id);
|
2023-11-16 22:51:30 +00:00
|
|
|
base.OnConnected(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDisconnected(TcpSession session)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogInformation("Client disconnected {Session}", session.Id);
|
2023-11-16 22:51:30 +00:00
|
|
|
base.OnDisconnected(session);
|
|
|
|
}
|
|
|
|
|
2023-11-19 16:07:28 +00:00
|
|
|
protected override void OnError(SocketError error)
|
|
|
|
{
|
|
|
|
_logger.LogError("An error has occured {Error}", error);
|
|
|
|
}
|
2023-08-11 09:31:30 +00:00
|
|
|
}
|