< Summary

Information
Class: IceRpc.Protobuf.RpcMethods.Internal.MessageExtensions
Assembly: IceRpc.Protobuf
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/RpcMethods/Internal/MessageExtensions.cs
Tag: 1856_27024993493
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 33
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 1
Fully covered methods: 1
Total methods: 1
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EncodeAsLengthPrefixedMessage(...)100%11100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/RpcMethods/Internal/MessageExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using Google.Protobuf;
 4using System.Buffers;
 5using System.Buffers.Binary;
 6using System.IO.Pipelines;
 7
 8namespace IceRpc.Protobuf.RpcMethods.Internal;
 9
 10/// <summary>Provides an extension method for <see cref="IMessage" />.</summary>
 11internal static class MessageExtensions
 12{
 13    /// <summary>Encodes an <see cref="IMessage"/> as a length-prefixed message, using the Protobuf encoding.</summary>
 14    /// <param name="message">The <see cref="IMessage" /> to encode.</param>
 15    /// <param name="pipeOptions">The options used to create the pipe.</param>
 16    /// <returns>A <see cref="PipeReader" /> containing the length-prefixed message.</returns>
 17    /// <remarks>The envelope follows the gRPC Length-Prefixed-Message format: a 1-byte compression flag (always
 18    /// 0 — IceRPC does not compress Protobuf payloads), followed by the message length as a 4-byte unsigned
 19    /// big-endian integer, followed by the Protobuf-encoded message bytes. See
 20    /// <see href="https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#data-frames" />.</remarks>
 21    internal static PipeReader EncodeAsLengthPrefixedMessage(this IMessage message, PipeOptions pipeOptions)
 6122    {
 6123        var pipe = new Pipe(pipeOptions);
 6124        pipe.Writer.Write(new Span<byte>([0])); // Not compressed
 6125        Span<byte> lengthPlaceholder = pipe.Writer.GetSpan(4);
 6126        pipe.Writer.Advance(4);
 6127        message.WriteTo(pipe.Writer);
 6128        int length = checked((int)pipe.Writer.UnflushedBytes);
 6129        BinaryPrimitives.WriteUInt32BigEndian(lengthPlaceholder, (uint)(length - 5));
 6130        pipe.Writer.Complete();
 6131        return pipe.Reader;
 6132    }
 33}