Test projects are working.
This commit is contained in:
22
NamedPipes/IVisualizerKontract.cs
Normal file
22
NamedPipes/IVisualizerKontract.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
namespace NamedPipes;
|
||||
|
||||
[Serializable]
|
||||
public struct Frame(Vector3 translation, Quaternion orientation)
|
||||
{
|
||||
public Vector3 Translation => translation;
|
||||
public Quaternion Orientation => orientation;
|
||||
}
|
||||
[Serializable]
|
||||
public class DebugObject
|
||||
{
|
||||
public string? Type { get; set; }
|
||||
public byte[]? Data { get; set; }
|
||||
}
|
||||
public interface IDebugVisualizer
|
||||
{
|
||||
Task<bool> SetDebugObjectAsync(DebugObject debugObject);
|
||||
Task<bool> SetMessageAsync(string message);
|
||||
}
|
||||
54
NamedPipes/NamedPipeClient.cs
Normal file
54
NamedPipes/NamedPipeClient.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using PipeMethodCalls;
|
||||
|
||||
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 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));
|
||||
|
||||
private async Task StartServerAsync()
|
||||
{
|
||||
if (IsConnected) return;
|
||||
PipeClient.SetLogger((m) => Trace.WriteLine(m));
|
||||
var serverAvailable = Process.GetProcessesByName(ServerName).Length > 0;
|
||||
if (!serverAvailable)
|
||||
{
|
||||
var serverPath = ServerLocation + ServerName + ".exe";
|
||||
if (!File.Exists(serverPath)) throw new FileNotFoundException(serverPath);
|
||||
var server = new Process { StartInfo = { FileName = serverPath } };
|
||||
server.Start();
|
||||
}
|
||||
await PipeClient.ConnectAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> SetDebugObjectAsync(DebugObject debugObject)
|
||||
{
|
||||
await StartServerAsync();
|
||||
var result = PipeClient.InvokeAsync(server => server.SetDebugObjectAsync(debugObject)).Result;
|
||||
Logger("NamedPipeClient.SetDebugObject: result = " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> SetMessageAsync(string message)
|
||||
{
|
||||
await StartServerAsync();
|
||||
var result = PipeClient.InvokeAsync(server => server.SetMessageAsync(message)).Result;
|
||||
Logger("NamedPipeClient.SetMessage: result = " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
PipeClient.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
52
NamedPipes/NamedPipeServer.cs
Normal file
52
NamedPipes/NamedPipeServer.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using PipeMethodCalls;
|
||||
|
||||
namespace NamedPipes;
|
||||
public partial class NamedPipesServer(string pipeName,Action<string>? logger = null) : ObservableObject,IDebugVisualizer, IDisposable
|
||||
{
|
||||
public bool IsConnected => PipeServer is { State: PipeState.Connected };
|
||||
private PipeServer<IDebugVisualizer>? PipeServer {get; set;}
|
||||
private string PipeName { get; } = pipeName;
|
||||
private Action<string> Logger { get; } = logger ?? ((m) => Trace.WriteLine(m));
|
||||
|
||||
public async Task WaitForConnectionAsync()
|
||||
{
|
||||
if(IsConnected) return;
|
||||
if (PipeServer == null || PipeServer.State == PipeState.Closed)
|
||||
{
|
||||
PipeServer?.Dispose();
|
||||
PipeServer = new PipeServer<IDebugVisualizer>(new PipeSerializer(), PipeName, () => this);
|
||||
PipeServer.SetLogger((m) => Trace.WriteLine(m));
|
||||
}
|
||||
await PipeServer.WaitForConnectionAsync();
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
public partial DebugObject? DebugObject { get; private set; }
|
||||
|
||||
public async Task<bool> SetDebugObjectAsync(DebugObject debugObject)
|
||||
{
|
||||
await Task.Delay(1);
|
||||
DebugObject = debugObject;
|
||||
Logger("Received DebugObject of type: " + DebugObject.Type);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> SetMessageAsync(string message)
|
||||
{
|
||||
await Task.Delay(1);
|
||||
Logger("Received message: " + message);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
PipeServer?.Dispose();
|
||||
PipeServer = null;
|
||||
}
|
||||
}
|
||||
|
||||
16
NamedPipes/NamedPipes.csproj
Normal file
16
NamedPipes/NamedPipes.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MessagePack" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="PipeMethodCalls" Version="4.0.3" />
|
||||
<PackageReference Include="System.Numerics.Vectors" Version="4.6.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
38
NamedPipes/PipeSerializer.cs
Normal file
38
NamedPipes/PipeSerializer.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using MessagePack;
|
||||
using PipeMethodCalls;
|
||||
|
||||
namespace NamedPipes
|
||||
{
|
||||
public class PipeSerializer : IPipeSerializer
|
||||
{
|
||||
public object? Deserialize(byte[] data, Type type)
|
||||
{
|
||||
if (data.Length == 0) return null;
|
||||
//Log.Trace("PipeSerializer.Deserialize: type = " + type + " , data.length = " + data.Length);
|
||||
var obj = MessagePackSerializer.Deserialize(type, data, MessagePack.Resolvers.ContractlessStandardResolver.Options);
|
||||
//if (obj is DebugObject debugObject)
|
||||
//{
|
||||
// var length = debugObject.Data?.Length ?? 0;
|
||||
// var debugObjectType = debugObject.Type ?? "null";
|
||||
// Log.Trace("PipeSerializer.Deserialize: debugObject.Type = " + debugObjectType + " , debugObject.Data.Length = " + length);
|
||||
//}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public byte[] Serialize(object o)
|
||||
{
|
||||
if (o.GetType().FullName == "System.Threading.Tasks.VoidTaskResult") return [];
|
||||
//if (o is DebugObject debugObject)
|
||||
//{
|
||||
// var length = debugObject.Data?.Length ?? 0;
|
||||
// Log.Trace("PipeSerializer.Deserialize: debugObject.Type = " + debugObject.Type + " , debugObject.Data.Length = " + length);
|
||||
//}
|
||||
|
||||
var bytearray = MessagePackSerializer.Serialize(o.GetType(), o, MessagePack.Resolvers.ContractlessStandardResolver.Options);
|
||||
//var xxx = Deserialize(bytearray, o.GetType());
|
||||
//Log.Trace("PipeSerializer.Serialize: type = " + o.GetType() + " , bytearray.length = " + bytearray.Length);
|
||||
return bytearray;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user