38 lines
1.0 KiB
C#
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);
|
|
}
|
|
} |