using System; using System.Diagnostics; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using PipeMethodCalls; namespace NamedPipes; public partial class NamedPipesServer(string pipeName,Action? logger = null) : ObservableObject,IDebugVisualizer, IDisposable { public bool IsConnected => PipeServer is { State: PipeState.Connected }; private PipeServer? PipeServer {get; set;} private string PipeName { get; } = pipeName; private Action 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(new PipeSerializer(), PipeName, () => this); PipeServer.SetLogger((m) => Trace.WriteLine(m)); } await PipeServer.WaitForConnectionAsync(); } [ObservableProperty] public partial DebugObject? DebugObject { get; private set; } public async Task SetDebugObjectAsync(DebugObject debugObject) { await Task.Delay(1); DebugObject = debugObject; Logger("Received DebugObject of type: " + DebugObject.Type); return true; } public async Task SetMessageAsync(string message) { await Task.Delay(1); Logger("Received message: " + message); return true; } public void Dispose() { GC.SuppressFinalize(this); PipeServer?.Dispose(); PipeServer = null; } }