diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs
index b0fea6d..aba5a34 100644
--- a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs
+++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs
@@ -17,6 +17,8 @@ internal static class LaunchWire
private const string SuccessStem = "[WEB] Launch code";
public const int CodeLength = 43;
public const long ReplyTimeoutMs = 10_000, MinIntervalMs = 3_000;
+ /// How long a code stays valid server-side: a code arriving this long after the reply deadline is still eaten, never opened.
+ public const long CodeLifetimeMs = 60_000;
/// The chat line to send for a destination. One place to add an argument later.
public static string RequestFor(WebDestination d) => RequestCommand;
diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs
index 56d70f6..9150cec 100644
--- a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs
+++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs
@@ -7,30 +7,46 @@ using System;
namespace RynthCore.Plugin.RynthDreamWeave;
internal enum LaunchBegin { Started, Busy, TooSoon }
-internal enum LaunchLine { NotOurs, Code, Refused }
+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
{
- /// Held by the chat pump and the render thread around every call.
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; } = "";
- /// Busy while a request is pending (checked first); else TooSoon within MinIntervalMs of the last Begin.
+ ///
+ /// 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;
+ _expired = false;
Status = "Asking DreamWeave for a login code for " + d.Label + "...";
return LaunchBegin.Started;
}
@@ -43,19 +59,30 @@ internal sealed class WebLauncher
}
///
- /// 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 .
+ /// 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.
///
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;
+ 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)
@@ -64,8 +91,8 @@ internal sealed class WebLauncher
Status = "Opening " + p.Label + "...";
return LaunchLine.Code;
}
- message = refusal;
- Status = p.Label + ": DreamWeave said: " + refusal;
+ message = HasCodeRun(refusal) ? UnexpectedReply : refusal;
+ Status = p.Label + ": DreamWeave said: " + message;
return LaunchLine.Refused;
}
@@ -74,17 +101,35 @@ internal sealed class WebLauncher
{
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.";
+ Expire();
return p;
}
- /// Login/logout: forget the request and the status.
+ /// Login/logout: forget the pending request and the status. The throttle survives.
public void Reset()
{
_pending = null;
Status = "";
- _everBegun = false;
}
-}
\ No newline at end of file
+
+ 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.";
+ }
+
+ /// 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;
+ }
+}
diff --git a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs
index 5585fc0..456bb2a 100644
--- a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs
+++ b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs
@@ -22,7 +22,12 @@ internal static class DreamWeaveWebTests
r.Add("dreamweave web: replies count only while asked, once", RepliesOnlyWhileAsked);
r.Add("dreamweave web: one request at a time, never stuck", OneAtATime);
r.Add("dreamweave web: the code never reaches a status or message", CodeNeverInStatus);
+ r.Add("dreamweave web: a code-shaped refusal is replaced, never shown", CodeShapedRefusal);
+ r.Add("dreamweave web: an expired request frees itself", SelfExpiry);
+ r.Add("dreamweave web: reset keeps the throttle", ResetKeepsThrottle);
+ r.Add("dreamweave web: a late code is eaten while it could still be valid", LateCodeIsStale);
}
+
private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars
private const string Line = "[WEB] Launch code: " + Code + " (single use, expires in 60 seconds)";
@@ -173,7 +178,7 @@ internal static class DreamWeaveWebTests
// Timeout at exactly +10,000.
var t = new WebLauncher();
t.Begin(Market, 50_000);
- Check.True(t.OnChatLine(Line, 0x05, 60_000, out _, out _, out _) == LaunchLine.NotOurs, "line at +10,000 ms is late");
+ Check.True(t.OnChatLine(Line, 0x05, 60_000, out _, out _, out _) == LaunchLine.Stale, "a valid code at +10,000 ms is late: stale, not a code");
Check.True(t.TakeTimedOut(59_999) == null, "not timed out at +9,999");
Check.True(ReferenceEquals(t.TakeTimedOut(60_000), Market), "timed out once at +10,000");
Check.True(t.TakeTimedOut(60_001) == null, "and only once");
@@ -247,6 +252,88 @@ internal static class DreamWeaveWebTests
Check.True(!f.Status.Contains(Code) && !msg.Contains(Code), "refusal status and message");
}
+ private static void CodeShapedRefusal()
+ {
+ var w = new WebLauncher();
+ w.Begin(Market, 0);
+ var k = w.OnChatLine("[WEB] Try again later: " + Code, 0x05, 10, out var dest, out string code, out string msg);
+ Check.True(k == LaunchLine.Refused, "still a refusal, so the caller eats the line");
+ Check.True(ReferenceEquals(dest, Market), "destination handed back");
+ Check.Eq(code, "", "no code");
+ Check.True(!msg.Contains(Code), "message has no code");
+ Check.True(!w.Status.Contains(Code), "status has no code");
+ Check.Eq(msg, "DreamWeave sent an unexpected reply.", "fixed replacement text");
+ Check.Eq(w.Status, "Market: DreamWeave said: DreamWeave sent an unexpected reply.", "status from the replacement");
+ Check.True(w.Pending == null, "pending cleared");
+
+ // A long run of code chars anywhere, and a 42-char run (below the limit) left alone.
+ var w2 = new WebLauncher();
+ w2.Begin(Market, 0);
+ w2.OnChatLine("[WEB] x " + Code + Code + " y", 0x05, 10, out _, out _, out msg);
+ Check.Eq(msg, "DreamWeave sent an unexpected reply.", "longer run replaced");
+ var w3 = new WebLauncher();
+ w3.Begin(Market, 0);
+ w3.OnChatLine("[WEB] id " + Code.Substring(1) + " ok", 0x05, 10, out _, out _, out msg);
+ Check.Eq(msg, "id " + Code.Substring(1) + " ok", "42-char run is ordinary text");
+ }
+ private static void SelfExpiry()
+ {
+ var w = new WebLauncher();
+ w.Begin(Market, 0);
+ Check.True(w.Begin(Market, 9_999) == LaunchBegin.Busy, "before the deadline: busy");
+ Check.True(w.Begin(Market, 10_001) != LaunchBegin.Busy, "past the deadline: not busy");
+
+ var x = new WebLauncher();
+ x.Begin(Market, 0);
+ Check.True(x.Begin(DreamWeaveWeb.ById("suit")!, 10_001) == LaunchBegin.Started, "expired, then started as normal");
+ Check.True(x.Pending!.Id == "suit", "the new request is the pending one");
+ Check.True(x.TakeTimedOut(10_001) == null, "the new request is not timed out yet");
+ Check.Eq(x.TakeTimedOut(20_001)?.Id ?? "none", "suit", "and it times out at its own deadline");
+ }
+ private static void ResetKeepsThrottle()
+ {
+ var w = new WebLauncher();
+ Check.True(w.Begin(Market, 1_000) == LaunchBegin.Started, "begin");
+ w.Reset();
+ Check.True(w.Pending == null, "reset clears pending");
+ Check.True(w.Begin(Market, 1_001) == LaunchBegin.TooSoon, "throttle survives a relog");
+ }
+
+ private static void LateCodeIsStale()
+ {
+ var w = new WebLauncher();
+ w.Begin(Market, 0);
+ var k = w.OnChatLine(Line, 0x05, 10_500, out var dest, out string code, out string msg);
+ Check.True(k == LaunchLine.Stale, "valid code at +10,500 ms is stale");
+ Check.True(dest == null && code == "" && msg == "", "stale carries nothing");
+ Check.True(w.Pending != null, "stale does not clear pending");
+ Check.True(!w.Status.Contains(Code), "status has no code");
+ Check.True(ReferenceEquals(w.TakeTimedOut(10_500), Market), "timeout still fires once");
+ Check.True(w.TakeTimedOut(10_501) == null, "and only once");
+ Check.Eq(w.Status, "No reply from DreamWeave to /weblogin within 10 s, so nothing was opened.", "timeout status intact");
+ Check.True(w.OnChatLine(Line, 0x05, 20_000, out dest, out code, out _) == LaunchLine.Stale, "still stale after the timeout was taken");
+ Check.True(dest == null && code == "", "no destination or code");
+ Check.True(w.OnChatLine(Line, 0x05, 69_999, out _, out _, out _) == LaunchLine.Stale, "last stale ms (+69,999)");
+ Check.True(w.OnChatLine(Line, 0x05, 70_001, out _, out _, out _) == LaunchLine.NotOurs, "at +70,001 ms: not ours");
+ Check.True(w.OnChatLine("[WEB] some refusal", 0x05, 20_000, out _, out _, out _) == LaunchLine.NotOurs, "a late refusal is not ours");
+ Check.True(w.OnChatLine(Line, 0x02, 20_000, out _, out _, out _) == LaunchLine.NotOurs, "a late player copy is not ours");
+
+ var n = new WebLauncher();
+ Check.True(n.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.NotOurs, "no Begin ever: not ours");
+
+ // A consumed request leaves no stale window.
+ var c = new WebLauncher();
+ c.Begin(Market, 0);
+ c.OnChatLine(Line, 0x05, 100, out _, out _, out _);
+ Check.True(c.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.NotOurs, "after a code: not ours");
+
+ // A new Begin ends the old stale window.
+ var b = new WebLauncher();
+ b.Begin(Market, 0);
+ b.TakeTimedOut(10_000);
+ b.Begin(Market, 20_000);
+ Check.True(b.OnChatLine(Line, 0x05, 20_100, out _, out _, out _) == LaunchLine.Code, "new request takes the code");
+ }
private static string[] ServerPluginIds()
{
string? dir = AppContext.BaseDirectory;