From 894fbbfa5aa7fd8670024cfc424d547383c03cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Fri, 30 Jan 2026 15:31:43 +0100 Subject: [PATCH] Initial commit --- .github/copilot-instructions.md | 111 ++++++++ .gitignore | 482 ++++++++++++++++++++++++++++++++ ChildWorker/ChildWorker.csproj | 14 + ChildWorker/Program.cs | 194 +++++++++++++ CommIpc/Class1.cs | 6 + CommIpc/CommIpc.csproj | 9 + CommIpc/IpcFrame.cs | 15 + CommIpc/IpcJson.cs | 12 + CommIpc/IpcKinds.cs | 16 ++ CommIpc/IpcProtocol.cs | 129 +++++++++ CommIpc/PipeName.cs | 20 ++ CommTester.slnx | 5 + ParentWpf/App.xaml | 9 + ParentWpf/App.xaml.cs | 13 + ParentWpf/AssemblyInfo.cs | 10 + ParentWpf/ChildSession.cs | 129 +++++++++ ParentWpf/MainViewModel.cs | 297 ++++++++++++++++++++ ParentWpf/MainWindow.xaml | 92 ++++++ ParentWpf/MainWindow.xaml.cs | 48 ++++ ParentWpf/NotifyBase.cs | 23 ++ ParentWpf/ParentWpf.csproj | 15 + README.md | 52 ++++ 22 files changed, 1701 insertions(+) create mode 100644 .github/copilot-instructions.md create mode 100644 .gitignore create mode 100644 ChildWorker/ChildWorker.csproj create mode 100644 ChildWorker/Program.cs create mode 100644 CommIpc/Class1.cs create mode 100644 CommIpc/CommIpc.csproj create mode 100644 CommIpc/IpcFrame.cs create mode 100644 CommIpc/IpcJson.cs create mode 100644 CommIpc/IpcKinds.cs create mode 100644 CommIpc/IpcProtocol.cs create mode 100644 CommIpc/PipeName.cs create mode 100644 CommTester.slnx create mode 100644 ParentWpf/App.xaml create mode 100644 ParentWpf/App.xaml.cs create mode 100644 ParentWpf/AssemblyInfo.cs create mode 100644 ParentWpf/ChildSession.cs create mode 100644 ParentWpf/MainViewModel.cs create mode 100644 ParentWpf/MainWindow.xaml create mode 100644 ParentWpf/MainWindow.xaml.cs create mode 100644 ParentWpf/NotifyBase.cs create mode 100644 ParentWpf/ParentWpf.csproj create mode 100644 README.md diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..6809cd8 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,111 @@ + + +- [x] Verify that the copilot-instructions.md file in the .github directory is created. + +- [x] Clarify Project Requirements + + +- [x] Scaffold the Project + + +- [x] Customize the Project + + +- [x] Install Required Extensions + (no additional extensions required) + + +- [x] Compile the Project + (dotnet build successful) + + +- [x] Create and Run Task + (not needed for this prototype) + + +- [ ] Launch the Project + + +- [ ] Ensure Documentation is Complete + + + + +- Work through each checklist item systematically. +- Keep communication concise and focused. +- Follow development best practices. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0808c4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,482 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from `dotnet new gitignore` + +# dotenv files +.env + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +# but not Directory.Build.rsp, as it configures directory-level build defaults +!Directory.Build.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml +.idea/ + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Vim temporary swap files +*.swp diff --git a/ChildWorker/ChildWorker.csproj b/ChildWorker/ChildWorker.csproj new file mode 100644 index 0000000..be7725d --- /dev/null +++ b/ChildWorker/ChildWorker.csproj @@ -0,0 +1,14 @@ + + + + + + + + Exe + net10.0 + enable + enable + + + diff --git a/ChildWorker/Program.cs b/ChildWorker/Program.cs new file mode 100644 index 0000000..ab9e931 --- /dev/null +++ b/ChildWorker/Program.cs @@ -0,0 +1,194 @@ +using System.IO.Pipes; +using CommIpc; + +static (string? pipeName, int? childId) ParseArgs(string[] args) +{ + string? pipe = null; + int? id = null; + + for (int i = 0; i < args.Length; i++) + { + switch (args[i]) + { + case "--pipe" when i + 1 < args.Length: + pipe = args[++i]; + break; + case "--id" when i + 1 < args.Length: + if (int.TryParse(args[++i], out int parsed)) + { + id = parsed; + } + break; + } + } + + return (pipe, id); +} + +var (pipeName, childId) = ParseArgs(args); +if (string.IsNullOrWhiteSpace(pipeName) || childId is null) +{ + Console.Error.WriteLine("Usage: ChildWorker --pipe --id "); + return 2; +} + +using var appCts = new CancellationTokenSource(); +Console.CancelKeyPress += (_, e) => +{ + e.Cancel = true; + appCts.Cancel(); +}; + +using var pipe = new NamedPipeClientStream( + serverName: ".", + pipeName: pipeName, + direction: PipeDirection.InOut, + options: PipeOptions.Asynchronous); + +await pipe.ConnectAsync(appCts.Token); + +var writeLock = new SemaphoreSlim(1, 1); +async Task SendAsync(IpcFrame frame, CancellationToken ct) +{ + await writeLock.WaitAsync(ct); + try + { + await IpcProtocol.WriteFrameAsync(pipe, frame, ct); + } + finally + { + writeLock.Release(); + } +} + +await SendAsync( + new IpcFrame( + Kind: IpcKinds.Hello, + Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value, pid = Environment.ProcessId })), + appCts.Token); + +CancellationTokenSource? workCts = null; +Task? workTask = null; +string? currentWorkCorrelationId = null; + +async Task CancelWorkAsync() +{ + try + { + workCts?.Cancel(); + if (workTask is not null) + { + await workTask; + } + } + catch (OperationCanceledException) + { + // expected + } + finally + { + workCts?.Dispose(); + workCts = null; + workTask = null; + currentWorkCorrelationId = null; + } +} + +async Task StartWorkAsync(string correlationId, int steps, int delayMs, CancellationToken ct) +{ + await CancelWorkAsync(); + + currentWorkCorrelationId = correlationId; + workCts = CancellationTokenSource.CreateLinkedTokenSource(ct); + var wct = workCts.Token; + + workTask = Task.Run(async () => + { + await SendAsync(new IpcFrame( + Kind: IpcKinds.Log, + CorrelationId: correlationId, + Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work started ({steps} steps)." })), wct); + + for (int i = 1; i <= steps; i++) + { + wct.ThrowIfCancellationRequested(); + await Task.Delay(delayMs, wct); + + double percent = (i * 100.0) / steps; + await SendAsync(new IpcFrame( + Kind: IpcKinds.Progress, + CorrelationId: correlationId, + Payload: IpcProtocol.ToJsonElement(new { step = i, total = steps, percent })), wct); + + if (i % Math.Max(1, steps / 5) == 0) + { + await SendAsync(new IpcFrame( + Kind: IpcKinds.Log, + CorrelationId: correlationId, + Payload: IpcProtocol.ToJsonElement(new { level = "debug", message = $"Child {childId}: reached step {i}/{steps}." })), wct); + } + } + + await SendAsync(new IpcFrame( + Kind: IpcKinds.Result, + CorrelationId: correlationId, + Payload: IpcProtocol.ToJsonElement(new { message = $"Child {childId}: work finished." })), wct); + }, wct); +} + +try +{ + while (!appCts.IsCancellationRequested) + { + IpcFrame? frame = await IpcProtocol.ReadFrameAsync(pipe, cancellationToken: appCts.Token); + if (frame is null) + { + break; // parent disconnected + } + + switch (frame.Kind) + { + case IpcKinds.Ping: + await SendAsync(new IpcFrame( + Kind: IpcKinds.Pong, + CorrelationId: frame.CorrelationId, + Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value })), appCts.Token); + break; + + case IpcKinds.StartWork: + { + var payload = IpcProtocol.FromJsonElement(frame.Payload) ?? new StartWorkPayload(); + string corr = frame.CorrelationId ?? Guid.NewGuid().ToString("N"); + await StartWorkAsync(corr, payload.Steps, payload.DelayMs, appCts.Token); + break; + } + + case IpcKinds.CancelWork: + await CancelWorkAsync(); + await SendAsync(new IpcFrame( + Kind: IpcKinds.Log, + CorrelationId: frame.CorrelationId ?? currentWorkCorrelationId, + Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work cancelled." })), appCts.Token); + break; + + default: + await SendAsync(new IpcFrame( + Kind: IpcKinds.Error, + CorrelationId: frame.CorrelationId, + Payload: IpcProtocol.ToJsonElement(new { message = $"Unknown command kind '{frame.Kind}'." })), appCts.Token); + break; + } + } +} +catch (OperationCanceledException) +{ + // shutting down +} +finally +{ + await CancelWorkAsync(); +} + +return 0; + +file sealed record StartWorkPayload(int Steps = 25, int DelayMs = 150); diff --git a/CommIpc/Class1.cs b/CommIpc/Class1.cs new file mode 100644 index 0000000..88c6fc0 --- /dev/null +++ b/CommIpc/Class1.cs @@ -0,0 +1,6 @@ +namespace CommIpc; + +public class Class1 +{ + +} diff --git a/CommIpc/CommIpc.csproj b/CommIpc/CommIpc.csproj new file mode 100644 index 0000000..b760144 --- /dev/null +++ b/CommIpc/CommIpc.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/CommIpc/IpcFrame.cs b/CommIpc/IpcFrame.cs new file mode 100644 index 0000000..b6d2628 --- /dev/null +++ b/CommIpc/IpcFrame.cs @@ -0,0 +1,15 @@ +using System.Text.Json; + +namespace CommIpc; + +/// +/// Single protocol unit sent over the pipe. This is intentionally generic. +/// +/// Transport framing: 4-byte little-endian length prefix + UTF-8 JSON bytes. +/// +public sealed record IpcFrame( + string Kind, + string? CorrelationId = null, + JsonElement? Payload = null, + DateTimeOffset? Timestamp = null +); diff --git a/CommIpc/IpcJson.cs b/CommIpc/IpcJson.cs new file mode 100644 index 0000000..468217c --- /dev/null +++ b/CommIpc/IpcJson.cs @@ -0,0 +1,12 @@ +using System.Text.Json; + +namespace CommIpc; + +internal static class IpcJson +{ + public static readonly JsonSerializerOptions Options = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false + }; +} diff --git a/CommIpc/IpcKinds.cs b/CommIpc/IpcKinds.cs new file mode 100644 index 0000000..c1fd3b0 --- /dev/null +++ b/CommIpc/IpcKinds.cs @@ -0,0 +1,16 @@ +namespace CommIpc; + +public static class IpcKinds +{ + public const string Hello = "hello"; + public const string Ping = "ping"; + public const string Pong = "pong"; + + public const string StartWork = "startWork"; + public const string CancelWork = "cancelWork"; + + public const string Log = "log"; + public const string Progress = "progress"; + public const string Result = "result"; + public const string Error = "error"; +} diff --git a/CommIpc/IpcProtocol.cs b/CommIpc/IpcProtocol.cs new file mode 100644 index 0000000..392bcce --- /dev/null +++ b/CommIpc/IpcProtocol.cs @@ -0,0 +1,129 @@ +using System.Buffers; +using System.Text.Json; + +namespace CommIpc; + +public static class IpcProtocol +{ + // Keep the prototype safe from accidental runaway memory usage. + public const int DefaultMaxFrameBytes = 4 * 1024 * 1024; // 4 MiB + + public static async Task WriteFrameAsync( + Stream stream, + IpcFrame frame, + CancellationToken cancellationToken = default) + { + // Always include a timestamp if the sender didn't set one. + if (frame.Timestamp is null) + { + frame = frame with { Timestamp = DateTimeOffset.UtcNow }; + } + + byte[] json = JsonSerializer.SerializeToUtf8Bytes(frame, IpcJson.Options); + byte[] header = new byte[4]; + int len = json.Length; + header[0] = (byte)(len & 0xFF); + header[1] = (byte)((len >> 8) & 0xFF); + header[2] = (byte)((len >> 16) & 0xFF); + header[3] = (byte)((len >> 24) & 0xFF); + + await stream.WriteAsync(header, cancellationToken).ConfigureAwait(false); + await stream.WriteAsync(json, cancellationToken).ConfigureAwait(false); + await stream.FlushAsync(cancellationToken).ConfigureAwait(false); + } + + public static async Task ReadFrameAsync( + Stream stream, + int maxFrameBytes = DefaultMaxFrameBytes, + CancellationToken cancellationToken = default) + { + byte[] header = ArrayPool.Shared.Rent(4); + try + { + int headerRead = await ReadExactOrEofAsync(stream, header, 0, 4, cancellationToken).ConfigureAwait(false); + if (headerRead == 0) + { + return null; // clean EOF + } + if (headerRead != 4) + { + throw new EndOfStreamException("Unexpected end of stream while reading frame header."); + } + + int len = header[0] + | (header[1] << 8) + | (header[2] << 16) + | (header[3] << 24); + + if (len < 0) + { + throw new InvalidDataException("Negative frame length."); + } + if (len == 0) + { + throw new InvalidDataException("Zero-length frame."); + } + if (len > maxFrameBytes) + { + throw new InvalidDataException($"Frame too large: {len} bytes (limit {maxFrameBytes})."); + } + + byte[] payload = ArrayPool.Shared.Rent(len); + try + { + int read = await ReadExactOrEofAsync(stream, payload, 0, len, cancellationToken).ConfigureAwait(false); + if (read != len) + { + throw new EndOfStreamException("Unexpected end of stream while reading frame payload."); + } + + // Deserialize from the rented buffer slice. + return JsonSerializer.Deserialize(payload.AsSpan(0, len), IpcJson.Options); + } + finally + { + ArrayPool.Shared.Return(payload); + } + } + finally + { + ArrayPool.Shared.Return(header); + } + } + + public static JsonElement ToJsonElement(T value) + { + using JsonDocument doc = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value, IpcJson.Options)); + return doc.RootElement.Clone(); + } + + public static T? FromJsonElement(JsonElement? element) + { + if (element is null) + { + return default; + } + return element.Value.Deserialize(IpcJson.Options); + } + + private static async Task ReadExactOrEofAsync( + Stream stream, + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken) + { + int total = 0; + while (total < count) + { + int n = await stream.ReadAsync(buffer.AsMemory(offset + total, count - total), cancellationToken) + .ConfigureAwait(false); + if (n == 0) + { + return total; // EOF + } + total += n; + } + return total; + } +} diff --git a/CommIpc/PipeName.cs b/CommIpc/PipeName.cs new file mode 100644 index 0000000..5a93fd3 --- /dev/null +++ b/CommIpc/PipeName.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; + +namespace CommIpc; + +public static class PipeName +{ + /// + /// Creates a pipe name that is unique per parent process instance and child id. + /// + public static string ForChild(int childId, int? parentPid = null) + { + parentPid ??= Environment.ProcessId; + return $"CommTester_{parentPid}_{childId}"; + } + + /// + /// Helpful for logs / debugging. + /// + public static string Describe(string pipeName) => $"\\\\.\\pipe\\{pipeName}"; +} diff --git a/CommTester.slnx b/CommTester.slnx new file mode 100644 index 0000000..e1134bc --- /dev/null +++ b/CommTester.slnx @@ -0,0 +1,5 @@ + + + + + diff --git a/ParentWpf/App.xaml b/ParentWpf/App.xaml new file mode 100644 index 0000000..2d58572 --- /dev/null +++ b/ParentWpf/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/ParentWpf/App.xaml.cs b/ParentWpf/App.xaml.cs new file mode 100644 index 0000000..0be1f1a --- /dev/null +++ b/ParentWpf/App.xaml.cs @@ -0,0 +1,13 @@ +using System.Configuration; +using System.Data; +using System.Windows; + +namespace ParentWpf; + +/// +/// Interaction logic for App.xaml +/// +public partial class App : Application +{ +} + diff --git a/ParentWpf/AssemblyInfo.cs b/ParentWpf/AssemblyInfo.cs new file mode 100644 index 0000000..cc29e7f --- /dev/null +++ b/ParentWpf/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/ParentWpf/ChildSession.cs b/ParentWpf/ChildSession.cs new file mode 100644 index 0000000..d3601e4 --- /dev/null +++ b/ParentWpf/ChildSession.cs @@ -0,0 +1,129 @@ +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO.Pipes; +using System.Text.Json; +using CommIpc; + +namespace ParentWpf; + +public sealed class ChildSession : NotifyBase, IAsyncDisposable +{ + private readonly SemaphoreSlim _writeLock = new(1, 1); + private string _status = "Starting"; + private double _progressPercent; + private string? _currentWorkId; + private bool _isConnected; + + public int Id { get; } + public string PipeName { get; } + public string PipePath => CommIpc.PipeName.Describe(PipeName); + + public NamedPipeServerStream Pipe { get; } + public Process Process { get; } + public CancellationTokenSource LifetimeCts { get; } = new(); + + public ObservableCollection Logs { get; } = new(); + + public bool IsConnected + { + get => _isConnected; + set + { + if (SetField(ref _isConnected, value)) + { + OnPropertyChanged(nameof(DisplayName)); + OnPropertyChanged(nameof(StatusLine)); + } + } + } + + public string Status + { + get => _status; + set + { + if (SetField(ref _status, value)) + { + OnPropertyChanged(nameof(DisplayName)); + OnPropertyChanged(nameof(StatusLine)); + } + } + } + + public string DisplayName => $"Child {Id} ({(IsConnected ? "Connected" : "Disconnected")})"; + public string StatusLine => $"{Status} | Work: {(_currentWorkId ?? "-")}"; + + public double ProgressPercent + { + get => _progressPercent; + set => SetField(ref _progressPercent, value); + } + + public string? CurrentWorkId + { + get => _currentWorkId; + set + { + if (SetField(ref _currentWorkId, value)) + { + OnPropertyChanged(nameof(StatusLine)); + } + } + } + + public ChildSession(int id, string pipeName, NamedPipeServerStream pipe, Process process) + { + Id = id; + PipeName = pipeName; + Pipe = pipe; + Process = process; + } + + public async Task SendAsync(IpcFrame frame, CancellationToken cancellationToken) + { + await _writeLock.WaitAsync(cancellationToken); + try + { + await IpcProtocol.WriteFrameAsync(Pipe, frame, cancellationToken); + } + finally + { + _writeLock.Release(); + } + } + + public void AddLog(string line) + { + // keep it from growing without bounds during prototyping + if (Logs.Count > 2000) + { + Logs.RemoveAt(0); + } + Logs.Add(line); + } + + public async ValueTask DisposeAsync() + { + try + { + LifetimeCts.Cancel(); + } + catch { } + + try + { + if (!Process.HasExited) + { + Process.Kill(entireProcessTree: true); + } + } + catch { } + + try { Pipe.Dispose(); } catch { } + try { Process.Dispose(); } catch { } + try { LifetimeCts.Dispose(); } catch { } + try { _writeLock.Dispose(); } catch { } + + await Task.CompletedTask; + } +} diff --git a/ParentWpf/MainViewModel.cs b/ParentWpf/MainViewModel.cs new file mode 100644 index 0000000..7ebc31a --- /dev/null +++ b/ParentWpf/MainViewModel.cs @@ -0,0 +1,297 @@ +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO; +using System.IO.Pipes; +using System.Text.Json; +using System.Windows; +using CommIpc; + +namespace ParentWpf; + +public sealed class MainViewModel : NotifyBase, IAsyncDisposable +{ + private ChildSession? _selectedChild; + private bool _childrenStarted; + private readonly CancellationTokenSource _shutdownCts = new(); + + public ObservableCollection Children { get; } = new(); + + public ChildSession? SelectedChild + { + get => _selectedChild; + set => SetField(ref _selectedChild, value); + } + + public async Task StartChildrenAsync(int count) + { + if (_childrenStarted) + { + return; + } + _childrenStarted = true; + + for (int i = 1; i <= count; i++) + { + await StartChildAsync(i, _shutdownCts.Token); + } + } + + public async Task PingSelectedAsync() + { + if (SelectedChild is null) + { + return; + } + + string corr = Guid.NewGuid().ToString("N"); + await SelectedChild.SendAsync( + new IpcFrame(IpcKinds.Ping, CorrelationId: corr, Payload: IpcProtocol.ToJsonElement(new { from = "parent" })), + SelectedChild.LifetimeCts.Token); + + SelectedChild.AddLog($"[parent] -> ping ({corr})"); + } + + public async Task StartWorkSelectedAsync(int steps = 30, int delayMs = 120) + { + if (SelectedChild is null) + { + return; + } + + string corr = Guid.NewGuid().ToString("N"); + SelectedChild.CurrentWorkId = corr; + SelectedChild.ProgressPercent = 0; + + await SelectedChild.SendAsync( + new IpcFrame( + IpcKinds.StartWork, + CorrelationId: corr, + Payload: IpcProtocol.ToJsonElement(new { steps, delayMs })), + SelectedChild.LifetimeCts.Token); + + SelectedChild.AddLog($"[parent] -> startWork (corr={corr}, steps={steps}, delayMs={delayMs})"); + } + + public async Task CancelWorkSelectedAsync() + { + if (SelectedChild is null) + { + return; + } + + string corr = SelectedChild.CurrentWorkId ?? Guid.NewGuid().ToString("N"); + await SelectedChild.SendAsync( + new IpcFrame(IpcKinds.CancelWork, CorrelationId: corr), + SelectedChild.LifetimeCts.Token); + + SelectedChild.AddLog($"[parent] -> cancelWork (corr={corr})"); + } + + private async Task StartChildAsync(int childId, CancellationToken cancellationToken) + { + string pipeName = PipeName.ForChild(childId, Environment.ProcessId); + var server = new NamedPipeServerStream( + pipeName: pipeName, + direction: PipeDirection.InOut, + maxNumberOfServerInstances: 1, + transmissionMode: PipeTransmissionMode.Byte, + options: PipeOptions.Asynchronous); + + Task waitForConnection = server.WaitForConnectionAsync(cancellationToken); + + Process process = StartChildProcess(childId, pipeName); + var session = new ChildSession(childId, pipeName, server, process); + Children.Add(session); + SelectedChild ??= session; + session.AddLog($"[parent] Starting {process.StartInfo.FileName} {process.StartInfo.Arguments}"); + + await waitForConnection; + session.IsConnected = true; + session.Status = "Connected"; + session.AddLog($"[parent] Connected to {PipeName.Describe(pipeName)}"); + + _ = Task.Run(() => ReceiveLoopAsync(session), _shutdownCts.Token); + } + + private static Process StartChildProcess(int childId, string pipeName) + { + // Prefer spawning the built exe. If not found, fall back to running the dll via dotnet. + string? solutionRoot = TryFindSolutionRoot(AppContext.BaseDirectory); + if (solutionRoot is null) + { + throw new InvalidOperationException("Could not find solution root (CommTester.slnx)."); + } + + string debugExe = Path.Combine(solutionRoot, "ChildWorker", "bin", "Debug", "net10.0", "ChildWorker.exe"); + string releaseExe = Path.Combine(solutionRoot, "ChildWorker", "bin", "Release", "net10.0", "ChildWorker.exe"); + string debugDll = Path.Combine(solutionRoot, "ChildWorker", "bin", "Debug", "net10.0", "ChildWorker.dll"); + string releaseDll = Path.Combine(solutionRoot, "ChildWorker", "bin", "Release", "net10.0", "ChildWorker.dll"); + + string fileName; + string arguments; + if (File.Exists(debugExe)) + { + fileName = debugExe; + arguments = $"--pipe \"{pipeName}\" --id {childId}"; + } + else if (File.Exists(releaseExe)) + { + fileName = releaseExe; + arguments = $"--pipe \"{pipeName}\" --id {childId}"; + } + else if (File.Exists(debugDll)) + { + fileName = "dotnet"; + arguments = $"\"{debugDll}\" --pipe \"{pipeName}\" --id {childId}"; + } + else if (File.Exists(releaseDll)) + { + fileName = "dotnet"; + arguments = $"\"{releaseDll}\" --pipe \"{pipeName}\" --id {childId}"; + } + else + { + // Last-resort: run via project (slower but robust in fresh checkouts). + string csproj = Path.Combine(solutionRoot, "ChildWorker", "ChildWorker.csproj"); + fileName = "dotnet"; + arguments = $"run --project \"{csproj}\" -- --pipe \"{pipeName}\" --id {childId}"; + } + + var psi = new ProcessStartInfo + { + FileName = fileName, + Arguments = arguments, + UseShellExecute = false, + CreateNoWindow = true + }; + + var p = new Process { StartInfo = psi, EnableRaisingEvents = true }; + p.Start(); + return p; + } + + private static string? TryFindSolutionRoot(string baseDirectory) + { + var dir = new DirectoryInfo(baseDirectory); + for (int i = 0; i < 10 && dir is not null; i++) + { + string slnx = Path.Combine(dir.FullName, "CommTester.slnx"); + if (File.Exists(slnx)) + { + return dir.FullName; + } + dir = dir.Parent; + } + return null; + } + + private async Task ReceiveLoopAsync(ChildSession session) + { + try + { + while (!session.LifetimeCts.IsCancellationRequested) + { + IpcFrame? frame = await IpcProtocol.ReadFrameAsync(session.Pipe, cancellationToken: session.LifetimeCts.Token); + if (frame is null) + { + break; + } + + await Application.Current.Dispatcher.InvokeAsync(() => + { + HandleIncomingFrame(session, frame); + }); + } + } + catch (OperationCanceledException) + { + // ignore + } + catch (Exception ex) + { + await Application.Current.Dispatcher.InvokeAsync(() => + { + session.Status = "Error"; + session.AddLog($"[parent] Receive loop error: {ex.Message}"); + }); + } + finally + { + await Application.Current.Dispatcher.InvokeAsync(() => + { + session.IsConnected = false; + session.Status = "Disconnected"; + session.AddLog("[parent] Disconnected."); + }); + } + } + + private static void HandleIncomingFrame(ChildSession session, IpcFrame frame) + { + switch (frame.Kind) + { + case IpcKinds.Hello: + { + var hello = IpcProtocol.FromJsonElement(frame.Payload); + session.AddLog($"[child] hello: id={hello?.ChildId}, pid={hello?.Pid}"); + break; + } + case IpcKinds.Pong: + session.AddLog($"[child] pong ({frame.CorrelationId})"); + break; + + case IpcKinds.Log: + { + var log = IpcProtocol.FromJsonElement(frame.Payload); + session.AddLog($"[child:{log?.Level ?? "info"}] {log?.Message}"); + break; + } + case IpcKinds.Progress: + { + var prog = IpcProtocol.FromJsonElement(frame.Payload); + if (prog is not null) + { + session.ProgressPercent = prog.Percent; + session.Status = $"Working ({prog.Step}/{prog.Total})"; + } + break; + } + case IpcKinds.Result: + { + var res = IpcProtocol.FromJsonElement(frame.Payload); + session.ProgressPercent = 100; + session.Status = "Idle"; + session.AddLog($"[child] result: {res?.Message}"); + break; + } + case IpcKinds.Error: + { + var err = IpcProtocol.FromJsonElement(frame.Payload); + session.Status = "Error"; + session.AddLog($"[child] error: {err?.Message}"); + break; + } + default: + session.AddLog($"[child] {frame.Kind} ({frame.CorrelationId})"); + break; + } + } + + public async ValueTask DisposeAsync() + { + _shutdownCts.Cancel(); + + foreach (var child in Children.ToArray()) + { + try { await child.DisposeAsync(); } catch { } + } + + _shutdownCts.Dispose(); + } + + private sealed record HelloPayload(int ChildId, int Pid); + private sealed record LogPayload(string Level, string Message); + private sealed record ProgressPayload(int Step, int Total, double Percent); + private sealed record ResultPayload(string Message); + private sealed record ErrorPayload(string Message); +} diff --git a/ParentWpf/MainWindow.xaml b/ParentWpf/MainWindow.xaml new file mode 100644 index 0000000..84f8c99 --- /dev/null +++ b/ParentWpf/MainWindow.xaml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + +