Skip to content

Commit

Permalink
Merge pull request #76 from Cysharp/dotnet6_add_nats
Browse files Browse the repository at this point in the history
.NET 6 and Nats support
  • Loading branch information
neuecc authored Apr 11, 2022
2 parents ff7f6a6 + a4815f0 commit f0cef8a
Show file tree
Hide file tree
Showing 34 changed files with 521 additions and 72 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
--health-retries 5
ports:
- 6379:6379
nats:
image: nats
ports:
- 4222:4222
steps:
- uses: actions/checkout@v2
- run: dotnet build ./tools/PostBuildUtility/ -c Debug
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ jobs:
--health-retries 5
ports:
- 6379:6379
nats:
image: nats
ports:
- 4222:4222
steps:
- run: echo ${{ needs.update-packagejson.outputs.sha }}
- uses: actions/checkout@v2
Expand All @@ -116,6 +120,7 @@ jobs:
- run: dotnet pack ./src/MessagePipe.Analyzer/MessagePipe.Analyzer.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Redis/MessagePipe.Redis.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Interprocess/MessagePipe.Interprocess.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Nats/MessagePipe.Nats.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
# Store artifacts.
- uses: actions/upload-artifact@v2
with:
Expand Down
20 changes: 19 additions & 1 deletion MessagePipe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Interprocess.Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterprocessServer", "sandbox\InterprocessServer\InterprocessServer.csproj", "{0E645BF9-3464-4856-A624-FEFCF0050220}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Interprocess.Benchmark", "tests\MessagePipe.Interprocess.Benchmark\MessagePipe.Interprocess.Benchmark.csproj", "{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Interprocess.Benchmark", "tests\MessagePipe.Interprocess.Benchmark\MessagePipe.Interprocess.Benchmark.csproj", "{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Nats", "src\MessagePipe.Nats\MessagePipe.Nats.csproj", "{80D9A03B-CDF0-4D52-BF07-2C198707E65C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Nats.Tests", "tests\MessagePipe.Nats.Tests\MessagePipe.Nats.Tests.csproj", "{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -143,6 +147,18 @@ Global
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.Release|Any CPU.Build.0 = Release|Any CPU
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Release|Any CPU.Build.0 = Release|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Release|Any CPU.Build.0 = Release|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -163,6 +179,8 @@ Global
{3B2C3908-E9EB-43F3-9340-3CF5EFBA2F09} = {36546FD6-866F-4809-AFCE-87F7F4201361}
{0E645BF9-3464-4856-A624-FEFCF0050220} = {9813BFC3-7860-4697-A3AF-118BDF710BD0}
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C} = {36546FD6-866F-4809-AFCE-87F7F4201361}
{80D9A03B-CDF0-4D52-BF07-2C198707E65C} = {381F6F79-110B-4CE4-9A49-583046D8C164}
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2} = {36546FD6-866F-4809-AFCE-87F7F4201361}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {138B7AA8-E4C0-46A3-A48B-0D373CAC365D}
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ services:
image: "redis:6.2.1"
ports:
- "6379:6379"
nats:
image: nats
ports:
- 4222:4222
4 changes: 2 additions & 2 deletions sandbox/InterprocessServer/InterprocessServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="3.3.0" />
<PackageReference Include="ConsoleAppFramework" Version="4.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WinBenchmark</Configurations>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="3.3.0" />
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
<PackageReference Include="ZLogger" Version="1.5.0" />
<PackageReference Include="ConsoleAppFramework" Version="4.0.6" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
<PackageReference Include="ZLogger" Version="1.6.1" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/MessagePipe.Analyzer/MessagePipe.Analyzer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
</ItemGroup>

<Target Name="PackBuildOutputs" DependsOnTargets="SatelliteDllsProjectOutputGroup;DebugSymbolsProjectOutputGroup">
Expand Down
10 changes: 5 additions & 5 deletions src/MessagePipe.Interprocess/MessagePipe.Interprocess.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<WarningsAsErrors>true</WarningsAsErrors>
<Configurations>Debug;Release;</Configurations>

Expand All @@ -16,14 +16,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MessagePack" Version="2.2.85" />
<PackageReference Include="System.IO.Pipelines" Version="5.0.1" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<ProjectReference Include="..\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
</ItemGroup>

<!-- Copy files for Unity -->
Expand Down
4 changes: 2 additions & 2 deletions src/MessagePipe.Interprocess/Workers/NamedPipeWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ async void RunReceiveLoop(Stream pipeStream, Func<CancellationToken, Task>? wait
{
var t = AsyncRequestHandlerRegistory.Get(reqTypeName, resTypeName);
var interfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandler"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var coreInterfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandlerCore"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var service = provider.GetRequiredService(interfaceType); // IAsyncRequestHandler<TRequest,TResponse>
var genericArgs = interfaceType.GetGenericArguments(); // [TRequest, TResponse]
var request = MessagePackSerializer.Deserialize(genericArgs[0], message.ValueMemory, options.MessagePackSerializerOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/MessagePipe.Interprocess/Workers/TcpWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ async void RunReceiveLoop(SocketTcpClient client)
{
var t = AsyncRequestHandlerRegistory.Get(reqTypeName, resTypeName);
var interfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandler"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var coreInterfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandlerCore"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var service = provider.GetRequiredService(interfaceType); // IAsyncRequestHandler<TRequest,TResponse>
var genericArgs = interfaceType.GetGenericArguments(); // [TRequest, TResponse]
// Unity IL2CPP does not work(can not invoke nongenerics MessagePackSerializer)
Expand Down
Binary file added src/MessagePipe.Nats/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/MessagePipe.Nats/MessagePipe.Nats.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- NuGet Packaging -->
<PackageTags>pubsub;eventaggregator</PackageTags>
<Description>Nats IDistributedPublisher/Subscriber provider for MessagePipe.</Description>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<None Include="Icon.png" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AlterNats" Version="0.0.2" />
<PackageReference Include="MessagePack" Version="2.3.85" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/MessagePipe.Nats/MessagePipeNatsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MessagePipe.Nats;

public sealed class MessagePipeNatsOptions
{
public NatsConnectionFactory NatsConnectionFactory { get; }

public MessagePipeNatsOptions(NatsConnectionFactory connectionFactory)
{
NatsConnectionFactory = connectionFactory;
}
}
28 changes: 28 additions & 0 deletions src/MessagePipe.Nats/NatsConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using AlterNats;

namespace MessagePipe.Nats;

public class NatsConnectionFactory
{
readonly NatsOptions options;
NatsConnection? connection;

public NatsConnectionFactory() : this(NatsOptions.Default) { }

public NatsConnectionFactory(NatsOptions options)
{
this.options = options;
}

public async ValueTask<NatsConnection> GetConnectionAsync()
{
connection ??= new NatsConnection(options);

if (connection.ConnectionState == NatsConnectionState.Closed)
{
await connection.ConnectAsync();
}

return connection;
}
}
38 changes: 38 additions & 0 deletions src/MessagePipe.Nats/NatsPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using AlterNats;

namespace MessagePipe.Nats;

public sealed class NatsPublisher<TKey, TMessage> : IDistributedPublisher<TKey, TMessage>
{
readonly NatsConnectionFactory connectionFactory;

public NatsPublisher(
NatsConnectionFactory connectionFactory)
{
this.connectionFactory = connectionFactory;
}

public async ValueTask PublishAsync(TKey key, TMessage message, CancellationToken cancellationToken = new CancellationToken())
{
var natsKey = GetNatsKey(key);

if (natsKey == null) throw new ArgumentNullException(nameof(key));

var connection = await connectionFactory.GetConnectionAsync();
await connection.PublishAsync(natsKey.Value, message);
}

NatsKey? GetNatsKey(TKey key)
{
switch (key)
{
case NatsKey natsKey:
return natsKey;
case string s:
return new NatsKey(s);
default:
var k = key?.ToString();
return k != null ? new NatsKey(k) : null;
}
}
}
Loading

0 comments on commit f0cef8a

Please sign in to comment.