PacketMediator/RaiNote.PacketMediator/IPacketHandler.cs

36 lines
1.2 KiB
C#
Raw Normal View History

2024-02-26 12:18:04 +00:00
// Licensed to Timothy Schenk under the Apache 2.0 License.
2024-02-08 11:55:35 +00:00
using System.Diagnostics;
using JetBrains.Annotations;
2024-04-04 14:32:04 +00:00
namespace RaiNote.PacketMediator;
2024-02-08 11:55:35 +00:00
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
where TIncomingPacket : IIncomingPacket
{
async Task<bool> IPacketHandler<TSession>.TryHandleAsync(IIncomingPacket packet, TSession session,
CancellationToken cancellationToken)
{
if (packet is not TIncomingPacket tPacket)
{
return false;
}
using var activity = new ActivitySource(nameof(PacketMediator)).StartActivity(nameof(HandleAsync));
activity?.AddTag("Handler", ToString());
activity?.AddTag("Packet", packet.ToString());
await HandleAsync(tPacket, session, cancellationToken);
return true;
}
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public Task HandleAsync(TIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}
public interface IPacketHandler<in TSession>
{
Task<bool> TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}