RynthServers/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs
Daniel Wallace c03a38a3e8 RynthDreamWeave: launch state review fixes
Code-shaped refusals are replaced, self-expiry in Begin, Reset keeps the throttle,
late valid codes are eaten as Stale within the code lifetime, thread contract documented.

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

135 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;
// A request that expired: its late code is eaten (Stale) until the code itself would have expired.
private bool _expired;
private long _expiredDeadlineMs;
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;
_expired = false;
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>
/// NotOurs unless a request is pending, or expired within the code's lifetime. Before the
/// deadline, Code and Refused clear Pending and the caller eats the line. After the deadline,
/// a valid code is Stale (eat it, open nothing; dest null, code empty) until CodeLifetimeMs
/// past the deadline; any other line is NotOurs. On Code, message is empty.
/// </summary>
public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message)
{
dest = null;
code = "";
message = "";
long deadline;
if (_pending != null) deadline = _deadlineMs;
else if (_expired) deadline = _expiredDeadlineMs;
else return LaunchLine.NotOurs;
if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs;
if (nowMs >= deadline)
{
if (parsedCode.Length > 0 && nowMs < deadline + LaunchWire.CodeLifetimeMs) return LaunchLine.Stale;
return LaunchLine.NotOurs;
}
WebDestination p = _pending!;
_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;
_expired = true;
_expiredDeadlineMs = _deadlineMs;
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;
}
}