ChannelOptions.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /// Commonly used channel option names are defined in <c>ChannelOptions</c>,
  29. /// but any of the GRPC_ARG_* channel options names defined in grpc_types.h can be used.
  30. /// </summary>
  31. public sealed class ChannelOption : IEquatable<ChannelOption>
  32. {
  33. /// <summary>
  34. /// Type of <c>ChannelOption</c>.
  35. /// </summary>
  36. public enum OptionType
  37. {
  38. /// <summary>
  39. /// Channel option with integer value.
  40. /// </summary>
  41. Integer,
  42. /// <summary>
  43. /// Channel option with string value.
  44. /// </summary>
  45. String
  46. }
  47. private readonly OptionType type;
  48. private readonly string name;
  49. private readonly int intValue;
  50. private readonly string stringValue;
  51. /// <summary>
  52. /// Creates a channel option with a string value.
  53. /// </summary>
  54. /// <param name="name">Name.</param>
  55. /// <param name="stringValue">String value.</param>
  56. public ChannelOption(string name, string stringValue)
  57. {
  58. this.type = OptionType.String;
  59. this.name = GrpcPreconditions.CheckNotNull(name, "name");
  60. this.stringValue = GrpcPreconditions.CheckNotNull(stringValue, "stringValue");
  61. }
  62. /// <summary>
  63. /// Creates a channel option with an integer value.
  64. /// </summary>
  65. /// <param name="name">Name.</param>
  66. /// <param name="intValue">Integer value.</param>
  67. public ChannelOption(string name, int intValue)
  68. {
  69. this.type = OptionType.Integer;
  70. this.name = GrpcPreconditions.CheckNotNull(name, "name");
  71. this.intValue = intValue;
  72. }
  73. /// <summary>
  74. /// Gets the type of the <c>ChannelOption</c>.
  75. /// </summary>
  76. public OptionType Type
  77. {
  78. get
  79. {
  80. return type;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the name of the <c>ChannelOption</c>.
  85. /// </summary>
  86. public string Name
  87. {
  88. get
  89. {
  90. return name;
  91. }
  92. }
  93. /// <summary>
  94. /// Gets the integer value the <c>ChannelOption</c>.
  95. /// </summary>
  96. public int IntValue
  97. {
  98. get
  99. {
  100. GrpcPreconditions.CheckState(type == OptionType.Integer);
  101. return intValue;
  102. }
  103. }
  104. /// <summary>
  105. /// Gets the string value the <c>ChannelOption</c>.
  106. /// </summary>
  107. public string StringValue
  108. {
  109. get
  110. {
  111. GrpcPreconditions.CheckState(type == OptionType.String);
  112. return stringValue;
  113. }
  114. }
  115. /// <summary>
  116. /// Determines whether the specified object is equal to the current object.
  117. /// </summary>
  118. public override bool Equals(object obj)
  119. {
  120. return Equals(obj as ChannelOption);
  121. }
  122. /// <summary>
  123. /// Determines whether the specified object is equal to the current object.
  124. /// </summary>
  125. public bool Equals(ChannelOption other)
  126. {
  127. return other != null &&
  128. type == other.type &&
  129. name == other.name &&
  130. intValue == other.intValue &&
  131. stringValue == other.stringValue;
  132. }
  133. /// <summary>
  134. /// A hash code for the current object.
  135. /// </summary>
  136. public override int GetHashCode()
  137. {
  138. var hashCode = 1412678443;
  139. hashCode = hashCode * -1521134295 + type.GetHashCode();
  140. hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(name);
  141. hashCode = hashCode * -1521134295 + intValue.GetHashCode();
  142. hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(stringValue);
  143. return hashCode;
  144. }
  145. /// <summary>
  146. /// Equality operator.
  147. /// </summary>
  148. public static bool operator ==(ChannelOption option1, ChannelOption option2)
  149. {
  150. return EqualityComparer<ChannelOption>.Default.Equals(option1, option2);
  151. }
  152. /// <summary>
  153. /// Inequality operator.
  154. /// </summary>
  155. public static bool operator !=(ChannelOption option1, ChannelOption option2)
  156. {
  157. return !(option1 == option2);
  158. }
  159. }
  160. /// <summary>
  161. /// Defines names of most commonly used channel options.
  162. /// Other supported options names can be found in grpc_types.h (GRPC_ARG_* definitions)
  163. /// </summary>
  164. public static class ChannelOptions
  165. {
  166. /// <summary>Override SSL target check. Only to be used for testing.</summary>
  167. public const string SslTargetNameOverride = "grpc.ssl_target_name_override";
  168. /// <summary>Enable census for tracing and stats collection</summary>
  169. public const string Census = "grpc.census";
  170. /// <summary>Maximum number of concurrent incoming streams to allow on a http2 connection</summary>
  171. public const string MaxConcurrentStreams = "grpc.max_concurrent_streams";
  172. /// <summary>Maximum message length that the channel can receive</summary>
  173. public const string MaxReceiveMessageLength = "grpc.max_receive_message_length";
  174. /// <summary>Maximum message length that the channel can send</summary>
  175. public const string MaxSendMessageLength = "grpc.max_send_message_length";
  176. /// <summary>Obsolete, for backward compatibility only.</summary>
  177. [Obsolete("Use MaxReceiveMessageLength instead.")]
  178. public const string MaxMessageLength = MaxReceiveMessageLength;
  179. /// <summary>Initial sequence number for http2 transports</summary>
  180. public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number";
  181. /// <summary>Default authority for calls.</summary>
  182. public const string DefaultAuthority = "grpc.default_authority";
  183. /// <summary>Primary user agent: goes at the start of the user-agent metadata</summary>
  184. public const string PrimaryUserAgentString = "grpc.primary_user_agent";
  185. /// <summary>Secondary user agent: goes at the end of the user-agent metadata</summary>
  186. public const string SecondaryUserAgentString = "grpc.secondary_user_agent";
  187. /// <summary>If non-zero, allow the use of SO_REUSEPORT for server if it's available (default 1)</summary>
  188. public const string SoReuseport = "grpc.so_reuseport";
  189. /// <summary>
  190. /// Creates native object for a collection of channel options.
  191. /// </summary>
  192. /// <returns>The native channel arguments.</returns>
  193. internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection<ChannelOption> options)
  194. {
  195. if (options == null || options.Count == 0)
  196. {
  197. return ChannelArgsSafeHandle.CreateNull();
  198. }
  199. ChannelArgsSafeHandle nativeArgs = null;
  200. try
  201. {
  202. nativeArgs = ChannelArgsSafeHandle.Create(options.Count);
  203. int i = 0;
  204. foreach (var option in options)
  205. {
  206. if (option.Type == ChannelOption.OptionType.Integer)
  207. {
  208. nativeArgs.SetInteger(i, option.Name, option.IntValue);
  209. }
  210. else if (option.Type == ChannelOption.OptionType.String)
  211. {
  212. nativeArgs.SetString(i, option.Name, option.StringValue);
  213. }
  214. else
  215. {
  216. throw new InvalidOperationException("Unknown option type");
  217. }
  218. i++;
  219. }
  220. return nativeArgs;
  221. }
  222. catch (Exception)
  223. {
  224. if (nativeArgs != null)
  225. {
  226. nativeArgs.Dispose();
  227. }
  228. throw;
  229. }
  230. }
  231. }
  232. }