| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Features; |
| | 4 | | using System.Buffers; |
| | 5 | | using System.Collections.Immutable; |
| | 6 | |
|
| | 7 | | namespace IceRpc; |
| | 8 | |
|
| | 9 | | /// <summary>Represents a request frame received by the application.</summary> |
| | 10 | | public sealed class IncomingRequest : IncomingFrame, IDisposable |
| | 11 | | { |
| | 12 | | /// <summary>Gets or sets the features of this request.</summary> |
| | 13 | | /// <value>The <see cref="IFeatureCollection" /> of this request. Defaults to <see cref="FeatureCollection.Empty" |
| | 14 | | /// />.</value> |
| 6520 | 15 | | public IFeatureCollection Features { get; set; } = FeatureCollection.Empty; |
| | 16 | |
|
| | 17 | | /// <summary>Gets or sets the fields of this request.</summary> |
| | 18 | | /// <value>The fields of this request. Defaults to <see cref="ImmutableDictionary{TKey, TValue}.Empty" />.</value> |
| 8411 | 19 | | public IDictionary<RequestFieldKey, ReadOnlySequence<byte>> Fields { get; set; } = |
| 5659 | 20 | | ImmutableDictionary<RequestFieldKey, ReadOnlySequence<byte>>.Empty; |
| | 21 | |
|
| | 22 | | /// <summary>Gets or initializes the fragment of the target service.</summary> |
| | 23 | | /// <value>The fragment of the target service. It is always the empty string with the icerpc protocol. Defaults to |
| | 24 | | /// the empty string.</value> |
| | 25 | | public string Fragment |
| | 26 | | { |
| 82 | 27 | | get => _fragment; |
| 2709 | 28 | | init => _fragment = Protocol == Protocol.Ice || value.Length == 0 ? value : |
| 2709 | 29 | | throw new InvalidOperationException("Cannot create an icerpc request with a non-empty fragment."); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary>Gets a value indicating whether this request is one-way or two-way.</summary> |
| | 33 | | /// <value><see langword="true" /> for one-way requests; otherwise, <see langword="false" />. The default is |
| | 34 | | /// <see langword="false" />.</value> |
| 11056 | 35 | | public bool IsOneway { get; init; } |
| | 36 | |
|
| | 37 | | /// <summary>Gets or initializes the name of the operation to call on the target service.</summary> |
| | 38 | | /// <value>The name of the operation. Defaults to the empty string.</value> |
| 11471 | 39 | | public string Operation { get; init; } = ""; |
| | 40 | |
|
| | 41 | | /// <summary>Gets or initializes the path of the target service.</summary> |
| | 42 | | /// <value>The path of the target service. Defaults to <c>/</c>.</value> |
| 11501 | 43 | | public string Path { get; init; } = "/"; |
| | 44 | |
|
| | 45 | | /// <summary>Gets or sets the latest response for this request.</summary> |
| | 46 | | /// <value>The request's latest response or <see langword="null"/> if the response is not set yet.</value> |
| | 47 | | /// <remarks>Setting a response completes the previous response when there is one.</remarks> |
| | 48 | | internal OutgoingResponse? Response |
| | 49 | | { |
| 5414 | 50 | | get => _response; |
| | 51 | | set |
| 5584 | 52 | | { |
| 5584 | 53 | | ObjectDisposedException.ThrowIf(_isDisposed, this); |
| | 54 | |
|
| 5584 | 55 | | _response?.Payload.Complete(); |
| 5584 | 56 | | _response?.PayloadContinuation?.Complete(); |
| 5584 | 57 | | _response = value; |
| 5584 | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| 5659 | 61 | | private readonly string _fragment = ""; |
| | 62 | |
|
| | 63 | | // IncomingRequest is not thread-safe and does not accept a response after it is disposed. |
| | 64 | | private bool _isDisposed; |
| | 65 | |
|
| | 66 | | private OutgoingResponse? _response; |
| | 67 | |
|
| | 68 | | /// <summary>Constructs an incoming request.</summary> |
| | 69 | | /// <param name="protocol">The protocol of this request.</param> |
| | 70 | | /// <param name="connectionContext">The connection context of the connection that received this request.</param> |
| | 71 | | public IncomingRequest(Protocol protocol, IConnectionContext connectionContext) |
| 5659 | 72 | | : base(protocol, connectionContext) |
| 5659 | 73 | | { |
| 5659 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary>Disposes this incoming request. This completes the payload of this request and the payload(s) of the |
| | 77 | | /// response associated with this request (if set).</summary> |
| | 78 | | public void Dispose() |
| 5654 | 79 | | { |
| 5654 | 80 | | if (!_isDisposed) |
| 5654 | 81 | | { |
| 5654 | 82 | | _isDisposed = true; |
| 5654 | 83 | | Payload.Complete(); |
| 5654 | 84 | | _response?.Payload.Complete(); |
| 5654 | 85 | | _response?.PayloadContinuation?.Complete(); |
| 5654 | 86 | | } |
| 5654 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary>Returns a string that represents this incoming request.</summary> |
| | 90 | | /// <returns>A string that represents this incoming requests.</returns> |
| 0 | 91 | | public override string ToString() => Fragment.Length == 0 ? |
| 0 | 92 | | $"'{Operation}' on '{Path}'" : |
| 0 | 93 | | $"'{Operation}' on '{Path}#{Fragment}'"; |
| | 94 | | } |