Skip to content

Commit

Permalink
feat: Use MessagePackFormatter by default on net8.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Nov 27, 2023
1 parent a66597d commit 5048d67
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 24 deletions.
11 changes: 10 additions & 1 deletion src/libs/H.Pipes/H.Pipes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
- Supports `PipeAccessRule`'s(see `H.Pipes.AccessControl` nuget package) or more complex code to access using the `PipeServer.PipeStreamInitializeAction` property
</Description>
</PropertyGroup>

<ItemGroup Label="Usings">
<Using Include="H.Formatters.BinaryFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' != 'net8.0' " />
<Using Include="H.Formatters.MessagePackFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' == 'net8.0' " />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
Expand All @@ -30,10 +35,14 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'net8.0' ">
<ProjectReference Include="..\H.Formatters.BinaryFormatter\H.Formatters.BinaryFormatter.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<ProjectReference Include="..\H.Formatters.MessagePack\H.Formatters.MessagePack.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="EventGenerator.Generator" Version="0.13.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/libs/H.Pipes/PipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void OnExceptionOccurred(Exception exception)
/// <param name="pipeName">Name of the server's pipe</param>
/// <param name="serverName">the Name of the server, default is local machine</param>
/// <param name="reconnectionInterval">Default reconnection interval - <see langword="100 ms"/></param>
/// <param name="formatter">Default formatter - <see cref="BinaryFormatter"/></param>
/// <param name="formatter">Default formatter - <see cref="DefaultFormatter"/></param>
public PipeClient(string pipeName, string serverName = ".", TimeSpan? reconnectionInterval = default, IFormatter? formatter = default)
{
PipeName = pipeName;
Expand Down Expand Up @@ -141,7 +141,7 @@ public PipeClient(string pipeName, string serverName = ".", TimeSpan? reconnecti
}
};

Formatter = formatter ?? new BinaryFormatter();
Formatter = formatter ?? new DefaultFormatter();
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/libs/H.Pipes/PipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ private void OnExceptionOccurred(Exception exception)
/// Constructs a new <c>NamedPipeServer</c> object that listens for client connections on the given <paramref name="pipeName"/>.
/// </summary>
/// <param name="pipeName">Name of the pipe to listen on</param>
/// <param name="formatter">Default formatter - <see cref="BinaryFormatter"/></param>
/// <param name="formatter">Default formatter - <see cref="DefaultFormatter"/></param>
public PipeServer(string pipeName, IFormatter? formatter = default)
{
PipeName = pipeName;
Formatter = formatter ?? new BinaryFormatter();
Formatter = formatter ?? new DefaultFormatter();
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/libs/H.Pipes/SingleConnectionPipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void OnExceptionOccurred(Exception exception)
/// <param name="pipeName">Name of the server's pipe</param>
/// <param name="serverName">the Name of the server, default is local machine</param>
/// <param name="reconnectionInterval">Default reconnection interval - <see langword="100 ms"/></param>
/// <param name="formatter">Default formatter - <see cref="BinaryFormatter"/></param>
/// <param name="formatter">Default formatter - <see cref="DefaultFormatter"/></param>
public SingleConnectionPipeClient(string pipeName, string serverName = ".", TimeSpan? reconnectionInterval = default, IFormatter? formatter = default)
{
PipeName = pipeName;
Expand Down Expand Up @@ -141,7 +141,7 @@ public SingleConnectionPipeClient(string pipeName, string serverName = ".", Time
}
};

Formatter = formatter ?? new BinaryFormatter();
Formatter = formatter ?? new DefaultFormatter();
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/libs/H.Pipes/SingleConnectionPipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ private void OnExceptionOccurred(Exception exception)
/// Constructs a new <c>NamedPipeServer</c> object that listens for client connections on the given <paramref name="pipeName"/>.
/// </summary>
/// <param name="pipeName">Name of the pipe to listen on</param>
/// <param name="formatter">Default formatter - <see cref="BinaryFormatter"/></param>
/// <param name="formatter">Default formatter - <see cref="DefaultFormatter"/></param>
public SingleConnectionPipeServer(string pipeName, IFormatter? formatter = default)
{
PipeName = pipeName;
Formatter = formatter ?? new BinaryFormatter();
Formatter = formatter ?? new DefaultFormatter();
}

#endregion
Expand Down
7 changes: 6 additions & 1 deletion src/tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

<Import Project="..\Directory.Build.props" />

<ItemGroup Label="GlobalUsings">
<ItemGroup Label="Usings">
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
<Using Include="FluentAssertions" />
</ItemGroup>

<ItemGroup Label="Usings">
<Using Include="H.Formatters.BinaryFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' != 'net8.0' " />
<Using Include="H.Formatters.MessagePackFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' == 'net8.0' " />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions src/tests/H.Pipes.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public static async Task DataTestAsync<T>(List<T> values, Func<T?, string>? hash
using var cancellationTokenSource = new CancellationTokenSource(timeout ?? TimeSpan.FromMinutes(1));

const string pipeName = "pipe";
await using var server = new PipeServer<T>(pipeName, formatter ?? new BinaryFormatter())
await using var server = new PipeServer<T>(pipeName, formatter ?? new DefaultFormatter())
{
#if NET48
// https://github.com/HavenDV/H.Pipes/issues/6
WaitFreePipe = true,
#endif
};
await using var client = new PipeClient<T>(pipeName, formatter: formatter ?? new BinaryFormatter());
await using var client = new PipeClient<T>(pipeName, formatter: formatter ?? new DefaultFormatter());

await DataTestAsync(server, client, values, hashFunc, cancellationTokenSource.Token);
}
Expand All @@ -123,11 +123,11 @@ public static async Task DataSingleTestAsync<T>(List<T> values, Func<T?, string>
using var cancellationTokenSource = new CancellationTokenSource(timeout ?? TimeSpan.FromMinutes(1));

const string pipeName = "pipe";
await using var server = new SingleConnectionPipeServer<T>(pipeName, formatter ?? new BinaryFormatter())
await using var server = new SingleConnectionPipeServer<T>(pipeName, formatter ?? new DefaultFormatter())
{
WaitFreePipe = true
};
await using var client = new SingleConnectionPipeClient<T>(pipeName, formatter: formatter ?? new BinaryFormatter());
await using var client = new SingleConnectionPipeClient<T>(pipeName, formatter: formatter ?? new DefaultFormatter());

await DataTestAsync(server, client, values, hashFunc, cancellationTokenSource.Token);
}
Expand Down
6 changes: 4 additions & 2 deletions src/tests/H.Pipes.Tests/DataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public async Task Single_TestMessageSize1Kx3_SystemTextJson()
//{
// await BaseTests.BinaryDataSingleTestAsync(1025, 3, new CerasFormatter());
//}


#if !NET8_0_OR_GREATER
[TestMethod]
public async Task TypeTest()
{
Expand All @@ -170,7 +171,7 @@ public async Task TypeTest()
using var registration = cancellationTokenSource.Token.Register(() => completionSource.TrySetCanceled());

const string pipeName = "pipe";
var formatter = new BinaryFormatter();
var formatter = new DefaultFormatter();
await using var server = new PipeServer<object>(pipeName, formatter);
await using var client = new PipeClient<object>(pipeName, formatter: formatter);

Expand All @@ -191,4 +192,5 @@ public async Task TypeTest()

Assert.IsTrue(await completionSource.Task);
}
#endif
}
2 changes: 1 addition & 1 deletion src/tests/H.Pipes.Tests/H.Pipes.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net4.8</TargetFrameworks>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
Expand Down
12 changes: 5 additions & 7 deletions src/tests/H.Pipes.Tests/SerializableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class SerializableTests
private PipeServer<TestCollection>? _server;
private PipeClient<TestCollection>? _client;

private TestCollection? _expectedData;
private int _expectedHash;
private TestCollection? _actualData;
private int _actualHash;

Expand All @@ -36,8 +34,6 @@ public async Task SetUp()
_server = new PipeServer<TestCollection>(PipeName);
_client = new PipeClient<TestCollection>(PipeName);

_expectedData = null;
_expectedHash = 0;
_actualData = null;
_actualHash = 0;

Expand Down Expand Up @@ -97,16 +93,17 @@ private void ServerOnMessageReceived(object? sender, ConnectionMessageEventArgs<

#region Tests

#if !NET8_0_OR_GREATER
[TestMethod]
public async Task TestCircularReferences()
{
_expectedData = new TestCollection();
var _expectedData = new TestCollection();
for (var i = 0; i < 10; i++)
{
var item = new TestItem(i, _expectedData, RandomEnum());
_expectedData.Add(item);
}
_expectedHash = _expectedData.GetHashCode();
var _expectedHash = _expectedData.GetHashCode();

if (_client != null)
{
Expand Down Expand Up @@ -135,7 +132,8 @@ public async Task TestCircularReferences()
$"Item at index {i}'s Parent property should reference the item's parent collection");
}
}

#endif

private TestEnum RandomEnum()
{
var rand = new Random().NextDouble();
Expand Down
1 change: 1 addition & 0 deletions src/tests/H.Pipes.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace H.Pipes.Tests;
[TestClass]
public class Tests
{
[Ignore]
[TestMethod]
public async Task Interrupted()
{
Expand Down

0 comments on commit 5048d67

Please sign in to comment.