OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Threading; |
| 36 |
| 37 using Grpc.Core.Internal; |
| 38 using Grpc.Core.Utils; |
| 39 |
| 40 namespace Grpc.Core |
| 41 { |
| 42 /// <summary> |
| 43 /// Options for calls made by client. |
| 44 /// </summary> |
| 45 public struct CallOptions |
| 46 { |
| 47 Metadata headers; |
| 48 DateTime? deadline; |
| 49 CancellationToken cancellationToken; |
| 50 WriteOptions writeOptions; |
| 51 ContextPropagationToken propagationToken; |
| 52 CallCredentials credentials; |
| 53 |
| 54 /// <summary> |
| 55 /// Creates a new instance of <c>CallOptions</c> struct. |
| 56 /// </summary> |
| 57 /// <param name="headers">Headers to be sent with the call.</param> |
| 58 /// <param name="deadline">Deadline for the call to finish. null means n
o deadline.</param> |
| 59 /// <param name="cancellationToken">Can be used to request cancellation
of the call.</param> |
| 60 /// <param name="writeOptions">Write options that will be used for this
call.</param> |
| 61 /// <param name="propagationToken">Context propagation token obtained fr
om <see cref="ServerCallContext"/>.</param> |
| 62 /// <param name="credentials">Credentials to use for this call.</param> |
| 63 public CallOptions(Metadata headers = null, DateTime? deadline = null, C
ancellationToken cancellationToken = default(CancellationToken), |
| 64 WriteOptions writeOptions = null, ContextPropagationT
oken propagationToken = null, CallCredentials credentials = null) |
| 65 { |
| 66 this.headers = headers; |
| 67 this.deadline = deadline; |
| 68 this.cancellationToken = cancellationToken; |
| 69 this.writeOptions = writeOptions; |
| 70 this.propagationToken = propagationToken; |
| 71 this.credentials = credentials; |
| 72 } |
| 73 |
| 74 /// <summary> |
| 75 /// Headers to send at the beginning of the call. |
| 76 /// </summary> |
| 77 public Metadata Headers |
| 78 { |
| 79 get { return headers; } |
| 80 } |
| 81 |
| 82 /// <summary> |
| 83 /// Call deadline. |
| 84 /// </summary> |
| 85 public DateTime? Deadline |
| 86 { |
| 87 get { return deadline; } |
| 88 } |
| 89 |
| 90 /// <summary> |
| 91 /// Token that can be used for cancelling the call. |
| 92 /// </summary> |
| 93 public CancellationToken CancellationToken |
| 94 { |
| 95 get { return cancellationToken; } |
| 96 } |
| 97 |
| 98 /// <summary> |
| 99 /// Write options that will be used for this call. |
| 100 /// </summary> |
| 101 public WriteOptions WriteOptions |
| 102 { |
| 103 get |
| 104 { |
| 105 return this.writeOptions; |
| 106 } |
| 107 } |
| 108 |
| 109 /// <summary> |
| 110 /// Token for propagating parent call context. |
| 111 /// </summary> |
| 112 public ContextPropagationToken PropagationToken |
| 113 { |
| 114 get |
| 115 { |
| 116 return this.propagationToken; |
| 117 } |
| 118 } |
| 119 |
| 120 /// <summary> |
| 121 /// Credentials to use for this call. |
| 122 /// </summary> |
| 123 public CallCredentials Credentials |
| 124 { |
| 125 get |
| 126 { |
| 127 return this.credentials; |
| 128 } |
| 129 } |
| 130 |
| 131 /// <summary> |
| 132 /// Returns new instance of <see cref="CallOptions"/> with |
| 133 /// <c>Headers</c> set to the value provided. Values of all other fields
are preserved. |
| 134 /// </summary> |
| 135 /// <param name="headers">The headers.</param> |
| 136 public CallOptions WithHeaders(Metadata headers) |
| 137 { |
| 138 var newOptions = this; |
| 139 newOptions.headers = headers; |
| 140 return newOptions; |
| 141 } |
| 142 |
| 143 /// <summary> |
| 144 /// Returns new instance of <see cref="CallOptions"/> with |
| 145 /// <c>Deadline</c> set to the value provided. Values of all other field
s are preserved. |
| 146 /// </summary> |
| 147 /// <param name="deadline">The deadline.</param> |
| 148 public CallOptions WithDeadline(DateTime deadline) |
| 149 { |
| 150 var newOptions = this; |
| 151 newOptions.deadline = deadline; |
| 152 return newOptions; |
| 153 } |
| 154 |
| 155 /// <summary> |
| 156 /// Returns new instance of <see cref="CallOptions"/> with |
| 157 /// <c>CancellationToken</c> set to the value provided. Values of all ot
her fields are preserved. |
| 158 /// </summary> |
| 159 /// <param name="cancellationToken">The cancellation token.</param> |
| 160 public CallOptions WithCancellationToken(CancellationToken cancellationT
oken) |
| 161 { |
| 162 var newOptions = this; |
| 163 newOptions.cancellationToken = cancellationToken; |
| 164 return newOptions; |
| 165 } |
| 166 |
| 167 /// <summary> |
| 168 /// Returns a new instance of <see cref="CallOptions"/> with |
| 169 /// all previously unset values set to their defaults and deadline and c
ancellation |
| 170 /// token propagated when appropriate. |
| 171 /// </summary> |
| 172 internal CallOptions Normalize() |
| 173 { |
| 174 var newOptions = this; |
| 175 if (propagationToken != null) |
| 176 { |
| 177 if (propagationToken.Options.IsPropagateDeadline) |
| 178 { |
| 179 GrpcPreconditions.CheckArgument(!newOptions.deadline.HasValu
e, |
| 180 "Cannot propagate deadline from parent call. The deadlin
e has already been set explicitly."); |
| 181 newOptions.deadline = propagationToken.ParentDeadline; |
| 182 } |
| 183 if (propagationToken.Options.IsPropagateCancellation) |
| 184 { |
| 185 GrpcPreconditions.CheckArgument(!newOptions.cancellationToke
n.CanBeCanceled, |
| 186 "Cannot propagate cancellation token from parent call. T
he cancellation token has already been set to a non-default value."); |
| 187 newOptions.cancellationToken = propagationToken.ParentCancel
lationToken; |
| 188 } |
| 189 } |
| 190 |
| 191 newOptions.headers = newOptions.headers ?? Metadata.Empty; |
| 192 newOptions.deadline = newOptions.deadline ?? DateTime.MaxValue; |
| 193 return newOptions; |
| 194 } |
| 195 } |
| 196 } |
OLD | NEW |