Table of Contents

Class PipeMessageHandler

Namespace
StreamJsonRpc
Assembly
StreamJsonRpc.dll

An abstract base class for for sending and receiving messages using PipeReader and PipeWriter.

public abstract class PipeMessageHandler : MessageHandlerBase, IJsonRpcMessageHandler, IDisposableObservable, IDisposable, IAsyncDisposable, IJsonRpcMessageBufferManager
Inheritance
PipeMessageHandler
Implements
Derived
Inherited Members

Constructors

PipeMessageHandler(IDuplexPipe, IJsonRpcMessageFormatter)

Initializes a new instance of the PipeMessageHandler class.

public PipeMessageHandler(IDuplexPipe pipe, IJsonRpcMessageFormatter formatter)

Parameters

pipe IDuplexPipe

The reader and writer to use for receiving/transmitting messages.

formatter IJsonRpcMessageFormatter

The formatter used to serialize messages.

PipeMessageHandler(PipeWriter?, PipeReader?, IJsonRpcMessageFormatter)

Initializes a new instance of the PipeMessageHandler class.

public PipeMessageHandler(PipeWriter? writer, PipeReader? reader, IJsonRpcMessageFormatter formatter)

Parameters

writer PipeWriter

The writer to use for transmitting messages.

reader PipeReader

The reader to use for receiving messages.

formatter IJsonRpcMessageFormatter

The formatter used to serialize messages.

PipeMessageHandler(Stream?, Stream?, IJsonRpcMessageFormatter)

Initializes a new instance of the PipeMessageHandler class.

public PipeMessageHandler(Stream? writer, Stream? reader, IJsonRpcMessageFormatter formatter)

Parameters

writer Stream

The stream to use for transmitting messages.

reader Stream

The stream to use for receiving messages.

formatter IJsonRpcMessageFormatter

The formatter used to serialize messages.

Properties

CanRead

Gets a value indicating whether this message handler can receive messages.

public override bool CanRead { get; }

Property Value

bool

CanWrite

Gets a value indicating whether this message handler can send messages.

public override bool CanWrite { get; }

Property Value

bool

Reader

Gets the reader to use for receiving messages.

protected PipeReader? Reader { get; }

Property Value

PipeReader

Writer

Gets the writer to use for transmitting messages.

protected PipeWriter? Writer { get; }

Property Value

PipeWriter

Methods

DisposeReader()

Disposes resources allocated by this instance that are used for reading (not writing).

protected override void DisposeReader()

Remarks

This method is called by MessageHandlerBase after the last read operation has completed.

DisposeWriter()

Disposes resources allocated by this instance that are used for writing (not reading).

protected override void DisposeWriter()

Remarks

This method is called by MessageHandlerBase after the last write operation has completed. Overrides of this method should call the base method as well.

FlushAsync(CancellationToken)

Ensures that all messages transmitted up to this point are en route to their destination, rather than sitting in some local buffer.

protected override ValueTask FlushAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A Task that completes when the write buffer has been transmitted, or at least that the operation is in progress, if final transmission cannot be tracked.

ReadAtLeastAsync(int, bool, CancellationToken)

Reads from the Reader until at least a specified number of bytes are available.

protected ValueTask<ReadResult> ReadAtLeastAsync(int requiredBytes, bool allowEmpty, CancellationToken cancellationToken)

Parameters

requiredBytes int

The number of bytes that must be available.

allowEmpty bool

true to allow returning 0 bytes if the end of the stream is encountered before any bytes are read.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<ReadResult>

The ReadResult containing at least requiredBytes bytes.

Exceptions

OperationCanceledException

Thrown if IsCanceled.

EndOfStreamException

Thrown if IsCompleted before we have requiredBytes bytes. Not thrown if 0 bytes were read and allowEmpty is true.

Write(JsonRpcMessage, CancellationToken)

Writes a message to the pipe.

protected abstract void Write(JsonRpcMessage content, CancellationToken cancellationToken)

Parameters

content JsonRpcMessage

The message to write.

cancellationToken CancellationToken

A token to cancel the transmission.

Remarks

Implementations may assume the method is never called before the previous call has completed. They can assume their caller will invoke FlushAsync(CancellationToken) on their behalf after writing is completed.

WriteCoreAsync(JsonRpcMessage, CancellationToken)

Writes a message.

protected override sealed ValueTask WriteCoreAsync(JsonRpcMessage content, CancellationToken cancellationToken)

Parameters

content JsonRpcMessage

The message to write.

cancellationToken CancellationToken

A token to cancel the transmission.

Returns

ValueTask

A task that represents the asynchronous write operation.