Skip to content

Commit

Permalink
Adds DnsNotImplementedClient as a "no op" responder.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Feb 4, 2024
1 parent 1cc6e8e commit 896bb42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/Ae.Dns.Protocol/DnsNotImplementedClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Ae.Dns.Protocol.Enums;
using System.Threading;
using System.Threading.Tasks;

namespace Ae.Dns.Protocol
{
/// <summary>
/// A client which only returns a <see cref="DnsResponseCode.NotImp"/> code.
/// </summary>
public sealed class DnsNotImplementedClient : IDnsClient
{
/// <summary>
/// A static instance of this client which can be used to avoid allocating a new one.
/// </summary>
public static readonly IDnsClient Instance = new DnsNotImplementedClient();

/// <inheritdoc/>
public void Dispose()
{
}

/// <inheritdoc/>
public Task<DnsMessage> Query(DnsMessage query, CancellationToken token = default)
{
return Task.FromResult(query.CreateErrorMessage(DnsResponseCode.NotImp, ToString()));
}

/// <inheritdoc/>
public override string ToString() => nameof(DnsNotImplementedClient);
}
}
8 changes: 3 additions & 5 deletions src/Ae.Dns.Protocol/DnsOperationRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ public DnsOperationRouter(IReadOnlyDictionary<DnsOperationCode, IDnsClient> rout
/// <inheritdoc/>
public async Task<DnsMessage> Query(DnsMessage query, CancellationToken token = default)
{
_routes.TryGetValue(query.Header.OperationCode, out IDnsClient? client);

if (client == null)
if (!_routes.TryGetValue(query.Header.OperationCode, out IDnsClient? operationClient))
{
return query.CreateErrorMessage(DnsResponseCode.NotImp, ToString());
operationClient = DnsNotImplementedClient.Instance;
}

return await client.Query(query, token);
return await operationClient.Query(query, token);
}

/// <inheritdoc/>
Expand Down

0 comments on commit 896bb42

Please sign in to comment.