// The launch request state machine for the website buttons: one "/weblogin" at a time, a 10 s // reply window, and a status line for the card. Pure (no engine calls), so RynthServerPack.Tests // compiles it directly. The code is handed to the caller through OnChatLine and goes nowhere // else: never into Status, never into the message, never into a field. using System; namespace RynthCore.Plugin.RynthDreamWeave; internal enum LaunchBegin { Started, Busy, TooSoon } internal enum LaunchLine { NotOurs, Code, Refused } internal sealed class WebLauncher { /// Held by the chat pump and the render thread around every call. public readonly object Gate = new(); private WebDestination? _pending; private long _deadlineMs; private long _lastBeginMs; private bool _everBegun; public WebDestination? Pending => _pending; public string Status { get; private set; } = ""; /// Busy while a request is pending (checked first); else TooSoon within MinIntervalMs of the last Begin. public LaunchBegin Begin(WebDestination d, long nowMs) { if (_pending != null) return LaunchBegin.Busy; if (_everBegun && nowMs - _lastBeginMs < LaunchWire.MinIntervalMs) return LaunchBegin.TooSoon; _pending = d; _everBegun = true; _lastBeginMs = nowMs; _deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs; Status = "Asking DreamWeave for a login code for " + d.Label + "..."; return LaunchBegin.Started; } /// The send failed: nothing was asked, so nothing is pending. public void Cancel(string status) { _pending = null; Status = status; } /// /// NotOurs unless a request is pending and the deadline has not passed. Code and Refused clear /// Pending and the caller eats the line. On Code, is empty and the /// code is only in . /// public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message) { dest = null; code = ""; message = ""; WebDestination? p = _pending; if (p == null || nowMs >= _deadlineMs) return LaunchLine.NotOurs; if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs; _pending = null; dest = p; if (parsedCode.Length > 0) { code = parsedCode; Status = "Opening " + p.Label + "..."; return LaunchLine.Code; } message = refusal; Status = p.Label + ": DreamWeave said: " + refusal; return LaunchLine.Refused; } /// The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise. public WebDestination? TakeTimedOut(long nowMs) { WebDestination? p = _pending; if (p == null || nowMs < _deadlineMs) return null; _pending = null; Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within " + (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened."; return p; } /// Login/logout: forget the request and the status. public void Reset() { _pending = null; Status = ""; _everBegun = false; } }