use own PipeSerializer

SendToVisualizerAsync uses await
This commit is contained in:
Matthias Heil
2026-04-08 08:20:33 +02:00
parent 96bb165f68
commit 49e94b9b45
6 changed files with 36 additions and 12 deletions
+2 -4
View File
@@ -1,6 +1,4 @@
using PipeMethodCalls;
using PipeMethodCalls.NetJson;
using System;
using System.Diagnostics;
using System.IO;
@@ -11,7 +9,7 @@ namespace NamedPipes;
public class NamedPipeClient (string pipeName,string? serverLocation = null, string? serverName = null,Action<string>? logger = null): IDebugVisualizer, IDisposable
{
private bool IsConnected => PipeClient.State == PipeState.Connected;
private PipeClient<IDebugVisualizer> PipeClient { get; } = new(new NetJsonPipeSerializer(), pipeName);
private PipeClient<IDebugVisualizer> PipeClient { get; } = new(new PipeSerializer(), pipeName);
private string ServerLocation { get; } = serverLocation ?? Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Visual Studio 2026\Visualizers\Server\NrxVisualizer\";
private string ServerName { get; } = serverName ?? "Num.Roto.Nrx.VisualizerServer";
private Action<string> Logger { get;} = logger ??((m) => Trace.WriteLine(m));
@@ -32,7 +30,7 @@ public class NamedPipeClient (string pipeName,string? serverLocation = null, st
try
{
await PipeClient.ConnectAsync().ConfigureAwait(true);
Logger("NamedPipeClient.StartServerAsync succeded.");
Logger("NamedPipeClient.StartServerAsync succeeded.");
return;
}
catch(Exception e)
+1 -2
View File
@@ -1,7 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using PipeMethodCalls;
using PipeMethodCalls.NetJson;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
@@ -25,7 +24,7 @@ public partial class NamedPipesServer(string pipeName,Action<string>? logger = n
if (PipeServer == null || PipeServer.State != PipeState.Connected)
{
PipeServer?.Dispose();
PipeServer = new PipeServer<IDebugVisualizer>(new NetJsonPipeSerializer(), PipeName, () => this);
PipeServer = new PipeServer<IDebugVisualizer>(new PipeSerializer(), PipeName, () => this);
PipeServer.SetLogger((m) => Trace.WriteLine(m));
}
Logger("Waiting for client connection...");
+2 -1
View File
@@ -9,7 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="PipeMethodCalls.NetJson" Version="3.0.0" />
<PackageReference Include="PipeMethodCalls" Version="3.0.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.6.1" />
<PackageReference Include="System.Text.Json" Version="10.0.5" />
</ItemGroup>
</Project>
+27
View File
@@ -0,0 +1,27 @@
using PipeMethodCalls;
using System;
using System.IO;
using System.Text.Json;
namespace NamedPipes;
/// <summary>
/// Serializes pipe method call information with System.Text.Json.
/// </summary>
public class PipeSerializer : IPipeSerializer
{
public object Deserialize(byte[] data, Type type)
{
return JsonSerializer.Deserialize(data, type) ?? throw new InvalidDataException($"Can not deserialize to type: {type} ");
}
public byte[] Serialize(object o)
{
using (var memoryStream = new MemoryStream())
using (var utf8JsonWriter = new Utf8JsonWriter(memoryStream))
{
JsonSerializer.Serialize(utf8JsonWriter, o);
return memoryStream.ToArray();
}
}
}