Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 15 | #endregion |
| 16 | using System; |
| 17 | using System.Collections.Generic; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 18 | using System.Runtime.InteropServices; |
| 19 | using System.Threading; |
| 20 | using System.Threading.Tasks; |
| 21 | using Grpc.Core.Internal; |
| 22 | using Grpc.Core.Utils; |
| 23 | |
| 24 | namespace Grpc.Core |
| 25 | { |
| 26 | /// <summary> |
| 27 | /// Channel option specified when creating a channel. |
| 28 | /// Corresponds to grpc_channel_args from grpc/grpc.h. |
| 29 | /// </summary> |
| 30 | public sealed class ChannelOption |
| 31 | { |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 32 | /// <summary> |
| 33 | /// Type of <c>ChannelOption</c>. |
| 34 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 35 | public enum OptionType |
| 36 | { |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 37 | /// <summary> |
| 38 | /// Channel option with integer value. |
| 39 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 40 | Integer, |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 41 | |
| 42 | /// <summary> |
| 43 | /// Channel option with string value. |
| 44 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 45 | String |
| 46 | } |
| 47 | |
| 48 | private readonly OptionType type; |
| 49 | private readonly string name; |
| 50 | private readonly int intValue; |
| 51 | private readonly string stringValue; |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Creates a channel option with a string value. |
| 55 | /// </summary> |
| 56 | /// <param name="name">Name.</param> |
| 57 | /// <param name="stringValue">String value.</param> |
| 58 | public ChannelOption(string name, string stringValue) |
| 59 | { |
| 60 | this.type = OptionType.String; |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 61 | this.name = GrpcPreconditions.CheckNotNull(name, "name"); |
| 62 | this.stringValue = GrpcPreconditions.CheckNotNull(stringValue, "stringValue"); |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Creates a channel option with an integer value. |
| 67 | /// </summary> |
| 68 | /// <param name="name">Name.</param> |
Jan Tattermusch | e717852 | 2015-08-17 14:59:14 -0700 | [diff] [blame] | 69 | /// <param name="intValue">Integer value.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 70 | public ChannelOption(string name, int intValue) |
| 71 | { |
| 72 | this.type = OptionType.Integer; |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 73 | this.name = GrpcPreconditions.CheckNotNull(name, "name"); |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 74 | this.intValue = intValue; |
| 75 | } |
| 76 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 77 | /// <summary> |
| 78 | /// Gets the type of the <c>ChannelOption</c>. |
| 79 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 80 | public OptionType Type |
| 81 | { |
| 82 | get |
| 83 | { |
| 84 | return type; |
| 85 | } |
| 86 | } |
| 87 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 88 | /// <summary> |
| 89 | /// Gets the name of the <c>ChannelOption</c>. |
| 90 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 91 | public string Name |
| 92 | { |
| 93 | get |
| 94 | { |
| 95 | return name; |
| 96 | } |
| 97 | } |
| 98 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 99 | /// <summary> |
| 100 | /// Gets the integer value the <c>ChannelOption</c>. |
| 101 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 102 | public int IntValue |
| 103 | { |
| 104 | get |
| 105 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 106 | GrpcPreconditions.CheckState(type == OptionType.Integer); |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 107 | return intValue; |
| 108 | } |
| 109 | } |
| 110 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 111 | /// <summary> |
| 112 | /// Gets the string value the <c>ChannelOption</c>. |
| 113 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 114 | public string StringValue |
| 115 | { |
| 116 | get |
| 117 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 118 | GrpcPreconditions.CheckState(type == OptionType.String); |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 119 | return stringValue; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 124 | /// <summary> |
| 125 | /// Defines names of supported channel options. |
| 126 | /// </summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 127 | public static class ChannelOptions |
| 128 | { |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 129 | /// <summary>Override SSL target check. Only to be used for testing.</summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 130 | public const string SslTargetNameOverride = "grpc.ssl_target_name_override"; |
| 131 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 132 | /// <summary>Enable census for tracing and stats collection</summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 133 | public const string Census = "grpc.census"; |
| 134 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 135 | /// <summary>Maximum number of concurrent incoming streams to allow on a http2 connection</summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 136 | public const string MaxConcurrentStreams = "grpc.max_concurrent_streams"; |
| 137 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 138 | /// <summary>Maximum message length that the channel can receive</summary> |
Jan Tattermusch | 1810856 | 2017-03-13 11:21:48 +0100 | [diff] [blame] | 139 | public const string MaxReceiveMessageLength = "grpc.max_receive_message_length"; |
| 140 | |
| 141 | /// <summary>Maximum message length that the channel can send</summary> |
| 142 | public const string MaxSendMessageLength = "grpc.max_send_message_length"; |
| 143 | |
| 144 | /// <summary>Obsolete, for backward compatibility only.</summary> |
| 145 | [Obsolete("Use MaxReceiveMessageLength instead.")] |
| 146 | public const string MaxMessageLength = MaxReceiveMessageLength; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 147 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 148 | /// <summary>Initial sequence number for http2 transports</summary> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 149 | public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number"; |
| 150 | |
Jan Tattermusch | 07ea52c | 2015-08-03 18:34:31 -0700 | [diff] [blame] | 151 | /// <summary>Default authority for calls.</summary> |
| 152 | public const string DefaultAuthority = "grpc.default_authority"; |
| 153 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 154 | /// <summary>Primary user agent: goes at the start of the user-agent metadata</summary> |
| 155 | public const string PrimaryUserAgentString = "grpc.primary_user_agent"; |
| 156 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 157 | /// <summary>Secondary user agent: goes at the end of the user-agent metadata</summary> |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 158 | public const string SecondaryUserAgentString = "grpc.secondary_user_agent"; |
| 159 | |
Jan Tattermusch | f517fad | 2017-04-20 10:49:11 +0200 | [diff] [blame] | 160 | /// <summary>If non-zero, allow the use of SO_REUSEPORT for server if it's available (default 1)</summary> |
| 161 | public const string SoReuseport = "grpc.so_reuseport"; |
| 162 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 163 | /// <summary> |
| 164 | /// Creates native object for a collection of channel options. |
| 165 | /// </summary> |
| 166 | /// <returns>The native channel arguments.</returns> |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 167 | internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection<ChannelOption> options) |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 168 | { |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 169 | if (options == null || options.Count == 0) |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 170 | { |
| 171 | return ChannelArgsSafeHandle.CreateNull(); |
| 172 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 173 | ChannelArgsSafeHandle nativeArgs = null; |
| 174 | try |
| 175 | { |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 176 | nativeArgs = ChannelArgsSafeHandle.Create(options.Count); |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 177 | int i = 0; |
| 178 | foreach (var option in options) |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 179 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 180 | if (option.Type == ChannelOption.OptionType.Integer) |
| 181 | { |
| 182 | nativeArgs.SetInteger(i, option.Name, option.IntValue); |
| 183 | } |
| 184 | else if (option.Type == ChannelOption.OptionType.String) |
| 185 | { |
| 186 | nativeArgs.SetString(i, option.Name, option.StringValue); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | throw new InvalidOperationException("Unknown option type"); |
| 191 | } |
Jan Tattermusch | 7ebbc47 | 2015-12-08 22:39:02 -0800 | [diff] [blame] | 192 | i++; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 193 | } |
| 194 | return nativeArgs; |
| 195 | } |
| 196 | catch (Exception) |
| 197 | { |
| 198 | if (nativeArgs != null) |
| 199 | { |
| 200 | nativeArgs.Dispose(); |
| 201 | } |
| 202 | throw; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |