Files
VisualizerExtensionExample/NamedPipes/NamedPipeServer.cs
2026-04-01 12:46:48 +02:00

53 lines
1.6 KiB
C#

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;
}
}