StreamWorkloadAdapter
Namespace: Hex1b
Assembly: Hex1b.dll
A simple workload adapter that wraps input and output streams. Useful for testing or connecting to arbitrary byte streams.
public sealed class StreamWorkloadAdapter : IHex1bTerminalWorkloadAdapter, IAsyncDisposableInheritance
Object → StreamWorkloadAdapter
Implements
Constructors
StreamWorkloadAdapter(Stream, Stream)
Creates a new stream workload adapter.
Parameters:
outputReader(Stream): Stream to read workload output from (what the terminal displays).inputWriter(Stream): Stream to write input to (keystrokes to workload).
public StreamWorkloadAdapter(Stream outputReader, Stream inputWriter)Properties
Height
Terminal height (for informational purposes).
Returns: Int32
public int Height { get; set; }Width
Terminal width (for informational purposes).
Returns: Int32
public int Width { get; set; }Methods
CreateHeadless(int, int)
Creates a headless workload adapter with in-memory channels for testing.
Parameters:
Returns: StreamWorkloadAdapter
public static StreamWorkloadAdapter CreateHeadless(int width = 80, int height = 24)DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
Returns: ValueTask
A task that represents the asynchronous dispose operation.
public ValueTask DisposeAsync()ReadOutputAsync(CancellationToken)
Read output FROM the workload (ANSI sequences to display). The terminal calls this to get data to parse and send to presentation. Returns empty when workload has no more output (should be called in a loop).
Parameters:
ct(CancellationToken):
Returns: ValueTask<Byte>>
public ValueTask<ReadOnlyMemory<byte>> ReadOutputAsync(CancellationToken ct = default)ResizeAsync(int, int, CancellationToken)
Notify workload of terminal resize.
Parameters:
width(Int32):height(Int32):ct(CancellationToken):
Returns: ValueTask
public ValueTask ResizeAsync(int width, int height, CancellationToken ct = default)SignalDisconnected()
Signals that the workload has disconnected.
public void SignalDisconnected()WriteInputAsync(ReadOnlyMemory<byte>, CancellationToken)
Write input TO the workload (raw bytes from keyboard/mouse). The terminal calls this when it receives input from the presentation layer.
Parameters:
data(ReadOnlyMemory<Byte>):ct(CancellationToken):
Returns: ValueTask
public ValueTask WriteInputAsync(ReadOnlyMemory<byte> data, CancellationToken ct = default)Events
Disconnected
Raised when workload has disconnected/exited.
Returns: Action
public event Action? DisconnectedRemarks
This adapter treats the streams from the perspective of the workload: OutputStream: Where the workload writes its output (terminal reads from here)InputStream: Where the workload reads its input (terminal writes here)
For testing, you can use or pipe streams. The terminal will read from outputStream and write to inputStream.
Examples
// Create streams for bidirectional communication
var outputPipe = new Pipe();
var inputPipe = new Pipe();
var workload = new StreamWorkloadAdapter(
outputReader: outputPipe.Reader.AsStream(), // terminal reads workload output
inputWriter: inputPipe.Writer.AsStream()); // terminal writes workload input
// Now write to outputPipe.Writer to simulate workload output
// Read from inputPipe.Reader to see what terminal sent as input