|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace MyApp; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Minimaler I/O‑Adapter für die 1:1‑Weiterverwendung von Console.ReadLine/WriteLine |
| 10 | +/// in Blazor Server. In der Console‑App zeigen die Delegates auf System.Console, |
| 11 | +/// in Blazor werden sie von der UI (Razor) neu verdrahtet. |
| 12 | +/// </summary> |
| 13 | +public static class IOConsole |
| 14 | +{ |
| 15 | + // ---------- OUTPUT (WriteLine) ---------- |
| 16 | + /// <summary> |
| 17 | + /// Ziel für Ausgaben. In der Console-App: System.Console.WriteLine. |
| 18 | + /// In Blazor: UI fügt Zeilen in eine Liste ein und ruft StateHasChanged(). |
| 19 | + /// </summary> |
| 20 | + public static Action<string?> WriteLineConsumer { get; set; } = s => System.Console.WriteLine(s ?? ""); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Entspricht Console.WriteLine(text). |
| 24 | + /// </summary> |
| 25 | + public static void WriteLine(string? text) => WriteLineConsumer(text); |
| 26 | + |
| 27 | + |
| 28 | + // ---------- INPUT (ReadLine) ---------- |
| 29 | + private static TaskCompletionSource<string?>? _nextLineTcs; |
| 30 | + private static readonly object _lock = new(); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Blockiert so lange, bis die UI eine Zeile via SubmitLine(...) liefert. |
| 34 | + /// Wird synchron aufgerufen, um Console.ReadLine 1:1 zu emulieren. |
| 35 | + /// </summary> |
| 36 | + public static string? ReadLine() |
| 37 | + { |
| 38 | + Task<string?> waitTask; |
| 39 | + |
| 40 | + lock (_lock) |
| 41 | + { |
| 42 | + // Alte TCS (falls vorhanden) sauber verwerfen |
| 43 | + _nextLineTcs?.TrySetCanceled(); |
| 44 | + |
| 45 | + // Neue TCS für die nächste Eingabe |
| 46 | + _nextLineTcs = new TaskCompletionSource<string?>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 47 | + waitTask = _nextLineTcs.Task; |
| 48 | + } |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + // Warten, bis die UI eine Zeile liefert (SubmitLine) |
| 53 | + waitTask.Wait(); |
| 54 | + return waitTask.Result; |
| 55 | + } |
| 56 | + catch (AggregateException ae) when (ae.InnerException is TaskCanceledException) |
| 57 | + { |
| 58 | + // Falls jemand Reset() oder CancelRead() ruft |
| 59 | + return string.Empty; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Wird von der Blazor‑UI (z. B. beim Enter‑Key oder Button) aufgerufen. |
| 65 | + /// Liefert die aktuelle Eingabezeile an das wartende ReadLine(). |
| 66 | + /// </summary> |
| 67 | + public static void SubmitLine(string? line) |
| 68 | + { |
| 69 | + TaskCompletionSource<string?>? tcs; |
| 70 | + lock (_lock) |
| 71 | + { |
| 72 | + tcs = _nextLineTcs; |
| 73 | + _nextLineTcs = null; |
| 74 | + } |
| 75 | + tcs?.TrySetResult(line ?? string.Empty); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Bricht das aktuelle ReadLine()-Warten ab (optional). |
| 80 | + /// </summary> |
| 81 | + public static void CancelRead() |
| 82 | + { |
| 83 | + TaskCompletionSource<string?>? tcs; |
| 84 | + lock (_lock) |
| 85 | + { |
| 86 | + tcs = _nextLineTcs; |
| 87 | + _nextLineTcs = null; |
| 88 | + } |
| 89 | + tcs?.TrySetCanceled(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + // ---------- OPTIONALE HILFSWERTE FÜR DEINEN "f"-PFAD (PFX) ---------- |
| 94 | + /// <summary>Von der UI hochgeladene .pfx-Bytes (für Modus 'f').</summary> |
| 95 | + public static byte[]? UploadedPfxBytes { get; set; } |
| 96 | + |
| 97 | + /// <summary>Optionales PFX‑Passwort (wenn nicht im Code fest verdrahtet).</summary> |
| 98 | + public static string? PfxPassword { get; set; } |
| 99 | + |
| 100 | + |
| 101 | + // ---------- QUALITY-OF-LIFE: RESET ---------- |
| 102 | + /// <summary> |
| 103 | + /// Setzt den Zustand zurück (z. B. beim Neustart des Tools). |
| 104 | + /// </summary> |
| 105 | + public static void Reset() |
| 106 | + { |
| 107 | + CancelRead(); |
| 108 | + UploadedPfxBytes = null; |
| 109 | + PfxPassword = null; |
| 110 | + } |
| 111 | +} |
0 commit comments