< Summary

Information
Class: IceRpc.Protobuf.RpcMethods.IncomingRequestExtensions
Assembly: IceRpc.Protobuf
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc.Protobuf/RpcMethods/IncomingRequestExtensions.cs
Tag: 1856_27024993493
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 159
Line coverage: 100%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage
100%
Covered methods: 4
Fully covered methods: 4
Total methods: 4
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DispatchUnaryAsync()83.33%66100%
DispatchClientStreamingAsync()83.33%66100%
DispatchServerStreamingAsync()100%22100%
DispatchBidiStreamingAsync()100%22100%

File(s)

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

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using Google.Protobuf;
 4using IceRpc.Features;
 5using IceRpc.Protobuf.RpcMethods.Internal;
 6
 7namespace IceRpc.Protobuf.RpcMethods;
 8
 9/// <summary>Provides extension methods for <see cref="IncomingRequest" />.</summary>
 10/// <remarks>These methods are called by the <see cref="IDispatcher"/> implementation generated by the Protobuf service
 11/// generator.</remarks>
 12public static class IncomingRequestExtensions
 13{
 14    /// <summary>Dispatches a unary RPC method.</summary>
 15    /// <typeparam name="TInput">The type of the input message.</typeparam>
 16    /// <typeparam name="TOutput">The type of the output message.</typeparam>
 17    /// <typeparam name="TService">The generated INameService interface.</typeparam>
 18    /// <param name="request">The incoming request.</param>
 19    /// <param name="inputParser">A message parser used to decode the request payload.</param>
 20    /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param>
 21    /// <param name="method">The user-provided implementation of the RPC method.</param>
 22    /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param>
 23    /// <returns>A value task that holds the outgoing response.</returns>
 24    public static async ValueTask<OutgoingResponse> DispatchUnaryAsync<TInput, TOutput, TService>(
 25        this IncomingRequest request,
 26        MessageParser<TInput> inputParser,
 27        TService service,
 28        Func<TService, TInput, IFeatureCollection, CancellationToken, ValueTask<TOutput>> method,
 29        CancellationToken cancellationToken) where TInput : class, IMessage<TInput>
 30                                             where TOutput : class, IMessage<TOutput>
 31                                             where TService : class
 1432    {
 1433        IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default;
 34
 1435        TInput input = await request.Payload.DecodeProtobufMessageAsync(
 1436            inputParser,
 1437            protobufFeature.MaxMessageLength,
 1438            acceptEmptyPayload: false,
 1439            cancellationToken).ConfigureAwait(false);
 40
 1341        TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false);
 42
 1343        return new OutgoingResponse(request)
 1344        {
 1345            Payload = output.EncodeAsLengthPrefixedMessage(
 1346                protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions)
 1347        };
 1348    }
 49
 50    /// <summary>Dispatches a client-streaming RPC method.</summary>
 51    /// <typeparam name="TInput">The type of the input message.</typeparam>
 52    /// <typeparam name="TOutput">The type of the output message.</typeparam>
 53    /// <typeparam name="TService">The generated INameService interface.</typeparam>
 54    /// <param name="request">The incoming request.</param>
 55    /// <param name="inputParser">A message parser used to decode the request payload.</param>
 56    /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param>
 57    /// <param name="method">The user-provided implementation of the RPC method.</param>
 58    /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param>
 59    /// <returns>A value task that holds the outgoing response.</returns>
 60    public static async ValueTask<OutgoingResponse> DispatchClientStreamingAsync<TInput, TOutput, TService>(
 61        this IncomingRequest request,
 62        MessageParser<TInput> inputParser,
 63        TService service,
 64        Func<TService, IAsyncStream<TInput>, IFeatureCollection, CancellationToken, ValueTask<TOutput>> method,
 65        CancellationToken cancellationToken) where TInput : class, IMessage<TInput>
 66                                             where TOutput : class, IMessage<TOutput>
 67                                             where TService : class
 368    {
 369        IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default;
 70
 71        // Ownership of the input stream is transferred to the user method.
 72#pragma warning disable CA2000 // Dispose objects before losing scope
 373        IAsyncStream<TInput> input = request.DetachPayload().ToAsyncStream(
 374            inputParser,
 375            protobufFeature.MaxMessageLength);
 76#pragma warning restore CA2000
 77
 378        TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false);
 79
 380        return new OutgoingResponse(request)
 381        {
 382            Payload = output.EncodeAsLengthPrefixedMessage(
 383                protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions)
 384        };
 385    }
 86
 87    /// <summary>Dispatches a server-streaming RPC method.</summary>
 88    /// <typeparam name="TInput">The type of the input message.</typeparam>
 89    /// <typeparam name="TOutput">The type of the output message.</typeparam>
 90    /// <typeparam name="TService">The generated INameService interface.</typeparam>
 91    /// <param name="request">The incoming request.</param>
 92    /// <param name="inputParser">A message parser used to decode the request payload.</param>
 93    /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param>
 94    /// <param name="method">The user-provided implementation of the RPC method.</param>
 95    /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param>
 96    /// <returns>A value task that holds the outgoing response.</returns>
 97    public static async ValueTask<OutgoingResponse> DispatchServerStreamingAsync<TInput, TOutput, TService>(
 98        this IncomingRequest request,
 99        MessageParser<TInput> inputParser,
 100        TService service,
 101        Func<TService, TInput, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutput>>> method,
 102        CancellationToken cancellationToken) where TInput : class, IMessage<TInput>
 103                                             where TOutput : class, IMessage<TOutput>
 104                                             where TService : class
 3105    {
 3106        IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default;
 107
 3108        TInput input = await request.Payload.DecodeProtobufMessageAsync(
 3109            inputParser,
 3110            protobufFeature.MaxMessageLength,
 3111            acceptEmptyPayload: false,
 3112            cancellationToken).ConfigureAwait(false);
 113
 3114        IAsyncEnumerable<TOutput> output = await method(service, input, request.Features, cancellationToken)
 3115            .ConfigureAwait(false);
 116
 3117        return new OutgoingResponse(request)
 3118        {
 3119            PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions)
 3120        };
 3121    }
 122
 123    /// <summary>Dispatches a bidi-streaming RPC method.</summary>
 124    /// <typeparam name="TInput">The type of the input message.</typeparam>
 125    /// <typeparam name="TOutput">The type of the output message.</typeparam>
 126    /// <typeparam name="TService">The generated INameService interface.</typeparam>
 127    /// <param name="request">The incoming request.</param>
 128    /// <param name="inputParser">A message parser used to decode the request payload.</param>
 129    /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param>
 130    /// <param name="method">The user-provided implementation of the RPC method.</param>
 131    /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param>
 132    /// <returns>A value task that holds the outgoing response.</returns>
 133    public static async ValueTask<OutgoingResponse> DispatchBidiStreamingAsync<TInput, TOutput, TService>(
 134        this IncomingRequest request,
 135        MessageParser<TInput> inputParser,
 136        TService service,
 137        Func<TService, IAsyncStream<TInput>, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutput>>
 138        CancellationToken cancellationToken) where TInput : class, IMessage<TInput>
 139                                             where TOutput : class, IMessage<TOutput>
 140                                             where TService : class
 3141    {
 3142        IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default;
 143
 144        // Ownership of the input stream is transferred to the user method.
 145#pragma warning disable CA2000 // Dispose objects before losing scope
 3146        IAsyncStream<TInput> input = request.DetachPayload().ToAsyncStream(
 3147            inputParser,
 3148            protobufFeature.MaxMessageLength);
 149#pragma warning restore CA2000
 150
 3151        IAsyncEnumerable<TOutput> output =
 3152            await method(service, input, request.Features, cancellationToken).ConfigureAwait(false);
 153
 3154        return new OutgoingResponse(request)
 3155        {
 3156            PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions)
 3157        };
 3158    }
 159}