Table of Contents

Class StreamTransformWriter

Namespace
IceRpc
Assembly
IceRpc.dll

Decorates an IMultiplexedStreamWriter with a Stream transform, such as a BrotliStream or a CryptoStream. The bytes written to this writer flow through the transform stream; the bytes the transform stream writes to its inner stream are sent to the decoratee.

public sealed class StreamTransformWriter : IMultiplexedStreamWriter, IBufferWriter<byte>
Inheritance
StreamTransformWriter
Implements
Inherited Members

Remarks

The transform stream must write synchronously, and only to the inner stream it is given. Each WriteAsync(ReadOnlySequence<byte>, bool, CancellationToken) call flushes the transform stream and sends what this flush emits; a transform stream can keep part of its input until the stream ends. Ending the stream disposes the transform stream and sends its trailing bytes.

Constructors

StreamTransformWriter(IMultiplexedStreamWriter, Func<Stream, Stream>)

Constructs a stream transform writer.

public StreamTransformWriter(IMultiplexedStreamWriter decoratee, Func<Stream, Stream> transform)

Parameters

decoratee IMultiplexedStreamWriter

The writer that receives the transformed bytes.

transform Func<Stream, Stream>

Creates the transform stream over a write-only, in-memory inner stream.

Properties

UnflushedBytes

Gets the number of bytes buffered by the IBufferWriter<T> methods and not yet sent. IceRPC reads this property before and after encoding a header to compute the size of this header, since parts of the header can be written directly with the IBufferWriter<T> methods.

public long UnflushedBytes { get; }

Property Value

long

The number of unflushed bytes.

Methods

Abort()

Aborts this writer: discards the unflushed bytes, closes the writes of the stream abruptly and releases the resources of this writer. This method never blocks.

public void Abort()

Advance(int)

Notifies the IBufferWriter<T> that count data items were written to the output Span<T> or Memory<T>.

public void Advance(int count)

Parameters

count int

The number of data items written to the Span<T> or Memory<T>.

CompleteAsync(CancellationToken)

Completes this writer gracefully: sends the unflushed bytes and any trailing bytes the implementation produces, closes the writes of the stream if a WriteAsync(ReadOnlySequence<byte>, bool, CancellationToken) call with endStream hasn't already, and releases the resources of this writer. The peer having closed its reads is not a failure.

public ValueTask CompleteAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A cancellation token that receives the cancellation requests.

Returns

ValueTask

A task that completes once the writer is completed.

Remarks

When this method fails, it aborts the writer before throwing: the caller does not need to call Abort() afterwards.

GetMemory(int)

Returns a Memory<T> to write to that is at least the requested size (specified by sizeHint).

public Memory<byte> GetMemory(int sizeHint = 0)

Parameters

sizeHint int

The minimum length of the returned Memory<T>. If 0, a non-empty buffer is returned.

Returns

Memory<byte>

A Memory<T> of at least the size sizeHint. If sizeHint is 0, returns a non-empty buffer.

Exceptions

OutOfMemoryException

The requested buffer size is not available.

GetSpan(int)

Returns a Span<T> to write to that is at least the requested size (specified by sizeHint).

public Span<byte> GetSpan(int sizeHint = 0)

Parameters

sizeHint int

The minimum length of the returned Span<T>. If 0, a non-empty buffer is returned.

Returns

Span<byte>

A Span<T> of at least the size sizeHint. If sizeHint is 0, returns a non-empty buffer.

WriteAsync(ReadOnlySequence<byte>, bool, CancellationToken)

Sends the unflushed bytes followed by source.

public ValueTask<WriteResult> WriteAsync(ReadOnlySequence<byte> source, bool endStream, CancellationToken cancellationToken)

Parameters

source ReadOnlySequence<byte>

The bytes to send.

endStream bool

When true, no more bytes will be written: the transport marks this frame as the last one of the stream, and later calls to WriteAsync(ReadOnlySequence<byte>, bool, CancellationToken) and Advance(int) throw InvalidOperationException.

cancellationToken CancellationToken

A cancellation token that receives the cancellation requests.

Returns

ValueTask<WriteResult>

Written, or WritesClosed when the peer has closed its reads and the bytes were discarded.

Remarks

When this method throws, the owner aborts the writer with Abort().

Exceptions

InvalidOperationException

Thrown when the writer is completed or a previous call to WriteAsync(ReadOnlySequence<byte>, bool, CancellationToken) had endStream set.