Compare commits
1 commit
main
...
renovate/m
Author | SHA1 | Date | |
---|---|---|---|
fbeb431656 |
17 changed files with 147 additions and 191 deletions
|
@ -1,11 +1,11 @@
|
||||||
name: Release Rai.PacketMediator
|
name: Release Rai.PacketMediator
|
||||||
run-name: ${{ gitea.actor }} is building the Server application
|
run-name: ${{ gitea.actor }} is building the Server application
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- v*.*.*
|
- 'v*.*.*'
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- .run/**
|
- .run/**
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
preprocess:
|
preprocess:
|
||||||
|
@ -13,75 +13,72 @@ jobs:
|
||||||
outputs:
|
outputs:
|
||||||
sanitized_branch_name: ${{ steps.sanitize.outputs.sanitized_branch_name }}
|
sanitized_branch_name: ${{ steps.sanitize.outputs.sanitized_branch_name }}
|
||||||
steps:
|
steps:
|
||||||
- name: Sanitize branch name
|
- name: Sanitize branch name
|
||||||
id: sanitize
|
id: sanitize
|
||||||
run: echo "::set-output name=sanitized_branch_name::$(echo ${{ github.ref_name }} | sed 's/\//-/g')"
|
run: echo "::set-output name=sanitized_branch_name::$(echo ${{ github.ref_name }} | sed 's/\//-/g')"
|
||||||
|
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Setup dotnet
|
- name: Setup dotnet
|
||||||
uses: https://github.com/actions/setup-dotnet@v3
|
uses: https://github.com/actions/setup-dotnet@v3
|
||||||
with:
|
with:
|
||||||
global-json-file: global.json
|
global-json-file: global.json
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: dotnet restore
|
run: dotnet restore
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
cd RaiNote.PacketMediator
|
dotnet build Rai.PacketMediator.csproj -c Release
|
||||||
dotnet build RaiNote.PacketMediator.csproj -c Release
|
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
needs: [build]
|
needs: [ build ]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Setup dotnet
|
- name: Setup dotnet
|
||||||
uses: https://github.com/actions/setup-dotnet@v3
|
uses: https://github.com/actions/setup-dotnet@v3
|
||||||
with:
|
with:
|
||||||
global-json-file: global.json
|
global-json-file: global.json
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: dotnet restore
|
run: dotnet restore
|
||||||
- name: Extract Version
|
- name: Extract Version
|
||||||
run: |
|
run: |
|
||||||
VERSION=${GITHUB_REF#refs/tags/v}
|
VERSION=${GITHUB_REF#refs/tags/v}
|
||||||
echo "Extracted version is $VERSION"
|
echo "Extracted version is $VERSION"
|
||||||
echo "VERSION=$VERSION" >> $GITHUB_ENV
|
echo "VERSION=$VERSION" >> $GITHUB_ENV
|
||||||
shell: bash
|
shell: bash
|
||||||
- name: Pack nuget package
|
- name: Pack nugets
|
||||||
run: |
|
run: |
|
||||||
cd RaiNote.PacketMediator
|
dotnet build Rai.PacketMediator.csproj -c Release -p:PackageVersion=${{env.VERSION}}
|
||||||
dotnet build RaiNote.PacketMediator.csproj -c Release -p:PackageVersion=${{env.VERSION}}
|
dotnet pack -c Release -p:PackageVersion=${{env.VERSION}} --output .
|
||||||
dotnet pack -c Release -p:PackageVersion=${{env.VERSION}} --output .
|
- name: Push to NuGet
|
||||||
- name: Push to NuGet
|
run: |
|
||||||
run: |
|
dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://forge.rainote.dev/api/packages/rainote/nuget/index.json
|
||||||
cd RaiNote.PacketMediator
|
|
||||||
dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://forge.rainote.dev/api/packages/rainote/nuget/index.json
|
|
||||||
|
|
||||||
generate-licences:
|
generate-licences:
|
||||||
needs: [build, preprocess]
|
needs: [ build, preprocess ]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container: catthehacker/ubuntu:act-latest@sha256:efe7d859ca6f98a21b3b0471ab48455af597a064d719f65332d0f105aadcad65
|
container: catthehacker/ubuntu:act-latest@sha256:be6661897e8b51abab7e30728b387475dcb340da5ebf13a53965ef7c642034ff
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Setup dotnet
|
- name: Setup dotnet
|
||||||
uses: https://github.com/actions/setup-dotnet@v3
|
uses: https://github.com/actions/setup-dotnet@v3
|
||||||
with:
|
with:
|
||||||
dotnet-version: |
|
dotnet-version: |
|
||||||
7.0
|
7.0
|
||||||
8.0
|
8.0
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
dotnet restore
|
dotnet restore
|
||||||
echo "::add-path::$HOME/.dotnet/tools"
|
echo "::add-path::$HOME/.dotnet/tools"
|
||||||
- name: Install nuget-license
|
- name: Install nuget-license
|
||||||
run: dotnet tool install --global dotnet-project-licenses
|
run: dotnet tool install --global dotnet-project-licenses
|
||||||
- name: Export licenses
|
- name: Export licenses
|
||||||
run: dotnet-project-licenses -i . -u --projects-filter projects_ignore_licenses.json -m -j -e -f licenses
|
run: dotnet-project-licenses -i . -u --projects-filter projects_ignore_licenses.json -m -j -e -f licenses
|
||||||
- name: Upload licenses
|
- name: Upload licenses
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: licenses
|
name: licenses
|
||||||
path: licenses
|
path: licenses
|
||||||
retention-days: 31
|
retention-days: 31
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
repos:
|
repos:
|
||||||
- repo: local
|
- repo: local
|
||||||
hooks:
|
hooks:
|
||||||
#Use dotnet format already installed on your machine
|
#Use dotnet format already installed on your machine
|
||||||
- id: dotnet-format
|
- id: dotnet-format
|
||||||
name: dotnet-format
|
name: dotnet-format
|
||||||
language: system
|
language: system
|
||||||
entry: dotnet format --include
|
entry: dotnet format --include
|
||||||
types_or: [c#, vb]
|
types_or: [ c#, vb ]
|
||||||
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
|
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
|
||||||
rev: v1.7.1.15
|
rev: v1.6.26.11
|
||||||
hooks:
|
hooks:
|
||||||
- id: actionlint
|
- id: actionlint
|
||||||
additional_dependencies: [pyflakes>=3.0.1, shellcheck-py>=0.9.0.5]
|
additional_dependencies: [ pyflakes>=3.0.1, shellcheck-py>=0.9.0.5 ]
|
||||||
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
|
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
|
||||||
rev: v2.14.0
|
rev: v2.12.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: pretty-format-yaml
|
- id: pretty-format-yaml
|
||||||
args: [--autofix, --indent, '2']
|
args: [ --autofix, --indent, '2' ]
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\RaiNote.PacketMediator\RaiNote.PacketMediator.csproj" />
|
<ProjectReference Include="..\Rai.PacketMediator\Rai.PacketMediator.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -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>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
<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" Version="2.7.0" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\RaiNote.PacketMediator\RaiNote.PacketMediator.csproj" />
|
<ProjectReference Include="..\Rai.PacketMediator\Rai.PacketMediator.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.0.31903.59
|
VisualStudioVersion = 17.0.31903.59
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RaiNote.PacketMediator", "RaiNote.PacketMediator\RaiNote.PacketMediator.csproj", "{13243A92-DC1E-4DBF-8E2C-13CE9ABFEAD3}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rai.PacketMediator", "Rai.PacketMediator\Rai.PacketMediator.csproj", "{13243A92-DC1E-4DBF-8E2C-13CE9ABFEAD3}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PacketMediator.Samples", "PacketMediator.Samples\PacketMediator.Samples.csproj", "{C20254E6-0C15-43A4-9C4A-1D0B547F7983}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PacketMediator.Samples", "PacketMediator.Samples\PacketMediator.Samples.csproj", "{C20254E6-0C15-43A4-9C4A-1D0B547F7983}"
|
||||||
EndProject
|
EndProject
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
||||||
public interface IBidirectionalPacket : IOutgoingPacket, IIncomingPacket;
|
public interface IBidirectionalPacket : IOutgoingPacket, IIncomingPacket;
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
||||||
public interface IIncomingPacket : IPacket
|
public interface IIncomingPacket : IPacket
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
||||||
public interface IOutgoingPacket : IPacket
|
public interface IOutgoingPacket : IPacket
|
|
@ -1,5 +1,5 @@
|
||||||
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
public interface IPacket;
|
public interface IPacket;
|
|
@ -3,7 +3,7 @@
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
|
||||||
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
|
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
|
|
@ -9,7 +9,7 @@ using DotNext.Linq.Expressions;
|
||||||
using DotNext.Metaprogramming;
|
using DotNext.Metaprogramming;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : Enum
|
public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : Enum
|
||||||
{
|
{
|
||||||
|
@ -23,14 +23,12 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
||||||
public PacketDistributor(IServiceProvider serviceProvider,
|
public PacketDistributor(IServiceProvider serviceProvider,
|
||||||
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
|
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
|
||||||
{
|
{
|
||||||
_channel = Channel.CreateUnbounded<ValueTuple<byte[], TPacketIdEnum, TSession>>(
|
_channel = Channel.CreateUnbounded<ValueTuple<byte[], TPacketIdEnum, TSession>>(new UnboundedChannelOptions
|
||||||
new UnboundedChannelOptions
|
{
|
||||||
{
|
AllowSynchronousContinuations = false,
|
||||||
AllowSynchronousContinuations = false,
|
SingleReader = false,
|
||||||
SingleReader = false,
|
SingleWriter = false
|
||||||
SingleWriter = false
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
var containingPackets = sourcesContainingPackets as Assembly[] ?? sourcesContainingPackets.ToArray();
|
var containingPackets = sourcesContainingPackets as Assembly[] ?? sourcesContainingPackets.ToArray();
|
||||||
var allIncomingPackets = GetAllPackets(containingPackets, typeof(IIncomingPacket));
|
var allIncomingPackets = GetAllPackets(containingPackets, typeof(IIncomingPacket));
|
||||||
var allOutgoingPackets = GetAllPackets(containingPackets, typeof(IOutgoingPacket));
|
var allOutgoingPackets = GetAllPackets(containingPackets, typeof(IOutgoingPacket));
|
||||||
|
@ -44,32 +42,27 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
||||||
new ConcurrentDictionary<TPacketIdEnum, Func<byte[], IIncomingPacket>>();
|
new ConcurrentDictionary<TPacketIdEnum, Func<byte[], IIncomingPacket>>();
|
||||||
_packetHandlersInstantiation = new ConcurrentDictionary<TPacketIdEnum, IPacketHandler<TSession>?>();
|
_packetHandlersInstantiation = new ConcurrentDictionary<TPacketIdEnum, IPacketHandler<TSession>?>();
|
||||||
packetHandlers.ForEach(packetHandlerPair =>
|
packetHandlers.ForEach(packetHandlerPair =>
|
||||||
{
|
{
|
||||||
var packetHandler =
|
var packetHandler =
|
||||||
ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider,
|
ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider,
|
||||||
packetHandlerPair.Value
|
packetHandlerPair.Value);
|
||||||
);
|
_packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler<TSession>);
|
||||||
_packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler<TSession>);
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
allIncomingPackets.ForEach(packetsType =>
|
allIncomingPackets.ForEach(packetsType =>
|
||||||
|
{
|
||||||
|
var lambda = CodeGenerator.Lambda<Func<byte[], IIncomingPacket>>(fun =>
|
||||||
{
|
{
|
||||||
var lambda = CodeGenerator.Lambda<Func<byte[], IIncomingPacket>>(fun =>
|
var argPacketData = fun[0];
|
||||||
{
|
var newPacket = packetsType.Value.New();
|
||||||
var argPacketData = fun[0];
|
|
||||||
var newPacket = packetsType.Value.New();
|
|
||||||
|
|
||||||
var packetVariable = CodeGenerator.DeclareVariable(packetsType.Value, "packet");
|
var packetVariable = CodeGenerator.DeclareVariable(packetsType.Value, "packet");
|
||||||
CodeGenerator.Assign(packetVariable, newPacket);
|
CodeGenerator.Assign(packetVariable, newPacket);
|
||||||
CodeGenerator.Call(packetVariable, nameof(IIncomingPacket.Deserialize), argPacketData);
|
CodeGenerator.Call(packetVariable, nameof(IIncomingPacket.Deserialize), argPacketData);
|
||||||
|
|
||||||
CodeGenerator.Return(packetVariable);
|
CodeGenerator.Return(packetVariable);
|
||||||
}
|
}).Compile();
|
||||||
)
|
tempDeserializationMap.TryAdd(packetsType.Key, lambda);
|
||||||
.Compile();
|
});
|
||||||
tempDeserializationMap.TryAdd(packetsType.Key, lambda);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
|
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
|
||||||
}
|
}
|
||||||
|
@ -81,13 +74,11 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
||||||
{
|
{
|
||||||
var packetsWithId = sourcesContainingPackets.SelectMany(a => a.GetTypes()
|
var packetsWithId = sourcesContainingPackets.SelectMany(a => a.GetTypes()
|
||||||
.Where(type => type is { IsInterface: false, IsAbstract: false } &&
|
.Where(type => type is { IsInterface: false, IsAbstract: false } &&
|
||||||
type.GetInterfaces().Contains(packetType) &&
|
type.GetInterfaces().Contains(packetType)
|
||||||
type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
|
&& type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
|
||||||
)
|
))
|
||||||
)
|
|
||||||
.Select(type =>
|
.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));
|
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.Attribute!.Code, x.Type));
|
||||||
|
|
||||||
return packetsWithId;
|
return packetsWithId;
|
||||||
|
@ -98,32 +89,20 @@ public class PacketDistributor<TPacketIdEnum, TSession> where TPacketIdEnum : En
|
||||||
{
|
{
|
||||||
var packetHandlersWithId = sourcesContainingPacketHandlers.SelectMany(assembly => assembly.GetTypes()
|
var packetHandlersWithId = sourcesContainingPacketHandlers.SelectMany(assembly => assembly.GetTypes()
|
||||||
.Where(t =>
|
.Where(t =>
|
||||||
t is { IsClass: true, IsAbstract: false } &&
|
t is { IsClass: true, IsAbstract: false } && Array.Exists(t
|
||||||
Array.Exists(t
|
.GetInterfaces(), i =>
|
||||||
.GetInterfaces(),
|
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)))
|
||||||
i =>
|
|
||||||
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.Select(packetHandlerType => new
|
.Select(packetHandlerType => new
|
||||||
{
|
{
|
||||||
Type = packetHandlerType,
|
Type = packetHandlerType,
|
||||||
PacketId = packetHandlerType
|
PacketId = packetHandlerType
|
||||||
.GetInterfaces()
|
.GetInterfaces().First(t1 =>
|
||||||
.First(t1 =>
|
t1 is { IsGenericType: true } &&
|
||||||
t1 is { IsGenericType: true } &&
|
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)).GetGenericArguments()
|
||||||
t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)
|
.First(genericType => genericType.GetInterfaces().Any(packetType =>
|
||||||
)
|
packetType == typeof(IPacket)))
|
||||||
.GetGenericArguments()
|
.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>()
|
||||||
.First(genericType => genericType.GetInterfaces()
|
}))
|
||||||
.Any(packetType =>
|
|
||||||
packetType == typeof(IPacket)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.Where(x => x.PacketId != null)
|
.Where(x => x.PacketId != null)
|
||||||
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.PacketId!.Code, x.Type));
|
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.PacketId!.Code, x.Type));
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
||||||
where TPacketIdEnum : Enum
|
where TPacketIdEnum : Enum
|
||||||
|
@ -13,15 +13,13 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
||||||
public PacketDistributorService(IServiceProvider serviceProvider,
|
public PacketDistributorService(IServiceProvider serviceProvider,
|
||||||
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
|
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
|
||||||
{
|
{
|
||||||
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider,
|
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider, sourcesContainingPackets,
|
||||||
sourcesContainingPackets,
|
sourcesContainingPacketHandlers);
|
||||||
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)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
@ -34,10 +32,15 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
|
||||||
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
|
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DotNext.Optional<TPacketIdEnum> GetOperationCodeByPacketType(IPacket packet)
|
public TPacketIdEnum GetOperationCodeByPacketType(IPacket packet)
|
||||||
{
|
{
|
||||||
var type = packet.GetType();
|
var type = packet.GetType();
|
||||||
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
|
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
|
||||||
return value ?? DotNext.Optional<TPacketIdEnum>.None;
|
if (value is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(type.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
// Licensed to Timothy Schenk under the Apache 2.0 License.
|
||||||
|
|
||||||
namespace RaiNote.PacketMediator;
|
namespace Rai.PacketMediator;
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||||
public abstract class PacketIdAttribute<TPacketIdEnum> : Attribute where TPacketIdEnum : Enum
|
public abstract class PacketIdAttribute<TPacketIdEnum> : Attribute where TPacketIdEnum : Enum
|
|
@ -6,35 +6,23 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<PackageVersion>$(PackageVersion)</PackageVersion>
|
<PackageVersion>$(PackageVersion)</PackageVersion>
|
||||||
<PackageProjectUrl>https://forge.rainote.dev/rainote/PacketMediator</PackageProjectUrl>
|
<PackageProjectUrl>https://forge.rainote.dev/rainote/PacketMediator</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseUrl>https://forge.rainote.dev/rainote/PacketMediator/src/branch/main/LICENSE</PackageLicenseUrl>
|
||||||
<RepositoryUrl>https://forge.rainote.dev/rainote/PacketMediator</RepositoryUrl>
|
<RepositoryUrl>https://forge.rainote.dev/rainote/PacketMediator</RepositoryUrl>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
<Version>$(PackageVersion)</Version>
|
<Version>$(PackageVersion)</Version>
|
||||||
<Authors>Timothy (RaiNote) Schenk</Authors>
|
|
||||||
<AssemblyName>RaiNote.PacketMediator</AssemblyName>
|
|
||||||
<RootNamespace>RaiNote.PacketMediator</RootNamespace>
|
|
||||||
<PackageId>RaiNote.PacketMediator</PackageId>
|
|
||||||
<Title>Packet Mediator</Title>
|
|
||||||
<Description>A mediator specifically for packet handling when dispatching to respective packet handlers.</Description>
|
|
||||||
<Product>RaiNote.PacketMediator</Product>
|
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
|
||||||
<WarningsAsErrors>Nullable</WarningsAsErrors>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="../README.md" Pack="true" PackagePath="\"/>
|
<PackageReference Include="DotNext" Version="5.0.2"/>
|
||||||
<PackageReference Include="DotNext" Version="5.12.0"/>
|
<PackageReference Include="DotNext.Metaprogramming" Version="5.0.2"/>
|
||||||
<PackageReference Include="DotNext.Metaprogramming" Version="5.12.0"/>
|
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
|
<PackageReference Include="Meziantou.Analyzer" Version="2.0.145">
|
||||||
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1"/>
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0"/>
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20">
|
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.9.28">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"sdk": {
|
"sdk": {
|
||||||
"version": "8.0.400",
|
"version": "8.0.202",
|
||||||
"rollForward": "latestMinor",
|
"rollForward": "latestMinor",
|
||||||
"allowPrerelease": false
|
"allowPrerelease": false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue