Compare commits

...

2 commits

Author SHA1 Message Date
2b31505a9b
chore: formatting 2024-08-07 21:57:19 +02:00
2955b62012
chore: update pre-commit 2024-08-07 21:55:55 +02:00
5 changed files with 141 additions and 120 deletions

View file

@ -1,9 +1,9 @@
name: Release Rai.PacketMediator
name: Release Rai.PacketMediator
run-name: ${{ gitea.actor }} is building the Server application
on:
push:
tags:
- 'v*.*.*'
- v*.*.*
paths-ignore:
- .run/**

View file

@ -8,12 +8,12 @@ repos:
entry: dotnet format --include
types_or: [c#, vb]
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
rev: v1.6.26.11
rev: v1.7.1.15
hooks:
- id: actionlint
additional_dependencies: [pyflakes>=3.0.1, shellcheck-py>=0.9.0.5]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.12.0
rev: v2.14.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']

View file

@ -1,3 +1,3 @@
// Licensed to Timothy Schenk under the Apache 2.0 License.
// Licensed to Timothy Schenk under the Apache 2.0 License.
Console.WriteLine("Hello World!");

View file

@ -1,4 +1,4 @@
// Licensed to Timothy Schenk under the Apache 2.0 License.
// Licensed to Timothy Schenk under the Apache 2.0 License.
namespace PacketMediator.Samples;

View file

@ -23,12 +23,14 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
public PacketDistributor(IServiceProvider serviceProvider,
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
{
_channel = Channel.CreateUnbounded<ValueTuple<byte[], TPacketIdEnum, TSession>>(new UnboundedChannelOptions
_channel = Channel.CreateUnbounded<ValueTuple<byte[], TPacketIdEnum, TSession>>(
new UnboundedChannelOptions
{
AllowSynchronousContinuations = false,
SingleReader = false,
SingleWriter = false
});
}
);
var containingPackets = sourcesContainingPackets as Assembly[] ?? sourcesContainingPackets.ToArray();
var allIncomingPackets = GetAllPackets(containingPackets, typeof(IIncomingPacket));
var allOutgoingPackets = GetAllPackets(containingPackets, typeof(IOutgoingPacket));
@ -45,9 +47,11 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
{
var packetHandler =
ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider,
packetHandlerPair.Value);
packetHandlerPair.Value
);
_packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler<TSession>);
});
}
);
allIncomingPackets.ForEach(packetsType =>
{
var lambda = CodeGenerator.Lambda<Func<byte[], IIncomingPacket>>(fun =>
@ -60,9 +64,12 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
CodeGenerator.Call(packetVariable, nameof(IIncomingPacket.Deserialize), argPacketData);
CodeGenerator.Return(packetVariable);
}).Compile();
}
)
.Compile();
tempDeserializationMap.TryAdd(packetsType.Key, lambda);
});
}
);
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
}
@ -74,11 +81,13 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
{
var packetsWithId = sourcesContainingPackets.SelectMany(a => a.GetTypes()
.Where(type => type is { IsInterface: false, IsAbstract: false } &&
type.GetInterfaces().Contains(packetType)
&& type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
))
type.GetInterfaces().Contains(packetType) &&
type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
)
)
.Select(type =>
new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() })
new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() }
)
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.Attribute!.Code, x.Type));
return packetsWithId;
@ -89,20 +98,32 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
{
var packetHandlersWithId = sourcesContainingPacketHandlers.SelectMany(assembly => assembly.GetTypes()
.Where(t =>
t is { IsClass: true, IsAbstract: false } && Array.Exists(t
.GetInterfaces(), i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)))
t is { IsClass: true, IsAbstract: false } &&
Array.Exists(t
.GetInterfaces(),
i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)
)
)
.Select(packetHandlerType => new
{
Type = packetHandlerType,
PacketId = packetHandlerType
.GetInterfaces().First(t1 =>
.GetInterfaces()
.First(t1 =>
t1 is { IsGenericType: true } &&
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)).GetGenericArguments()
.First(genericType => genericType.GetInterfaces().Any(packetType =>
packetType == typeof(IPacket)))
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)
)
.GetGenericArguments()
.First(genericType => genericType.GetInterfaces()
.Any(packetType =>
packetType == typeof(IPacket)
)
)
.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>()
}))
}
)
)
.Where(x => x.PacketId != null)
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.PacketId!.Code, x.Type));