2024-02-07 16:40:36 +01:00
|
|
|
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
|
2024-02-04 19:52:45 +01:00
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
|
|
namespace Rai.PacketMediator;
|
|
|
|
|
|
|
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
2024-02-07 16:40:36 +01:00
|
|
|
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
|
|
|
|
where TIncomingPacket : IIncomingPacket
|
2024-02-04 19:52:45 +01:00
|
|
|
{
|
2024-02-05 12:08:00 +01:00
|
|
|
async Task<bool> IPacketHandler<TSession>.TryHandleAsync(IIncomingPacket packet, TSession session,
|
|
|
|
CancellationToken cancellationToken)
|
2024-02-04 19:52:45 +01:00
|
|
|
{
|
2024-02-05 10:43:04 +01:00
|
|
|
if (packet is not TIncomingPacket tPacket)
|
2024-02-04 19:52:45 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
using var activity = new ActivitySource(nameof(PacketMediator)).StartActivity(nameof(HandleAsync));
|
2024-02-07 16:40:36 +01:00
|
|
|
activity?.AddTag("Handler", ToString());
|
2024-02-04 19:52:45 +01:00
|
|
|
activity?.AddTag("Packet", packet.ToString());
|
2024-02-05 12:08:00 +01:00
|
|
|
await HandleAsync(tPacket, session, cancellationToken);
|
2024-02-04 19:52:45 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-07 16:40:36 +01:00
|
|
|
|
|
|
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
|
|
|
public Task HandleAsync(TIncomingPacket packet, TSession session, CancellationToken cancellationToken);
|
2024-02-04 19:52:45 +01:00
|
|
|
}
|
|
|
|
|
2024-02-05 10:43:04 +01:00
|
|
|
public interface IPacketHandler<in TSession>
|
2024-02-04 19:52:45 +01:00
|
|
|
{
|
2024-02-05 12:08:00 +01:00
|
|
|
Task<bool> TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken);
|
2024-02-04 19:52:45 +01:00
|
|
|
}
|