Skip to content

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.

csharp
public sealed class StreamWorkloadAdapter : IHex1bTerminalWorkloadAdapter, IAsyncDisposable

Inheritance

ObjectStreamWorkloadAdapter

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).
csharp
public StreamWorkloadAdapter(Stream outputReader, Stream inputWriter)

Properties

Height

Terminal height (for informational purposes).

Returns: Int32

csharp
public int Height { get; set; }

Width

Terminal width (for informational purposes).

Returns: Int32

csharp
public int Width { get; set; }

Methods

CreateHeadless(int, int)

Creates a headless workload adapter with in-memory channels for testing.

Parameters:

  • width (Int32): Terminal width.
  • height (Int32): Terminal height.

Returns: StreamWorkloadAdapter

csharp
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.

csharp
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:

Returns: ValueTask<Byte>>

csharp
public ValueTask<ReadOnlyMemory<byte>> ReadOutputAsync(CancellationToken ct = default)

ResizeAsync(int, int, CancellationToken)

Notify workload of terminal resize.

Parameters:

Returns: ValueTask

csharp
public ValueTask ResizeAsync(int width, int height, CancellationToken ct = default)

SignalDisconnected()

Signals that the workload has disconnected.

csharp
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:

Returns: ValueTask

csharp
public ValueTask WriteInputAsync(ReadOnlyMemory<byte> data, CancellationToken ct = default)

Events

Disconnected

Raised when workload has disconnected/exited.

Returns: Action

csharp
public event Action? Disconnected

Remarks

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

csharp
// 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

Released under the MIT License.