build: bootstrap solution and quality gates (#3)
quality-gate / quality (push) Successful in 58s
quality-gate / quality (push) Successful in 58s
Closes #3
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<AssemblyName>FinalFactory.Rendezvous.Server</AssemblyName>
|
||||
<RootNamespace>FinalFactory.Rendezvous.Server</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../FinalFactory.Rendezvous.Contracts/FinalFactory.Rendezvous.Contracts.csproj" />
|
||||
<PackageReference Include="LiteNetLib" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Net;
|
||||
using FinalFactory.Rendezvous.Server.Transport;
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<UdpMediatorOptions>()
|
||||
.BindConfiguration(UdpMediatorOptions.SectionName)
|
||||
.ValidateDataAnnotations()
|
||||
.Validate(
|
||||
options => IPAddress.TryParse(options.ListenAddress, out _),
|
||||
$"{UdpMediatorOptions.SectionName}:ListenAddress must be an IP address.")
|
||||
.ValidateOnStart();
|
||||
builder.Services.AddSingleton<UdpMediatorService>();
|
||||
builder.Services.AddHostedService(static services => services.GetRequiredService<UdpMediatorService>());
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
app.MapGet("/health/live", static () => Results.Ok(new { status = "live" }));
|
||||
app.MapGet(
|
||||
"/health/ready",
|
||||
static (UdpMediatorService mediator) => mediator.LocalEndpoint is null
|
||||
? Results.StatusCode(StatusCodes.Status503ServiceUnavailable)
|
||||
: Results.Ok(new { status = "ready" }));
|
||||
|
||||
await app.RunAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Entry point marker used by integration-test hosts.
|
||||
/// </summary>
|
||||
public partial class Program;
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace FinalFactory.Rendezvous.Server.Transport;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the UDP endpoint reserved for the Rendezvous mediator.
|
||||
/// </summary>
|
||||
public sealed class UdpMediatorOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration section containing UDP mediator settings.
|
||||
/// </summary>
|
||||
public const string SectionName = "Rendezvous:Udp";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the numeric IP address to bind.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string ListenAddress { get; set; } = "0.0.0.0";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the UDP port. Zero requests an ephemeral port for tests.
|
||||
/// </summary>
|
||||
[Range(0, 65_535)]
|
||||
public int Port { get; set; } = 9050;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FinalFactory.Rendezvous.Server.Transport;
|
||||
|
||||
/// <summary>
|
||||
/// Owns the cancellable UDP socket used by the future NAT mediator.
|
||||
/// </summary>
|
||||
public sealed partial class UdpMediatorService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<UdpMediatorService> _logger;
|
||||
private readonly UdpMediatorOptions _options;
|
||||
private UdpClient? _udpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new UDP mediator service.
|
||||
/// </summary>
|
||||
public UdpMediatorService(
|
||||
IOptions<UdpMediatorOptions> options,
|
||||
ILogger<UdpMediatorService> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bound endpoint after startup completes.
|
||||
/// </summary>
|
||||
public IPEndPoint? LocalEndpoint { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (_udpClient is not null)
|
||||
{
|
||||
throw new InvalidOperationException("The UDP mediator is already running.");
|
||||
}
|
||||
|
||||
IPAddress listenAddress = IPAddress.Parse(_options.ListenAddress);
|
||||
UdpClient udpClient = new(new IPEndPoint(listenAddress, _options.Port));
|
||||
_udpClient = udpClient;
|
||||
IPEndPoint localEndpoint =
|
||||
(IPEndPoint?)udpClient.Client.LocalEndPoint
|
||||
?? throw new InvalidOperationException("The UDP socket did not expose its bound endpoint.");
|
||||
LocalEndpoint = localEndpoint;
|
||||
|
||||
LogMediatorListening(_logger, localEndpoint.Address, localEndpoint.Port);
|
||||
|
||||
return base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await base.StopAsync(cancellationToken).ConfigureAwait(false);
|
||||
_udpClient?.Dispose();
|
||||
_udpClient = null;
|
||||
LocalEndpoint = null;
|
||||
LogMediatorStopped(_logger);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose()
|
||||
{
|
||||
_udpClient?.Dispose();
|
||||
_udpClient = null;
|
||||
LocalEndpoint = null;
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
UdpClient udpClient = _udpClient
|
||||
?? throw new InvalidOperationException("The UDP mediator socket was not initialized.");
|
||||
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
_ = await udpClient.ReceiveAsync(stoppingToken).ConfigureAwait(false);
|
||||
// Bootstrap deliberately emits no UDP response. Protocol handling lands in #11.
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// Expected during normal shutdown.
|
||||
}
|
||||
catch (ObjectDisposedException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// Disposing the socket is the fallback that releases a blocked receive.
|
||||
}
|
||||
finally
|
||||
{
|
||||
LocalEndpoint = null;
|
||||
}
|
||||
}
|
||||
|
||||
[LoggerMessage(
|
||||
EventId = 1,
|
||||
Level = LogLevel.Information,
|
||||
Message = "UDP mediator listening on {ListenAddress}:{ListenPort}")]
|
||||
private static partial void LogMediatorListening(
|
||||
ILogger logger,
|
||||
IPAddress listenAddress,
|
||||
int listenPort);
|
||||
|
||||
[LoggerMessage(
|
||||
EventId = 2,
|
||||
Level = LogLevel.Information,
|
||||
Message = "UDP mediator stopped")]
|
||||
private static partial void LogMediatorStopped(ILogger logger);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Rendezvous": {
|
||||
"Udp": {
|
||||
"ListenAddress": "0.0.0.0",
|
||||
"Port": 9050
|
||||
}
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0": {
|
||||
"LiteNetLib": {
|
||||
"type": "Direct",
|
||||
"requested": "[2.1.4, )",
|
||||
"resolved": "2.1.4",
|
||||
"contentHash": "KWlxvMw3Urpqj9joD96LRiK+LC62pQNs/zkXRJc+rHnxgkGp+vV703xzDrxRmv+V1YhCFfIGzs5nrVWtREIlyA=="
|
||||
},
|
||||
"finalfactory.rendezvous.contracts": {
|
||||
"type": "Project"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user