Class TerminalProcessor
The default implementation of ITerminalProcessor for processing inputs in a terminal environment.
public sealed class TerminalProcessor : ITerminalProcessor, IAsyncDisposable
- Inheritance
-
TerminalProcessor
- Implements
- Inherited Members
Remarks
The TerminalProcessor manages a queue of TerminalInputOutput that are processed asynchronously in the background. It routes individual requests to the ICommandRouter for execution. The processor supports handling both single requests and batches of requests, as well as partial streams sent by clients.
Constructors
TerminalProcessor(ICommandRouter, ITerminalExceptionHandler, IOptions<TerminalOptions>, ITerminalTextHandler, ILogger<TerminalProcessor>)
Initializes a new instance of the TerminalProcessor class.
public TerminalProcessor(ICommandRouter commandRouter, ITerminalExceptionHandler terminalExceptionHandler, IOptions<TerminalOptions> terminalOptions, ITerminalTextHandler textHandler, ILogger<TerminalProcessor> logger)
Parameters
commandRouterICommandRouterThe command router to process commands.
terminalExceptionHandlerITerminalExceptionHandlerThe handler for exceptions thrown during command processing.
terminalOptionsIOptions<TerminalOptions>Configuration options for the terminal.
textHandlerITerminalTextHandlerThe terminal text handler.
loggerILogger<TerminalProcessor>Logger for logging operations within the queue.
Properties
IsBackground
Gets a value indicating whether the processor is running in the background.
public bool IsBackground { get; }
Property Value
IsProcessing
Gets a value indicating whether the processor is actively handling requests.
public bool IsProcessing { get; }
Property Value
UnprocessedIOs
Retrieves a snapshot of inputs that are pending processing.
public IReadOnlyCollection<TerminalInputOutput> UnprocessedIOs { get; }
Property Value
Remarks
The returned collection represents the state of unprocessed inputs at the time of retrieval. The actual state may change by the time the caller processes it.
Methods
AddAsync(TerminalInputOutput)
Asynchronously adds a TerminalInputOutput for processing.
public Task AddAsync(TerminalInputOutput terminalIO)
Parameters
terminalIOTerminalInputOutputThe input to add.
Returns
- Task
A task representing the asynchronous operation.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
ExecuteAsync(TerminalInputOutput)
Asynchronously executes the input and populates the output.
public Task ExecuteAsync(TerminalInputOutput terminalIO)
Parameters
terminalIOTerminalInputOutputThe input to execute.
Returns
Remarks
The ExecuteAsync(TerminalInputOutput) method processes the input immediately and returns the output. For background processing, use AddAsync(TerminalInputOutput).
StartProcessing(TerminalRouterContext, bool, Func<TerminalInputOutput, Task>?)
Starts processing terminal inputs with the specified context and configuration.
public void StartProcessing(TerminalRouterContext terminalRouterContext, bool background, Func<TerminalInputOutput, Task>? responseHandler = null)
Parameters
terminalRouterContextTerminalRouterContextThe context for the terminal router.
backgroundboolIf
true, the processor operates in the background, handling multiple requests asynchronously. Iffalse, it processes individual requests and sends responses asynchronously.responseHandlerFunc<TerminalInputOutput, Task>An optional handler for processing responses.
StopProcessingAsync(int)
Attempts to stop background processing within a specified timeout period.
public Task<bool> StopProcessingAsync(int timeout)
Parameters
timeoutintThe timeout duration in milliseconds.
Returns
- Task<bool>
A task representing the asynchronous operation, returning
trueif processing stopped within the timeout; otherwise,false.
StreamAsync(byte[], int, string, string?)
Asynchronously streams a continuous flow of TerminalInputOutput as a byte array.
public Task StreamAsync(byte[] bytes, int bytesLength, string senderId, string? senderEndpoint)
Parameters
bytesbyte[]The data to stream.
bytesLengthintThe length of the data to process. Use the total length to process all bytes.
senderIdstringThe identifier of the sender.
senderEndpointstringAn optional endpoint of the sender.
Returns
Remarks
The StreamAsync(byte[], int, string, string?) method is optimized for handling continuous
streams of data. The bytesLength parameter specifies the portion of the input to process,
avoiding unnecessary duplication of data.
WaitUntilCanceledAsync(CancellationToken)
Initiates a task that delays indefinitely until cancellation is requested.
public Task WaitUntilCanceledAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe cancellation token to trigger termination.