| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | |
| | | 6 | | namespace IceRpc; |
| | | 7 | | |
| | | 8 | | /// <summary>Represents a request frame sent by the application.</summary> |
| | | 9 | | public sealed class OutgoingRequest : OutgoingFrame, IDisposable |
| | | 10 | | { |
| | | 11 | | /// <summary>Gets or sets the features of this request.</summary> |
| | | 12 | | /// <value>The <see cref="IFeatureCollection" /> of this request. Defaults to <see |
| | | 13 | | /// cref="FeatureCollection.Empty" />.</value> |
| | 6655 | 14 | | public IFeatureCollection Features { get; set; } = FeatureCollection.Empty; |
| | | 15 | | |
| | | 16 | | /// <summary>Gets or sets the fields of this request.</summary> |
| | | 17 | | /// <value>The fields of this request. Defaults to <see cref="ImmutableDictionary{TKey, TValue}.Empty" />.</value> |
| | 8447 | 18 | | public IDictionary<RequestFieldKey, OutgoingFieldValue> Fields { get; set; } = |
| | 5737 | 19 | | ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty; |
| | | 20 | | |
| | | 21 | | /// <summary>Gets a value indicating whether this request is one-way or two-way.</summary> |
| | | 22 | | /// <value><see langword="true" /> for one-way requests; otherwise, <see langword="false" />. The default is |
| | | 23 | | /// <see langword="false" />.</value> |
| | 15183 | 24 | | public bool IsOneway { get; init; } |
| | | 25 | | |
| | | 26 | | /// <summary>Gets or initializes the name of the operation to call on the target service.</summary> |
| | | 27 | | /// <value>The name of the operation. The default is the empty string.</value> |
| | 11538 | 28 | | public string Operation { get; init; } = ""; |
| | | 29 | | |
| | | 30 | | /// <summary>Gets the address of the target service.</summary> |
| | | 31 | | /// <value>The <see cref="ServiceAddress" /> of this request.</value> |
| | 11454 | 32 | | public ServiceAddress ServiceAddress { get; } |
| | | 33 | | |
| | | 34 | | /// <summary>Gets or sets the latest response for this request.</summary> |
| | | 35 | | /// <value>The request's latest response or <see langword="null"/> if the response is not set yet.</value> |
| | | 36 | | /// <remarks>Setting a response completes the previous response when there is one.</remarks> |
| | | 37 | | internal IncomingResponse? Response |
| | | 38 | | { |
| | 0 | 39 | | get => _response; |
| | | 40 | | set |
| | 5584 | 41 | | { |
| | 5584 | 42 | | ObjectDisposedException.ThrowIf(_isDisposed, this); |
| | | 43 | | |
| | 5584 | 44 | | _response?.Dispose(); |
| | 5584 | 45 | | _response = value; |
| | 5584 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // OutgoingRequest is not thread-safe and should not receive a response after it is disposed. |
| | | 50 | | private bool _isDisposed; |
| | | 51 | | |
| | | 52 | | private IncomingResponse? _response; |
| | | 53 | | |
| | | 54 | | /// <summary>Constructs an outgoing request.</summary> |
| | | 55 | | /// <param name="serviceAddress">The address of the target service.</param> |
| | | 56 | | public OutgoingRequest(ServiceAddress serviceAddress) |
| | 5737 | 57 | | : base(serviceAddress.Protocol ?? |
| | 5737 | 58 | | throw new ArgumentException( |
| | 5737 | 59 | | "An outgoing request requires a service address with a protocol such as icerpc or ice.", |
| | 5737 | 60 | | nameof(serviceAddress))) => |
| | 5737 | 61 | | ServiceAddress = serviceAddress; |
| | | 62 | | |
| | | 63 | | /// <summary>Disposes this outgoing request. This completes the payload and payload continuation of this request, |
| | | 64 | | /// and the response associated with this request (if already received).</summary> |
| | | 65 | | public void Dispose() |
| | 5762 | 66 | | { |
| | 5762 | 67 | | if (!_isDisposed) |
| | 5734 | 68 | | { |
| | 5734 | 69 | | _isDisposed = true; |
| | 5734 | 70 | | Payload.Complete(); |
| | 5734 | 71 | | PayloadContinuation?.Complete(); |
| | 5734 | 72 | | _response?.Dispose(); |
| | 5734 | 73 | | } |
| | 5762 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Returns a string that represents this outgoing request.</summary> |
| | | 77 | | /// <returns>A string that represents this outgoing request.</returns> |
| | 0 | 78 | | public override string ToString() => $"'{Operation}' on '{ServiceAddress}'"; |
| | | 79 | | } |