continuity/Rai.PacketMediator/IPacketHandler.cs

35 lines
1.2 KiB
C#
Raw Normal View History

// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using System.Diagnostics;
using JetBrains.Annotations;
namespace Rai.PacketMediator;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
2024-02-05 09:43:04 +00:00
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession> where TIncomingPacket : IIncomingPacket
{
2024-02-05 08:02:27 +00:00
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
2024-02-05 11:08:00 +00:00
public Task HandleAsync(TIncomingPacket packet, TSession session, CancellationToken cancellationToken);
2024-02-05 11:08:00 +00:00
async Task<bool> IPacketHandler<TSession>.TryHandleAsync(IIncomingPacket packet, TSession session,
CancellationToken cancellationToken)
{
2024-02-05 09:43:04 +00:00
if (packet is not TIncomingPacket tPacket)
{
return false;
}
using var activity = new ActivitySource(nameof(PacketMediator)).StartActivity(nameof(HandleAsync));
activity?.AddTag("Handler", this.ToString());
activity?.AddTag("Packet", packet.ToString());
2024-02-05 11:08:00 +00:00
await HandleAsync(tPacket, session, cancellationToken);
return true;
}
}
2024-02-05 09:43:04 +00:00
public interface IPacketHandler<in TSession>
{
2024-02-05 11:08:00 +00:00
Task<bool> TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}