Files

71 lines
2.1 KiB
C#
Raw Permalink Normal View History

2026-04-01 12:46:48 +02:00
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
{
~NamedPipesServer()
{
Logger(@"~NamedPipesServer called!");
}
2026-04-01 12:46:48 +02:00
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.Connected)
2026-04-01 12:46:48 +02:00
{
PipeServer?.Dispose();
PipeServer = new PipeServer<IDebugVisualizer>(new PipeSerializer(), PipeName, () => this);
PipeServer.SetLogger((m) => Trace.WriteLine(m));
}
Logger("Waiting for client connection...");
await PipeServer.WaitForConnectionAsync().ConfigureAwait(true);
}
public async Task RunAsync()
{
while(true)
{
await WaitForConnectionAsync().ConfigureAwait(true);
Logger("Client connected.");
while (IsConnected)
{
await Task.Delay(100).ConfigureAwait(true);
}
Logger("Client disconnected.");
}
2026-04-01 12:46:48 +02:00
}
[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;
}
}