continuity/Continuity.AuthServer/Consumers/PacketConsumer.cs

40 lines
1.4 KiB
C#

// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Diagnostics;
using Continuity.AuthServer.Packets;
using JetBrains.Annotations;
using MassTransit;
using Rai.PacketMediator;
using Wonderking.Packets;
namespace Continuity.AuthServer.Consumers;
[UsedImplicitly]
public class PacketConsumer : IConsumer<RawPacket>, IDisposable
{
private readonly ActivitySource _activitySource;
private readonly PacketDistributorService<OperationCode, AuthSession> _distributorService;
public PacketConsumer(PacketDistributorService<OperationCode, AuthSession> distributorService)
{
_distributorService = distributorService;
_activitySource = new ActivitySource(nameof(PacketConsumer));
}
public Task Consume(ConsumeContext<RawPacket> context)
{
using var activity = _activitySource?.StartActivity("PacketConsumption");
activity?.SetTag("PacketId", context.Message.OperationCode.ToString());
activity?.SetTag("SessionId", context.Message.Session.Id.ToString());
activity?.SetTag("PacketSize", context.Message.MessageBody.Length);
return _distributorService.AddPacketAsync(context.Message.MessageBody, context.Message.OperationCode,
context.Message.Session);
}
public void Dispose()
{
_activitySource.Dispose();
GC.SuppressFinalize(this);
}
}