Public Access
Add the Avalonia app and the xterm renderer, and fix two real bugs
The terminal works end to end. A new integration test drives a real sshd in a container through a real PTY, the real pump, the real loopback WebSocket with its token and origin checks, and a ClientWebSocket standing in for the page: the login banner arrives, typed input round-trips, and `stty size` reports the 100x30 the session asked for. The only untested link left is xterm drawing bytes it was handed. The WebView is de-risked on Windows, which was the plan's largest risk. Not by assertion: with the app running there is an established TCP connection from msedgewebview2 to the data plane port, so WebView2 launched, navigated to the loopback page, executed terminal.js, and completed the WebSocket handshake against the real token and origin checks. Linux remains unproven and the package's own release notes now corroborate the concern -- Linux uses a WPE backend, and it ships a NativeWebDialog described as useful where embedded WebViews may be unavailable. Two bugs found by building it, both of which would have shipped: - ShellStream.Write buffers and needs an explicit Flush. Without one a keystroke is accepted, reported as written, and never reaches the remote: the terminal displays output perfectly and simply stops responding to input. SSH.NET's own WriteLine flushes, which is why the earlier spike never hit it. Found by isolating the pump against real SSH and reading BytesRead=51 -- banner and prompt through, nothing after. - The Windows app manifest needs a supportedOS list, or Avalonia's native control host fails outright and the terminal never starts. Also fixed a genuinely flaky test I happened to catch: SyncCursorTests tampered with the *last* base64url character, whose low bits the decoder ignores when the input length is not a multiple of three -- so a tampered cursor sometimes decoded to identical bytes and verified. It failed roughly one run in thirty, depending on a random key. Now tampers the penultimate character, which is fully significant at every length; 40 consecutive runs are clean. xterm 6.0.0 plus the fit and webgl addons are vendored as UMD bundles rather than built with npm, so a clean clone needs only the .NET SDK. Provenance and licences are recorded next to them, along with the UMD global names terminal.js depends on -- a bundle that switched to ES modules would load without error and leave Terminal undefined. The renderer acknowledges output from term.write's completion callback, not on receipt. Acknowledging early would return flow-control credit for bytes the screen has not caught up with, which is the one thing the credit window exists to measure. TerminalWorkspace moved into DodoSSH.Client.Terminal: it has no Avalonia dependency, and having it there is what let the end-to-end test exist at all. 404 tests pass, zero warnings on a clean rebuild, format clean.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
using DodoSSH.Client.Ssh;
|
||||
|
||||
namespace DodoSSH.Client.Terminal;
|
||||
|
||||
/// <summary>
|
||||
/// Owns the loopback data plane and every live terminal session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// One data plane and one renderer page for the whole application, with a session id per terminal.
|
||||
/// Not one WebView per tab: each WebView2 is a separate browser process, so twenty tabs would mean
|
||||
/// twenty renderer processes and several hundred megabytes for a working set a user would call
|
||||
/// ordinary. Splits and tabs are layout inside the single page.
|
||||
/// </remarks>
|
||||
public sealed class TerminalWorkspace : IAsyncDisposable
|
||||
{
|
||||
private readonly TerminalDataPlane dataPlane;
|
||||
private readonly ISshConnectionFactory connections;
|
||||
private readonly TimeProvider clock;
|
||||
private readonly Dictionary<uint, LiveSession> sessions = [];
|
||||
private readonly CancellationTokenSource lifetime = new();
|
||||
|
||||
private uint nextSessionId = 1;
|
||||
private Task? server;
|
||||
private int disposed;
|
||||
|
||||
public TerminalWorkspace(
|
||||
ITerminalAssetProvider assets,
|
||||
ISshConnectionFactory connections,
|
||||
TimeProvider clock)
|
||||
{
|
||||
this.connections = connections;
|
||||
this.clock = clock;
|
||||
|
||||
dataPlane = new TerminalDataPlane(assets);
|
||||
}
|
||||
|
||||
/// <summary>Where the WebView should navigate.</summary>
|
||||
public Uri PageUrl => dataPlane.PageUrl;
|
||||
|
||||
/// <summary>Starts the loopback listener.</summary>
|
||||
public void Start() => server = dataPlane.RunAsync(lifetime.Token);
|
||||
|
||||
/// <summary>
|
||||
/// Waits until the renderer page has attached its socket.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A session opened before the renderer attaches would have its <c>SessionOpened</c> frame
|
||||
/// dropped — the transport discards frames when nothing is connected — leaving output arriving
|
||||
/// for a terminal that was never created.
|
||||
/// </remarks>
|
||||
public Task WaitForRendererAsync() => dataPlane.RendererAttached;
|
||||
|
||||
/// <summary>Connects to a host and starts a terminal for it.</summary>
|
||||
/// <returns>The session id, which identifies this terminal in the renderer.</returns>
|
||||
public async Task<uint> OpenSessionAsync(
|
||||
SshConnectionRequest request,
|
||||
TerminalSize size,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var connection = await connections.ConnectAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
ISshShellSession shell;
|
||||
try
|
||||
{
|
||||
shell = await connection.OpenShellAsync(size, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await connection.DisposeAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
|
||||
var sessionId = nextSessionId++;
|
||||
var pump = new TerminalSessionPump(sessionId, shell, dataPlane, clock);
|
||||
|
||||
dataPlane.Register(sessionId, pump);
|
||||
|
||||
// Registered before running, so an acknowledgement that arrives with the very first output
|
||||
// frame has somewhere to go.
|
||||
var run = RunSessionAsync(sessionId, pump);
|
||||
|
||||
sessions[sessionId] = new LiveSession(connection, pump, run);
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
/// <summary>Closes one terminal.</summary>
|
||||
public async Task CloseSessionAsync(uint sessionId)
|
||||
{
|
||||
if (!sessions.Remove(sessionId, out var session))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dataPlane.Unregister(sessionId);
|
||||
|
||||
await session.Pump.DisposeAsync().ConfigureAwait(false);
|
||||
await session.Connection.DisposeAsync().ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
await session.Run.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Expected: disposing the pump cancels its run.
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Interlocked.Exchange(ref disposed, 1) == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var sessionId in sessions.Keys.ToArray())
|
||||
{
|
||||
await CloseSessionAsync(sessionId).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await lifetime.CancelAsync().ConfigureAwait(false);
|
||||
await dataPlane.DisposeAsync().ConfigureAwait(false);
|
||||
|
||||
if (server is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await server.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Expected: the accept loop is stopped by cancelling it.
|
||||
}
|
||||
}
|
||||
|
||||
lifetime.Dispose();
|
||||
}
|
||||
|
||||
private async Task RunSessionAsync(uint sessionId, TerminalSessionPump pump)
|
||||
{
|
||||
try
|
||||
{
|
||||
await pump.RunAsync(lifetime.Token).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
dataPlane.Unregister(sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record LiveSession(ISshConnection Connection, TerminalSessionPump Pump, Task Run);
|
||||
}
|
||||
Reference in New Issue
Block a user