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

45 lines
1.5 KiB
C#

using Serilog;
namespace CommonSocketLibrary.Abstract
{
public abstract class HandlerTypeManager<Client, Handler>
{
private readonly IDictionary<int, Type> _types;
public IDictionary<int, Type> HandlerTypes { get => _types; }
protected readonly ILogger _logger;
public HandlerTypeManager(ILogger logger, HandlerManager<Client, Handler> handlers)
{
_types = new Dictionary<int, Type>();
_logger = logger;
GenerateHandlerTypes(handlers.Handlers);
}
private void GenerateHandlerTypes(IDictionary<int, Handler> handlers)
{
foreach (var entry in handlers)
{
if (entry.Value == null)
{
_logger.Error($"Failed to link websocket handler #{entry.Key} due to null value.");
continue;
}
var type = entry.Value.GetType();
var target = FetchMessageType(type);
if (target == null)
{
_logger.Error($"Failed to link websocket handler #{entry.Key} due to no match for {target}.");
continue;
}
_types.Add(entry.Key, target);
_logger.Debug($"Linked websocket handler #{entry.Key} to type {target.AssemblyQualifiedName}.");
}
}
protected abstract Type? FetchMessageType(Type handlerType);
}
}