< Summary

Information
Class: IceRpc.Ice.Operations.Internal.PipeReaderExtensions
Assembly: IceRpc.Ice
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Ice/Operations/Internal/PipeReaderExtensions.cs
Tag: 1321_24790053727
Line coverage
54%
Covered lines: 17
Uncovered lines: 14
Coverable lines: 31
Total lines: 91
Line coverage: 54.8%
Branch coverage
50%
Covered branches: 8
Total branches: 16
Branch coverage: 50%
Method coverage
100%
Covered methods: 2
Fully covered methods: 0
Total methods: 2
Method coverage: 100%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReadFullPayloadAsync()50%7670%
TryReadFullPayload(...)50%241047.61%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Ice/Operations/Internal/PipeReaderExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Diagnostics;
 4using System.IO.Pipelines;
 5
 6namespace IceRpc.Ice.Operations.Internal;
 7
 8/// <summary>Provides extension methods for <see cref="PipeReader" /> to read payloads.</summary>
 9internal static class PipeReaderExtensions
 10{
 11    /// <summary>Reads the full payload from a pipe reader.</summary>
 12    /// <param name="reader">The pipe reader.</param>
 13    /// <param name="maxSize">The maximum size of this payload.</param>
 14    /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param>
 15    /// <returns>A read result with the payload read from the reader unless <see cref="ReadResult.IsCanceled" /> is
 16    /// <see langword="true" />.</returns>
 17    /// <exception cref="InvalidDataException">Thrown when the payload size exceeds <paramref name="maxSize" />.
 18    /// </exception>
 19    /// <remarks>The caller must call AdvanceTo on the reader, as usual. This method reads all the remaining bytes in
 20    /// the reader.</remarks>
 21    internal static async ValueTask<ReadResult> ReadFullPayloadAsync(
 22        this PipeReader reader,
 23        int maxSize,
 24        CancellationToken cancellationToken)
 1625    {
 1626        Debug.Assert(maxSize is > 0 and < int.MaxValue);
 27
 28        // This method does not attempt to read the reader synchronously. A caller that wants a sync attempt can
 29        // call TryReadFullPayload.
 30
 31        // We read everything up to the maxSize + 1.
 32        // It's maxSize + 1 and not maxSize because if the payload's size is maxSize, we could get
 33        // readResult.IsCompleted == false even though the full payload was read.
 1634        ReadResult readResult = await reader.ReadAtLeastAsync(maxSize + 1, cancellationToken).ConfigureAwait(false);
 35
 1636        if (readResult.IsCompleted && readResult.Buffer.Length <= maxSize)
 1637        {
 1638            return readResult;
 39        }
 40        else
 041        {
 042            reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
 043            throw new InvalidDataException("The payload size exceeds the maximum value.");
 44        }
 1645    }
 46
 47    /// <summary>Attempts to read the full payload from a pipe reader.</summary>
 48    /// <param name="reader">The pipe reader.</param>
 49    /// <param name="maxSize">The maximum size of this payload.</param>
 50    /// <param name="readResult">The read result.</param>
 51    /// <returns><see langword="true" /> when <paramref name="readResult" /> contains the payload read synchronously, or
 52    /// the call was cancelled; otherwise, <see langword="false" />.</returns>
 53    /// <exception cref="InvalidDataException">Thrown when the payload size exceeds the max payload size.</exception>
 54    /// <remarks>When this method returns <see langword="true" />, the caller must call AdvanceTo on the reader, as
 55    /// usual. When this method returns <see langword="false" />, the caller must call
 56    /// <see cref="ReadFullPayloadAsync" />.</remarks>
 57    internal static bool TryReadFullPayload(
 58        this PipeReader reader,
 59        int maxSize,
 60        out ReadResult readResult)
 22561    {
 22562        Debug.Assert(maxSize is > 0 and < int.MaxValue);
 63
 22564        if (reader.TryRead(out readResult))
 22565        {
 22566            if (readResult.IsCanceled)
 067            {
 068                return true; // and the buffer does not matter
 69            }
 70
 22571            if (readResult.Buffer.Length > maxSize)
 072            {
 073                reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
 074                throw new InvalidDataException("The payload size exceeds the maximum value.");
 75            }
 76
 22577            if (readResult.IsCompleted)
 22578            {
 22579                return true;
 80            }
 81            else
 082            {
 83                // don't consume anything but mark the whole buffer as examined - we need more.
 084                reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
 085            }
 086        }
 87
 088        readResult = default;
 089        return false;
 22590    }
 91}