Common networking stuffs
This commit is contained in:
45
Abstract/HandlerTypeManager.cs
Normal file
45
Abstract/HandlerTypeManager.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user