blob: 6ad5d56cad6e1810f62e8341adc448cc5925297d [file] [log] [blame]
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -07001#region Copyright notice and license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2015 gRPC authors.
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -07003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// 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 Tattermusch2ddb5a62015-06-08 17:51:36 -07007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -07009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// 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 Tattermusch2ddb5a62015-06-08 17:51:36 -070015#endregion
16using System;
17using System.Collections.Generic;
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070018using System.Runtime.InteropServices;
19using System.Threading;
20using System.Threading.Tasks;
21using Grpc.Core.Internal;
22using Grpc.Core.Utils;
23
24namespace 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 Tattermusch12855fc2015-08-24 16:43:23 -070032 /// <summary>
33 /// Type of <c>ChannelOption</c>.
34 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070035 public enum OptionType
36 {
Jan Tattermusch12855fc2015-08-24 16:43:23 -070037 /// <summary>
38 /// Channel option with integer value.
39 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070040 Integer,
Jan Tattermusch12855fc2015-08-24 16:43:23 -070041
42 /// <summary>
43 /// Channel option with string value.
44 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070045 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 Tattermusch7a3ee6a2016-02-18 10:36:02 -080061 this.name = GrpcPreconditions.CheckNotNull(name, "name");
62 this.stringValue = GrpcPreconditions.CheckNotNull(stringValue, "stringValue");
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070063 }
64
65 /// <summary>
66 /// Creates a channel option with an integer value.
67 /// </summary>
68 /// <param name="name">Name.</param>
Jan Tattermusche7178522015-08-17 14:59:14 -070069 /// <param name="intValue">Integer value.</param>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070070 public ChannelOption(string name, int intValue)
71 {
72 this.type = OptionType.Integer;
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080073 this.name = GrpcPreconditions.CheckNotNull(name, "name");
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070074 this.intValue = intValue;
75 }
76
Jan Tattermusch12855fc2015-08-24 16:43:23 -070077 /// <summary>
78 /// Gets the type of the <c>ChannelOption</c>.
79 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070080 public OptionType Type
81 {
82 get
83 {
84 return type;
85 }
86 }
87
Jan Tattermusch12855fc2015-08-24 16:43:23 -070088 /// <summary>
89 /// Gets the name of the <c>ChannelOption</c>.
90 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070091 public string Name
92 {
93 get
94 {
95 return name;
96 }
97 }
98
Jan Tattermusch12855fc2015-08-24 16:43:23 -070099 /// <summary>
100 /// Gets the integer value the <c>ChannelOption</c>.
101 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700102 public int IntValue
103 {
104 get
105 {
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -0800106 GrpcPreconditions.CheckState(type == OptionType.Integer);
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700107 return intValue;
108 }
109 }
110
Jan Tattermusch12855fc2015-08-24 16:43:23 -0700111 /// <summary>
112 /// Gets the string value the <c>ChannelOption</c>.
113 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700114 public string StringValue
115 {
116 get
117 {
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -0800118 GrpcPreconditions.CheckState(type == OptionType.String);
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700119 return stringValue;
120 }
121 }
122 }
123
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700124 /// <summary>
125 /// Defines names of supported channel options.
126 /// </summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700127 public static class ChannelOptions
128 {
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700129 /// <summary>Override SSL target check. Only to be used for testing.</summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700130 public const string SslTargetNameOverride = "grpc.ssl_target_name_override";
131
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700132 /// <summary>Enable census for tracing and stats collection</summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700133 public const string Census = "grpc.census";
134
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700135 /// <summary>Maximum number of concurrent incoming streams to allow on a http2 connection</summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700136 public const string MaxConcurrentStreams = "grpc.max_concurrent_streams";
137
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700138 /// <summary>Maximum message length that the channel can receive</summary>
Jan Tattermusch18108562017-03-13 11:21:48 +0100139 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 Tattermusch2ddb5a62015-06-08 17:51:36 -0700147
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700148 /// <summary>Initial sequence number for http2 transports</summary>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700149 public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number";
150
Jan Tattermusch07ea52c2015-08-03 18:34:31 -0700151 /// <summary>Default authority for calls.</summary>
152 public const string DefaultAuthority = "grpc.default_authority";
153
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700154 /// <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 Tattermusch12855fc2015-08-24 16:43:23 -0700157 /// <summary>Secondary user agent: goes at the end of the user-agent metadata</summary>
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700158 public const string SecondaryUserAgentString = "grpc.secondary_user_agent";
159
Jan Tattermuschf517fad2017-04-20 10:49:11 +0200160 /// <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 Tattermusch2ddb5a62015-06-08 17:51:36 -0700163 /// <summary>
164 /// Creates native object for a collection of channel options.
165 /// </summary>
166 /// <returns>The native channel arguments.</returns>
Jan Tattermusch7ebbc472015-12-08 22:39:02 -0800167 internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection<ChannelOption> options)
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700168 {
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700169 if (options == null || options.Count == 0)
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700170 {
171 return ChannelArgsSafeHandle.CreateNull();
172 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700173 ChannelArgsSafeHandle nativeArgs = null;
174 try
175 {
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700176 nativeArgs = ChannelArgsSafeHandle.Create(options.Count);
Jan Tattermusch7ebbc472015-12-08 22:39:02 -0800177 int i = 0;
178 foreach (var option in options)
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700179 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700180 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 Tattermusch7ebbc472015-12-08 22:39:02 -0800192 i++;
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700193 }
194 return nativeArgs;
195 }
196 catch (Exception)
197 {
198 if (nativeArgs != null)
199 {
200 nativeArgs.Dispose();
201 }
202 throw;
203 }
204 }
205 }
206}