Handlers
This commit is contained in:
parent
9b54818d54
commit
9f548cc482
11 changed files with 52 additions and 37 deletions
8
src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs
Normal file
8
src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using nietras.SeparatedValues;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public interface IKeaDhcpLeaseHandler
|
||||
{
|
||||
void Handle(in SepReader.Row row);
|
||||
}
|
||||
8
src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs
Normal file
8
src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using nietras.SeparatedValues;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp4LeaseHandler : IKeaDhcpLeaseHandler
|
||||
{
|
||||
public void Handle(in SepReader.Row row) { }
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
using pdns_dhcp.Options;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
||||
{
|
||||
}
|
||||
10
src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs
Normal file
10
src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using nietras.SeparatedValues;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp6LeaseHandler : IKeaDhcpLeaseHandler
|
||||
{
|
||||
public void Handle(in SepReader.Row row)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
using pdns_dhcp.Options;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
||||
{
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcpLeaseFileReader
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -11,7 +11,8 @@ using pdns_dhcp.Options;
|
|||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public abstract class KeaDhcpLeaseWatcher : IHostedService
|
||||
public sealed class KeaDhcpLeaseWatcher<T> : IHostedService
|
||||
where T : IKeaDhcpLeaseHandler
|
||||
{
|
||||
private static readonly FileStreamOptions LeaseFileStreamOptions = new()
|
||||
{
|
||||
|
|
@ -23,17 +24,20 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
|||
|
||||
private readonly Decoder _decoder;
|
||||
private readonly FileSystemWatcher _fsw;
|
||||
private readonly T _handler;
|
||||
private readonly string _leaseFile;
|
||||
private readonly Pipe _pipe;
|
||||
private Channel<FileSystemEventArgs>? _eventChannel;
|
||||
private Task? _executeTask;
|
||||
private CancellationTokenSource? _stoppingCts;
|
||||
|
||||
protected KeaDhcpServerOptions Options { get; }
|
||||
private KeaDhcpServerOptions Options { get; }
|
||||
|
||||
protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options)
|
||||
public KeaDhcpLeaseWatcher(KeaDhcpServerOptions options, T handler)
|
||||
{
|
||||
Options = options;
|
||||
_handler = handler;
|
||||
|
||||
var leases = options.Leases.AsSpan();
|
||||
if (leases.IsWhiteSpace())
|
||||
{
|
||||
|
|
@ -176,6 +180,7 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
|||
{
|
||||
CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed);
|
||||
writer.Advance(read);
|
||||
await writer.FlushAsync(stoppingToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -218,24 +223,23 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
|||
|
||||
private void OnLeaseChanged(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
var writer = _eventChannel?.Writer;
|
||||
if (writer?.TryWrite(e) != false)
|
||||
if (_eventChannel?.Writer is not { } writer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#pragma warning disable CA2012 // Task is awaited immediately.
|
||||
if (writer.WriteAsync(e) is { IsCompleted: false } task)
|
||||
{
|
||||
try
|
||||
{
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
#pragma warning disable CA2012
|
||||
var task = writer.WriteAsync(e, CancellationToken.None);
|
||||
#pragma warning restore
|
||||
if (task.IsCompleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
|
||||
private void OnLeaseError(object sender, ErrorEventArgs e)
|
||||
{
|
||||
_eventChannel?.Writer.Complete(e.GetException());
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using pdns_dhcp.Kea;
|
||||
using pdns_dhcp.Options;
|
||||
using pdns_dhcp.PowerDns;
|
||||
using pdns_dhcp.Services;
|
||||
|
|
@ -18,4 +19,7 @@ builder.Services.AddHostedService<PowerDnsBackend>();
|
|||
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
|
||||
builder.Services.AddTypedFactory<IPowerDnsFactory>();
|
||||
|
||||
builder.Services.AddTransient<KeaDhcp4LeaseHandler>();
|
||||
builder.Services.AddTransient<KeaDhcp6LeaseHandler>();
|
||||
|
||||
builder.Build().Run();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.Collections.Immutable;
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using pdns_dhcp.Kea;
|
||||
using pdns_dhcp.Options;
|
||||
|
||||
namespace pdns_dhcp.Services;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ namespace pdns_dhcp.Services;
|
|||
|
||||
public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy
|
||||
{
|
||||
KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options);
|
||||
KeaDhcpLeaseWatcher<KeaDhcp4LeaseHandler> KeaDhcp4Watcher(KeaDhcpServerOptions options);
|
||||
|
||||
KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options);
|
||||
KeaDhcpLeaseWatcher<KeaDhcp6LeaseHandler> KeaDhcp6Watcher(KeaDhcpServerOptions options);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
"Leases": "../../ext/kea/dhcp4.leases"
|
||||
},
|
||||
"DHcp6": {
|
||||
"Leases": "../..ext/kea/dhcp4.leases"
|
||||
"Leases": "../../ext/kea/dhcp4.leases"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue