OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 #endregion |
| 31 using System; |
| 32 using System.Collections.Generic; |
| 33 using System.Runtime.InteropServices; |
| 34 using System.Threading; |
| 35 using System.Threading.Tasks; |
| 36 using Grpc.Core.Internal; |
| 37 using Grpc.Core.Utils; |
| 38 |
| 39 namespace Grpc.Core |
| 40 { |
| 41 /// <summary> |
| 42 /// Channel option specified when creating a channel. |
| 43 /// Corresponds to grpc_channel_args from grpc/grpc.h. |
| 44 /// </summary> |
| 45 public sealed class ChannelOption |
| 46 { |
| 47 /// <summary> |
| 48 /// Type of <c>ChannelOption</c>. |
| 49 /// </summary> |
| 50 public enum OptionType |
| 51 { |
| 52 /// <summary> |
| 53 /// Channel option with integer value. |
| 54 /// </summary> |
| 55 Integer, |
| 56 |
| 57 /// <summary> |
| 58 /// Channel option with string value. |
| 59 /// </summary> |
| 60 String |
| 61 } |
| 62 |
| 63 private readonly OptionType type; |
| 64 private readonly string name; |
| 65 private readonly int intValue; |
| 66 private readonly string stringValue; |
| 67 |
| 68 /// <summary> |
| 69 /// Creates a channel option with a string value. |
| 70 /// </summary> |
| 71 /// <param name="name">Name.</param> |
| 72 /// <param name="stringValue">String value.</param> |
| 73 public ChannelOption(string name, string stringValue) |
| 74 { |
| 75 this.type = OptionType.String; |
| 76 this.name = GrpcPreconditions.CheckNotNull(name, "name"); |
| 77 this.stringValue = GrpcPreconditions.CheckNotNull(stringValue, "stri
ngValue"); |
| 78 } |
| 79 |
| 80 /// <summary> |
| 81 /// Creates a channel option with an integer value. |
| 82 /// </summary> |
| 83 /// <param name="name">Name.</param> |
| 84 /// <param name="intValue">Integer value.</param> |
| 85 public ChannelOption(string name, int intValue) |
| 86 { |
| 87 this.type = OptionType.Integer; |
| 88 this.name = GrpcPreconditions.CheckNotNull(name, "name"); |
| 89 this.intValue = intValue; |
| 90 } |
| 91 |
| 92 /// <summary> |
| 93 /// Gets the type of the <c>ChannelOption</c>. |
| 94 /// </summary> |
| 95 public OptionType Type |
| 96 { |
| 97 get |
| 98 { |
| 99 return type; |
| 100 } |
| 101 } |
| 102 |
| 103 /// <summary> |
| 104 /// Gets the name of the <c>ChannelOption</c>. |
| 105 /// </summary> |
| 106 public string Name |
| 107 { |
| 108 get |
| 109 { |
| 110 return name; |
| 111 } |
| 112 } |
| 113 |
| 114 /// <summary> |
| 115 /// Gets the integer value the <c>ChannelOption</c>. |
| 116 /// </summary> |
| 117 public int IntValue |
| 118 { |
| 119 get |
| 120 { |
| 121 GrpcPreconditions.CheckState(type == OptionType.Integer); |
| 122 return intValue; |
| 123 } |
| 124 } |
| 125 |
| 126 /// <summary> |
| 127 /// Gets the string value the <c>ChannelOption</c>. |
| 128 /// </summary> |
| 129 public string StringValue |
| 130 { |
| 131 get |
| 132 { |
| 133 GrpcPreconditions.CheckState(type == OptionType.String); |
| 134 return stringValue; |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 /// <summary> |
| 140 /// Defines names of supported channel options. |
| 141 /// </summary> |
| 142 public static class ChannelOptions |
| 143 { |
| 144 /// <summary>Override SSL target check. Only to be used for testing.</su
mmary> |
| 145 public const string SslTargetNameOverride = "grpc.ssl_target_name_overri
de"; |
| 146 |
| 147 /// <summary>Enable census for tracing and stats collection</summary> |
| 148 public const string Census = "grpc.census"; |
| 149 |
| 150 /// <summary>Maximum number of concurrent incoming streams to allow on a
http2 connection</summary> |
| 151 public const string MaxConcurrentStreams = "grpc.max_concurrent_streams"
; |
| 152 |
| 153 /// <summary>Maximum message length that the channel can receive</summar
y> |
| 154 public const string MaxMessageLength = "grpc.max_message_length"; |
| 155 |
| 156 /// <summary>Initial sequence number for http2 transports</summary> |
| 157 public const string Http2InitialSequenceNumber = "grpc.http2.initial_seq
uence_number"; |
| 158 |
| 159 /// <summary>Default authority for calls.</summary> |
| 160 public const string DefaultAuthority = "grpc.default_authority"; |
| 161 |
| 162 /// <summary>Primary user agent: goes at the start of the user-agent met
adata</summary> |
| 163 public const string PrimaryUserAgentString = "grpc.primary_user_agent"; |
| 164 |
| 165 /// <summary>Secondary user agent: goes at the end of the user-agent met
adata</summary> |
| 166 public const string SecondaryUserAgentString = "grpc.secondary_user_agen
t"; |
| 167 |
| 168 /// <summary> |
| 169 /// Creates native object for a collection of channel options. |
| 170 /// </summary> |
| 171 /// <returns>The native channel arguments.</returns> |
| 172 internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection<Chan
nelOption> options) |
| 173 { |
| 174 if (options == null || options.Count == 0) |
| 175 { |
| 176 return ChannelArgsSafeHandle.CreateNull(); |
| 177 } |
| 178 ChannelArgsSafeHandle nativeArgs = null; |
| 179 try |
| 180 { |
| 181 nativeArgs = ChannelArgsSafeHandle.Create(options.Count); |
| 182 int i = 0; |
| 183 foreach (var option in options) |
| 184 { |
| 185 if (option.Type == ChannelOption.OptionType.Integer) |
| 186 { |
| 187 nativeArgs.SetInteger(i, option.Name, option.IntValue); |
| 188 } |
| 189 else if (option.Type == ChannelOption.OptionType.String) |
| 190 { |
| 191 nativeArgs.SetString(i, option.Name, option.StringValue)
; |
| 192 } |
| 193 else |
| 194 { |
| 195 throw new InvalidOperationException("Unknown option type
"); |
| 196 } |
| 197 i++; |
| 198 } |
| 199 return nativeArgs; |
| 200 } |
| 201 catch (Exception) |
| 202 { |
| 203 if (nativeArgs != null) |
| 204 { |
| 205 nativeArgs.Dispose(); |
| 206 } |
| 207 throw; |
| 208 } |
| 209 } |
| 210 } |
| 211 } |
OLD | NEW |