Compare commits
1 commit
6d9d788a9a
...
7c1fb1dd22
Author | SHA1 | Date | |
---|---|---|---|
7c1fb1dd22 |
9 changed files with 132 additions and 162 deletions
|
@ -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/**
|
||||
|
||||
|
@ -33,7 +33,7 @@ jobs:
|
|||
dotnet build RaiNote.PacketMediator.csproj -c Release
|
||||
|
||||
publish:
|
||||
needs: [build]
|
||||
needs: [ build ]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
@ -60,7 +60,7 @@ jobs:
|
|||
dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://forge.rainote.dev/api/packages/rainote/nuget/index.json
|
||||
|
||||
generate-licences:
|
||||
needs: [build, preprocess]
|
||||
needs: [ build, preprocess ]
|
||||
runs-on: ubuntu-latest
|
||||
container: catthehacker/ubuntu:act-latest@sha256:efe7d859ca6f98a21b3b0471ab48455af597a064d719f65332d0f105aadcad65
|
||||
steps:
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
repos:
|
||||
- repo: local
|
||||
- repo: local
|
||||
hooks:
|
||||
#Use dotnet format already installed on your machine
|
||||
- id: dotnet-format
|
||||
name: dotnet-format
|
||||
language: system
|
||||
entry: dotnet format --include
|
||||
types_or: [c#, vb]
|
||||
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
|
||||
rev: v1.7.1.15
|
||||
types_or: [ c#, vb ]
|
||||
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
|
||||
rev: v1.6.26.11
|
||||
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.14.0
|
||||
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
|
||||
hooks:
|
||||
- id: pretty-format-yaml
|
||||
args: [--autofix, --indent, '2']
|
||||
args: [ --autofix, --indent, '2' ]
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
||||
|
||||
Console.WriteLine("Hello World!");
|
|
@ -1,8 +0,0 @@
|
|||
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
||||
|
||||
namespace PacketMediator.Samples;
|
||||
|
||||
public class Sample
|
||||
{
|
||||
|
||||
}
|
|
@ -15,9 +15,9 @@
|
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
|
|
@ -23,14 +23,12 @@ 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));
|
||||
|
@ -47,11 +45,9 @@ 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 =>
|
||||
|
@ -64,12 +60,9 @@ 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();
|
||||
}
|
||||
|
@ -81,13 +74,11 @@ 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;
|
||||
|
@ -98,32 +89,20 @@ 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));
|
||||
|
||||
|
|
|
@ -13,15 +13,13 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
|||
public PacketDistributorService(IServiceProvider serviceProvider,
|
||||
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
|
||||
{
|
||||
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider,
|
||||
sourcesContainingPackets,
|
||||
sourcesContainingPacketHandlers
|
||||
);
|
||||
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider, sourcesContainingPackets,
|
||||
sourcesContainingPacketHandlers);
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _packetDistributor.DequeuePacketAsync(cancellationToken);
|
||||
return _packetDistributor.DequeuePacketAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
|
@ -34,10 +32,15 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
|||
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
|
||||
}
|
||||
|
||||
public DotNext.Optional<TPacketIdEnum> GetOperationCodeByPacketType(IPacket packet)
|
||||
public TPacketIdEnum GetOperationCodeByPacketType(IPacket packet)
|
||||
{
|
||||
var type = packet.GetType();
|
||||
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
|
||||
return value ?? DotNext.Optional<TPacketIdEnum>.None;
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(type.Name);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,21 +20,20 @@
|
|||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<WarningsAsErrors>Nullable</WarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../README.md" Pack="true" PackagePath="\"/>
|
||||
<PackageReference Include="DotNext" Version="5.3.1"/>
|
||||
<PackageReference Include="DotNext.Metaprogramming" Version="5.3.0"/>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1"/>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.10.48">
|
||||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.9.28">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"sdk": {
|
||||
"version": "8.0.303",
|
||||
"version": "8.0.203",
|
||||
"rollForward": "latestMinor",
|
||||
"allowPrerelease": false
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue