hermes-common-library/Abstract/HandlerManager.cs
2024-06-24 22:28:40 +00:00

38 lines
1.0 KiB
C#

using Serilog;
namespace CommonSocketLibrary.Abstract
{
public abstract class HandlerManager<Client, Handler>
{
private readonly IDictionary<int, Handler> _handlers;
protected readonly ILogger _logger;
public IDictionary<int, Handler> Handlers { get => _handlers; }
public HandlerManager(ILogger logger)
{
_handlers = new Dictionary<int, Handler>();
_logger = logger;
}
protected void Add(int op, Handler handler)
{
_handlers.Add(op, handler);
}
public async Task Execute<T>(Client sender, int opcode, T val)
{
if (opcode < 0 || !_handlers.TryGetValue(opcode, out Handler? handler) || handler == null)
{
_logger.Warning("Invalid opcode received: " + opcode);
return;
}
await Execute(sender, handler, val);
}
protected abstract Task Execute<T>(Client sender, Handler handler, T value);
}
}