< Summary

Information
Class: IceRpc.Internal.IceDuplexConnectionWriter
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Internal/IceDuplexConnectionWriter.cs
Tag: 275_13775359185
Line coverage
96%
Covered lines: 32
Uncovered lines: 1
Coverable lines: 33
Total lines: 75
Line coverage: 96.9%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage
87%
Covered methods: 7
Total methods: 8
Method coverage: 87.5%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_UnflushedBytes()100%11100%
.ctor(...)100%11100%
Advance(...)100%11100%
Dispose()100%11100%
GetMemory(...)100%210%
GetSpan(...)100%11100%
FlushAsync(...)100%11100%
WriteAsync()50%22100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Internal/IceDuplexConnectionWriter.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports;
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.IO.Pipelines;
 7
 8namespace IceRpc.Internal;
 9
 10/// <summary>A helper class to write data to a duplex connection. It provides a PipeWriter-like API but is not a
 11/// PipeWriter. Like a PipeWriter, its methods shouldn't be called concurrently.</summary>
 12internal class IceDuplexConnectionWriter : IBufferWriter<byte>, IDisposable
 13{
 1614    public long UnflushedBytes => _pipe.Writer.UnflushedBytes;
 15
 16    private readonly IDuplexConnection _connection;
 17    private readonly Pipe _pipe;
 37218    private readonly SequenceCoupler _sequenceCoupler = new();
 19
 6495720    public void Advance(int bytes) => _pipe.Writer.Advance(bytes);
 21
 22    public void Dispose()
 37223    {
 37224        _pipe.Writer.Complete();
 37225        _pipe.Reader.Complete();
 37226    }
 27
 028    public Memory<byte> GetMemory(int sizeHint = 0) => _pipe.Writer.GetMemory(sizeHint);
 29
 6495930    public Span<byte> GetSpan(int sizeHint = 0) => _pipe.Writer.GetSpan(sizeHint);
 31
 32    /// <summary>Constructs a duplex connection writer.</summary>
 33    /// <param name="connection">The duplex connection to write to.</param>
 34    /// <param name="pool">The memory pool to use.</param>
 35    /// <param name="minimumSegmentSize">The minimum segment size for buffers allocated from <paramref name="pool"/>.
 36    /// </param>
 37237    internal IceDuplexConnectionWriter(IDuplexConnection connection, MemoryPool<byte> pool, int minimumSegmentSize)
 37238    {
 37239        _connection = connection;
 40
 41        // The readerScheduler doesn't matter (we don't call _pipe.Reader.ReadAsync) and the writerScheduler doesn't
 42        // matter (_pipe.Writer.FlushAsync never blocks).
 37243        _pipe = new Pipe(new PipeOptions(
 37244            pool: pool,
 37245            minimumSegmentSize: minimumSegmentSize,
 37246            pauseWriterThreshold: 0,
 37247            useSynchronizationContext: false));
 37248    }
 49
 50    /// <summary>Flush the buffered data.</summary>
 51    internal ValueTask FlushAsync(CancellationToken cancellationToken) =>
 23252        WriteAsync(ReadOnlySequence<byte>.Empty, cancellationToken);
 53
 54    /// <summary>Writes a sequence of bytes.</summary>
 55    internal async ValueTask WriteAsync(ReadOnlySequence<byte> source, CancellationToken cancellationToken)
 563656    {
 57        // Ice only calls WriteAsync or FlushAsync with unflushed bytes.
 563658        Debug.Assert(_pipe.Writer.UnflushedBytes > 0);
 59
 563660        _ = await _pipe.Writer.FlushAsync(CancellationToken.None).ConfigureAwait(false);
 563661        _pipe.Reader.TryRead(out ReadResult readResult);
 563662        Debug.Assert(!readResult.IsCompleted && !readResult.IsCanceled);
 63
 64        try
 563665        {
 563666            await _connection.WriteAsync(
 563667                _sequenceCoupler.Concat(readResult.Buffer, source),
 563668                cancellationToken).ConfigureAwait(false);
 562669        }
 70        finally
 563671        {
 563672            _pipe.Reader.AdvanceTo(readResult.Buffer.End);
 563673        }
 562674    }
 75}