1 #region Copyright notice and license
3 // Copyright 2015 gRPC authors.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 using System.Threading;
22 using Grpc.Core.Internal;
27 /// Options for calls made by client.
29 public struct CallOptions
33 CancellationToken cancellationToken;
34 WriteOptions writeOptions;
35 ContextPropagationToken propagationToken;
36 CallCredentials credentials;
40 /// Creates a new instance of <c>CallOptions</c> struct.
42 /// <param name="headers">Headers to be sent with the call.</param>
43 /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
44 /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
45 /// <param name="writeOptions">Write options that will be used for this call.</param>
46 /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
47 /// <param name="credentials">Credentials to use for this call.</param>
48 public CallOptions(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken),
49 WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null, CallCredentials credentials = null)
51 this.headers = headers;
52 this.deadline = deadline;
53 this.cancellationToken = cancellationToken;
54 this.writeOptions = writeOptions;
55 this.propagationToken = propagationToken;
56 this.credentials = credentials;
57 this.flags = default(CallFlags);
61 /// Headers to send at the beginning of the call.
63 public Metadata Headers
65 get { return headers; }
71 public DateTime? Deadline
73 get { return deadline; }
77 /// Token that can be used for cancelling the call on the client side.
78 /// Cancelling the token will request cancellation
79 /// of the remote call. Best effort will be made to deliver the cancellation
80 /// notification to the server and interaction of the call with the server side
81 /// will be terminated. Unless the call finishes before the cancellation could
82 /// happen (there is an inherent race),
83 /// the call will finish with <c>StatusCode.Cancelled</c> status.
85 public CancellationToken CancellationToken
87 get { return cancellationToken; }
91 /// Write options that will be used for this call.
93 public WriteOptions WriteOptions
95 get { return this.writeOptions; }
99 /// Token for propagating parent call context.
101 public ContextPropagationToken PropagationToken
103 get { return this.propagationToken; }
107 /// Credentials to use for this call.
109 public CallCredentials Credentials
111 get { return this.credentials; }
115 /// If <c>true</c> and and channel is in <c>ChannelState.TransientFailure</c>, the call will attempt waiting for the channel to recover
116 /// instead of failing immediately (which is the default "FailFast" semantics).
117 /// Note: experimental API that can change or be removed without any prior notice.
119 public bool IsWaitForReady
121 get { return (this.flags & CallFlags.WaitForReady) == CallFlags.WaitForReady; }
125 /// Flags to use for this call.
127 internal CallFlags Flags
129 get { return this.flags; }
133 /// Returns new instance of <see cref="CallOptions"/> with
134 /// <c>Headers</c> set to the value provided. Values of all other fields are preserved.
136 /// <param name="headers">The headers.</param>
137 public CallOptions WithHeaders(Metadata headers)
139 var newOptions = this;
140 newOptions.headers = headers;
145 /// Returns new instance of <see cref="CallOptions"/> with
146 /// <c>Deadline</c> set to the value provided. Values of all other fields are preserved.
148 /// <param name="deadline">The deadline.</param>
149 public CallOptions WithDeadline(DateTime deadline)
151 var newOptions = this;
152 newOptions.deadline = deadline;
157 /// Returns new instance of <see cref="CallOptions"/> with
158 /// <c>CancellationToken</c> set to the value provided. Values of all other fields are preserved.
160 /// <param name="cancellationToken">The cancellation token.</param>
161 public CallOptions WithCancellationToken(CancellationToken cancellationToken)
163 var newOptions = this;
164 newOptions.cancellationToken = cancellationToken;
169 /// Returns new instance of <see cref="CallOptions"/> with
170 /// <c>WriteOptions</c> set to the value provided. Values of all other fields are preserved.
172 /// <param name="writeOptions">The write options.</param>
173 public CallOptions WithWriteOptions(WriteOptions writeOptions)
175 var newOptions = this;
176 newOptions.writeOptions = writeOptions;
181 /// Returns new instance of <see cref="CallOptions"/> with
182 /// <c>PropagationToken</c> set to the value provided. Values of all other fields are preserved.
184 /// <param name="propagationToken">The context propagation token.</param>
185 public CallOptions WithPropagationToken(ContextPropagationToken propagationToken)
187 var newOptions = this;
188 newOptions.propagationToken = propagationToken;
193 /// Returns new instance of <see cref="CallOptions"/> with
194 /// <c>Credentials</c> set to the value provided. Values of all other fields are preserved.
196 /// <param name="credentials">The call credentials.</param>
197 public CallOptions WithCredentials(CallCredentials credentials)
199 var newOptions = this;
200 newOptions.credentials = credentials;
205 /// Returns new instance of <see cref="CallOptions"/> with "WaitForReady" semantics enabled/disabled.
206 /// <see cref="IsWaitForReady"/>.
207 /// Note: experimental API that can change or be removed without any prior notice.
209 public CallOptions WithWaitForReady(bool waitForReady = true)
213 return WithFlags(this.flags | CallFlags.WaitForReady);
215 return WithFlags(this.flags & ~CallFlags.WaitForReady);
219 /// Returns new instance of <see cref="CallOptions"/> with
220 /// <c>Flags</c> set to the value provided. Values of all other fields are preserved.
222 /// <param name="flags">The call flags.</param>
223 internal CallOptions WithFlags(CallFlags flags)
225 var newOptions = this;
226 newOptions.flags = flags;