// 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, Stale } /// /// Not thread-safe. Every member, including the Pending and Status getters, must be called under /// lock (Gate). The callers are the chat pump, the tick thread and the render thread. Hold /// the lock only around calls into this class, never around Send or ShellExecute. nowMs must come /// from one monotonic clock (Environment.TickCount64). After a Code reply, Status reads /// "Opening X..."; if the open then fails, the caller must overwrite Status. /// internal sealed class WebLauncher { public readonly object Gate = new(); private const string UnexpectedReply = "DreamWeave sent an unexpected reply."; private WebDestination? _pending; private long _deadlineMs; private long _lastBeginMs; private bool _everBegun; // Forward-only: the moment after which no code from any request begun so far can still be valid. // Nothing lowers it (not Begin, Cancel, Reset, Code, Refused or Expire), so a late code from any // earlier request is still eaten as Stale, whatever happened to the requests since. private long _staleUntilMs = long.MinValue; public WebDestination? Pending => _pending; public string Status { get; private set; } = ""; /// /// A pending request already past its deadline is expired here first. Then: Busy while a request /// is pending; else TooSoon within MinIntervalMs of the last Begin; else Started. /// public LaunchBegin Begin(WebDestination d, long nowMs) { if (_pending != null && nowMs >= _deadlineMs) Expire(); 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; _staleUntilMs = Math.Max(_staleUntilMs, nowMs + LaunchWire.ReplyTimeoutMs + LaunchWire.CodeLifetimeMs); 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; } /// /// A pending request before its deadline takes the line: Code or Refused clears Pending and the /// caller eats it (on Code, message is empty). Otherwise a line TryParse reads as a valid code is /// Stale (eat it, open nothing; dest null, code and message empty) while nowMs is before the /// forward-only stale deadline, and NotOurs after. Any other line outside a request is NotOurs. /// public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message) { dest = null; code = ""; message = ""; if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs; WebDestination? p = _pending; if (p == null || nowMs >= _deadlineMs) return parsedCode.Length > 0 && nowMs < _staleUntilMs ? LaunchLine.Stale : LaunchLine.NotOurs; _pending = null; dest = p; if (parsedCode.Length > 0) { code = parsedCode; Status = "Opening " + p.Label + "..."; return LaunchLine.Code; } message = HasCodeRun(refusal) ? UnexpectedReply : refusal; Status = p.Label + ": DreamWeave said: " + message; 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; Expire(); return p; } /// Login/logout: forget the pending request and the status. The throttle survives. public void Reset() { _pending = null; Status = ""; } private void Expire() { _pending = null; Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within " + (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened."; } /// True when the text holds CodeLength or more consecutive code characters. private static bool HasCodeRun(string s) { int run = 0; foreach (char c in s) { run = LaunchWire.IsCodeChar(c) ? run + 1 : 0; if (run >= LaunchWire.CodeLength) return true; } return false; } }