RynthServers/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs
Daniel Wallace e7af2e2100 RynthDreamWeave: Task 4 review minors
Restore blank line before the pending-destination doc comment, fix the stale-window test comment, and pin that a refused Begin does not extend the stale window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:20:38 -08:00

126 lines
5.1 KiB
C#

// 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 }
/// <summary>
/// Not thread-safe. Every member, including the Pending and Status getters, must be called under
/// <c>lock (Gate)</c>. 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.
/// </summary>
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; } = "";
/// <summary>
/// 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.
/// </summary>
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;
}
/// <summary>The send failed: nothing was asked, so nothing is pending.</summary>
public void Cancel(string status)
{
_pending = null;
Status = status;
}
/// <summary>
/// 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.
/// </summary>
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;
}
/// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary>
public WebDestination? TakeTimedOut(long nowMs)
{
WebDestination? p = _pending;
if (p == null || nowMs < _deadlineMs) return null;
Expire();
return p;
}
/// <summary>Login/logout: forget the pending request and the status. The throttle survives.</summary>
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.";
}
/// <summary>True when the text holds CodeLength or more consecutive code characters.</summary>
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;
}
}