ChannelOptions.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  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
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  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.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  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. namespace Grpc.Core
  24. {
  25. /// <summary>
  26. /// Channel option specified when creating a channel.
  27. /// Corresponds to grpc_channel_args from grpc/grpc.h.
  28. /// </summary>
  29. public sealed class ChannelOption
  30. {
  31. /// <summary>
  32. /// Type of <c>ChannelOption</c>.
  33. /// </summary>
  34. public enum OptionType
  35. {
  36. /// <summary>
  37. /// Channel option with integer value.
  38. /// </summary>
  39. Integer,
  40. /// <summary>
  41. /// Channel option with string value.
  42. /// </summary>
  43. String
  44. }
  45. private readonly OptionType type;
  46. private readonly string name;
  47. private readonly int intValue;
  48. private readonly string stringValue;
  49. /// <summary>
  50. /// Creates a channel option with a string value.
  51. /// </summary>
  52. /// <param name="name">Name.</param>
  53. /// <param name="stringValue">String value.</param>
  54. public ChannelOption(string name, string stringValue)
  55. {
  56. this.type = OptionType.String;
  57. this.name = GrpcPreconditions.CheckNotNull(name, "name");
  58. this.stringValue = GrpcPreconditions.CheckNotNull(stringValue, "stringValue");
  59. }
  60. /// <summary>
  61. /// Creates a channel option with an integer value.
  62. /// </summary>
  63. /// <param name="name">Name.</param>
  64. /// <param name="intValue">Integer value.</param>
  65. public ChannelOption(string name, int intValue)
  66. {
  67. this.type = OptionType.Integer;
  68. this.name = GrpcPreconditions.CheckNotNull(name, "name");
  69. this.intValue = intValue;
  70. }
  71. /// <summary>
  72. /// Gets the type of the <c>ChannelOption</c>.
  73. /// </summary>
  74. public OptionType Type
  75. {
  76. get
  77. {
  78. return type;
  79. }
  80. }
  81. /// <summary>
  82. /// Gets the name of the <c>ChannelOption</c>.
  83. /// </summary>
  84. public string Name
  85. {
  86. get
  87. {
  88. return name;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets the integer value the <c>ChannelOption</c>.
  93. /// </summary>
  94. public int IntValue
  95. {
  96. get
  97. {
  98. GrpcPreconditions.CheckState(type == OptionType.Integer);
  99. return intValue;
  100. }
  101. }
  102. /// <summary>
  103. /// Gets the string value the <c>ChannelOption</c>.
  104. /// </summary>
  105. public string StringValue
  106. {
  107. get
  108. {
  109. GrpcPreconditions.CheckState(type == OptionType.String);
  110. return stringValue;
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Defines names of supported channel options.
  116. /// </summary>
  117. public static class ChannelOptions
  118. {
  119. /// <summary>Override SSL target check. Only to be used for testing.</summary>
  120. public const string SslTargetNameOverride = "grpc.ssl_target_name_override";
  121. /// <summary>Enable census for tracing and stats collection</summary>
  122. public const string Census = "grpc.census";
  123. /// <summary>Maximum number of concurrent incoming streams to allow on a http2 connection</summary>
  124. public const string MaxConcurrentStreams = "grpc.max_concurrent_streams";
  125. /// <summary>Maximum message length that the channel can receive</summary>
  126. public const string MaxReceiveMessageLength = "grpc.max_receive_message_length";
  127. /// <summary>Maximum message length that the channel can send</summary>
  128. public const string MaxSendMessageLength = "grpc.max_send_message_length";
  129. /// <summary>Obsolete, for backward compatibility only.</summary>
  130. [Obsolete("Use MaxReceiveMessageLength instead.")]
  131. public const string MaxMessageLength = MaxReceiveMessageLength;
  132. /// <summary>Initial sequence number for http2 transports</summary>
  133. public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number";
  134. /// <summary>Default authority for calls.</summary>
  135. public const string DefaultAuthority = "grpc.default_authority";
  136. /// <summary>Primary user agent: goes at the start of the user-agent metadata</summary>
  137. public const string PrimaryUserAgentString = "grpc.primary_user_agent";
  138. /// <summary>Secondary user agent: goes at the end of the user-agent metadata</summary>
  139. public const string SecondaryUserAgentString = "grpc.secondary_user_agent";
  140. /// <summary>If non-zero, allow the use of SO_REUSEPORT for server if it's available (default 1)</summary>
  141. public const string SoReuseport = "grpc.so_reuseport";
  142. /// <summary>
  143. /// Creates native object for a collection of channel options.
  144. /// </summary>
  145. /// <returns>The native channel arguments.</returns>
  146. internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection<ChannelOption> options)
  147. {
  148. if (options == null || options.Count == 0)
  149. {
  150. return ChannelArgsSafeHandle.CreateNull();
  151. }
  152. ChannelArgsSafeHandle nativeArgs = null;
  153. try
  154. {
  155. nativeArgs = ChannelArgsSafeHandle.Create(options.Count);
  156. int i = 0;
  157. foreach (var option in options)
  158. {
  159. if (option.Type == ChannelOption.OptionType.Integer)
  160. {
  161. nativeArgs.SetInteger(i, option.Name, option.IntValue);
  162. }
  163. else if (option.Type == ChannelOption.OptionType.String)
  164. {
  165. nativeArgs.SetString(i, option.Name, option.StringValue);
  166. }
  167. else
  168. {
  169. throw new InvalidOperationException("Unknown option type");
  170. }
  171. i++;
  172. }
  173. return nativeArgs;
  174. }
  175. catch (Exception)
  176. {
  177. if (nativeArgs != null)
  178. {
  179. nativeArgs.Dispose();
  180. }
  181. throw;
  182. }
  183. }
  184. }
  185. }