| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using System.Collections.Immutable; |
| | 5 | | using System.IO.Pipelines; |
| | 6 | |
|
| | 7 | | namespace IceRpc; |
| | 8 | |
|
| | 9 | | /// <summary>Represents a response frame received by the application.</summary> |
| | 10 | | public sealed class IncomingResponse : IncomingFrame |
| | 11 | | { |
| | 12 | | /// <summary>Gets the error message of this response.</summary> |
| | 13 | | /// <value>The error message of this response if <see cref="StatusCode" /> is different from <see |
| | 14 | | /// cref="StatusCode.Ok" />; otherwise, <see langword="null"/>.</value> |
| 57 | 15 | | public string? ErrorMessage { get; } |
| | 16 | |
|
| | 17 | | /// <summary>Gets the fields of this incoming response.</summary> |
| | 18 | | /// <value>The fields of this incoming response. Defaults to <see cref="ImmutableDictionary{TKey, TValue}.Empty" |
| | 19 | | /// />.</value> |
| 11202 | 20 | | public IDictionary<ResponseFieldKey, ReadOnlySequence<byte>> Fields { get; private set; } |
| | 21 | |
|
| | 22 | | /// <summary>Gets the <see cref="StatusCode" /> of this response.</summary> |
| | 23 | | /// <value>The <see cref="IceRpc.StatusCode" /> of this response.</value> |
| 348 | 24 | | public StatusCode StatusCode { get; } |
| | 25 | |
|
| | 26 | | private readonly PipeReader? _fieldsPipeReader; |
| | 27 | |
|
| | 28 | | /// <summary>Constructs an incoming response with empty fields.</summary> |
| | 29 | | /// <param name="request">The corresponding outgoing request.</param> |
| | 30 | | /// <param name="connectionContext">The connection context of the connection that received this response.</param> |
| | 31 | | /// <param name="statusCode">The status code of this response.</param> |
| | 32 | | /// <param name="errorMessage">The error message of this response.</param> |
| | 33 | | /// <remarks>The constructor also associates this response with the request. If another response is already set on |
| | 34 | | /// the request, its payload and payload continuation are completed.</remarks> |
| | 35 | | public IncomingResponse( |
| | 36 | | OutgoingRequest request, |
| | 37 | | IConnectionContext connectionContext, |
| | 38 | | StatusCode statusCode = StatusCode.Ok, |
| | 39 | | string? errorMessage = null) |
| 4869 | 40 | | : this( |
| 4869 | 41 | | request, |
| 4869 | 42 | | connectionContext, |
| 4869 | 43 | | statusCode, |
| 4869 | 44 | | errorMessage, |
| 4869 | 45 | | ImmutableDictionary<ResponseFieldKey, ReadOnlySequence<byte>>.Empty, |
| 4869 | 46 | | fieldsPipeReader: null) |
| 4869 | 47 | | { |
| 4869 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary>Constructs an incoming response.</summary> |
| | 51 | | /// <param name="request">The corresponding outgoing request.</param> |
| | 52 | | /// <param name="connectionContext">The connection context of the connection that received this response.</param> |
| | 53 | | /// <param name="statusCode">The status code of this response.</param> |
| | 54 | | /// <param name="errorMessage">The error message of this response.</param> |
| | 55 | | /// <param name="fields">The fields of this response.</param> |
| | 56 | | /// <remarks>The constructor also associates this response with the request. If another response is already set on |
| | 57 | | /// the request, its payload and payload continuation are completed.</remarks> |
| | 58 | | public IncomingResponse( |
| | 59 | | OutgoingRequest request, |
| | 60 | | IConnectionContext connectionContext, |
| | 61 | | StatusCode statusCode, |
| | 62 | | string? errorMessage, |
| | 63 | | IDictionary<ResponseFieldKey, ReadOnlySequence<byte>> fields) |
| 3 | 64 | | : this(request, connectionContext, statusCode, errorMessage, fields, fieldsPipeReader: null) |
| 3 | 65 | | { |
| 3 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary>Constructs an incoming response with a pipe reader holding the memory for the fields.</summary> |
| | 69 | | /// <param name="request">The corresponding outgoing request.</param> |
| | 70 | | /// <param name="connectionContext">The connection context of the connection that received this response.</param> |
| | 71 | | /// <param name="statusCode">The status code of this response.</param> |
| | 72 | | /// <param name="errorMessage">The error message of this response.</param> |
| | 73 | | /// <param name="fields">The fields of this response.</param> |
| | 74 | | /// <param name="fieldsPipeReader">The pipe reader that holds the memory of the fields. Use <see langword="null"/> |
| | 75 | | /// when the fields memory is not held by a pipe reader.</param> |
| | 76 | | /// <remarks>The constructor also associates this response with the request. If another response is already set on |
| | 77 | | /// the request, its payload and payload continuation are completed.</remarks> |
| | 78 | | internal IncomingResponse( |
| | 79 | | OutgoingRequest request, |
| | 80 | | IConnectionContext connectionContext, |
| | 81 | | StatusCode statusCode, |
| | 82 | | string? errorMessage, |
| | 83 | | IDictionary<ResponseFieldKey, ReadOnlySequence<byte>> fields, |
| | 84 | | PipeReader? fieldsPipeReader) |
| 5583 | 85 | | : base(request.Protocol, connectionContext) |
| 5583 | 86 | | { |
| 5583 | 87 | | if (statusCode == StatusCode.Ok) |
| 5459 | 88 | | { |
| 5459 | 89 | | if (errorMessage is not null) |
| 0 | 90 | | { |
| 0 | 91 | | throw new ArgumentException( |
| 0 | 92 | | $"The {nameof(errorMessage)} argument must be null when {nameof(statusCode)} is {nameof(StatusCode.O |
| 0 | 93 | | nameof(errorMessage)); |
| | 94 | | } |
| 5459 | 95 | | } |
| 124 | 96 | | else if (errorMessage is null) |
| 0 | 97 | | { |
| 0 | 98 | | throw new ArgumentException( |
| 0 | 99 | | $"The {nameof(errorMessage)} argument must be non-null when {nameof(statusCode)} is greater than {nameof |
| 0 | 100 | | nameof(errorMessage)); |
| | 101 | | } |
| | 102 | |
|
| 5583 | 103 | | StatusCode = statusCode; |
| 5583 | 104 | | ErrorMessage = errorMessage; |
| 5583 | 105 | | Fields = fields; |
| 5583 | 106 | | _fieldsPipeReader = fieldsPipeReader; |
| 5583 | 107 | | request.Response = this; |
| 5583 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary>Completes the payload and releases the fields memory.</summary> |
| | 111 | | /// <remarks>Dispose is internal because application code must dispose the outgoing request that owns this incoming |
| | 112 | | /// response or create a different incoming response that disposes the previous response held by this outgoing |
| | 113 | | /// request.</remarks> |
| | 114 | | internal void Dispose() |
| 5580 | 115 | | { |
| 5580 | 116 | | Payload.Complete(); |
| 5580 | 117 | | _fieldsPipeReader?.Complete(); |
| 5580 | 118 | | Fields = ImmutableDictionary<ResponseFieldKey, ReadOnlySequence<byte>>.Empty; |
| 5580 | 119 | | } |
| | 120 | | } |