public PacketDispatcher( ILogger logger, IPacketLogger packetLogger, IEnumerable <IPacketHandler> handlers) { _logger = logger; _packetLogger = packetLogger; foreach (var handler in handlers) { if (_handlers.TryGetValue(handler.HandledPacketId, out var existingHandler)) { _logger.Error( "Cannot register packet handler of type {handlerType} with packet id {id} because that id is already registered to handler type {existingHandler}", handler.GetType().Name, handler.HandledPacketId, existingHandler.GetType().Name); throw new InvalidOperationException($"Cannot register packet handler of type {handler.GetType().Name} with packet id {handler.HandledPacketId} because that id is already registered to handler type {existingHandler.GetType().Name}"); } _handlers.Add(handler.HandledPacketId, handler); } }
/// <summary> /// The main packet processing loop. /// </summary> /// <remarks> /// This method illustrates how the dispatcher can be used. For this illustration /// we use an async foreach, to simulate a thread which receives and processes packets /// independent of the rest of the process, and which responds to cancellation tokens /// by terminating any outstanding dispatches and terminating the waits on any /// incoming packets. /// </remarks> static async Task ProcessPacketsAsync( IPacketDispatcher dispatcher, IPacketLogger packetLogger, CancellationToken cancellationToken) { await foreach (var packet in GeneratePacketsAsync(cancellationToken)) { if (cancellationToken.IsCancellationRequested) { return; } try { await dispatcher.DispatchAsync(packet, cancellationToken); } catch (Exception ex) { Guid correlationId = Guid.NewGuid(); Log.Error(ex, "CID:{correlationId}: Exception invoking dispatcher for packet with id {id} and type {type}", correlationId, packet?.Id, packet?.GetType().Name); packetLogger.LogPacket(PacketLogReason.Error, correlationId, packet); } } }
public AriesProtocolLogger(IPacketLogger packetLogger, ISerializationContext context) { this.PacketLogger = packetLogger; this.Context = context; }
public void Log(IPacketLogger packetLogger) { packetLogger.SendingPacket(new ReadOnlySpan <byte>(_owner._buffer, 0, _owner.Length)); }