Channel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #region Copyright notice and license
  2. // Copyright 2015, 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. namespace Grpc.Core
  38. {
  39. /// <summary>
  40. /// gRPC Channel
  41. /// </summary>
  42. public class Channel : IDisposable
  43. {
  44. readonly GrpcEnvironment environment;
  45. readonly ChannelSafeHandle handle;
  46. readonly string target;
  47. bool disposed;
  48. /// <summary>
  49. /// Creates a channel that connects to a specific host.
  50. /// Port will default to 80 for an unsecure channel and to 443 a secure channel.
  51. /// </summary>
  52. /// <param name="host">The DNS name of IP address of the host.</param>
  53. /// <param name="credentials">Optional credentials to create a secure channel.</param>
  54. /// <param name="options">Channel options.</param>
  55. public Channel(string host, Credentials credentials = null, IEnumerable<ChannelOption> options = null)
  56. {
  57. this.environment = GrpcEnvironment.GetInstance();
  58. using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(options))
  59. {
  60. if (credentials != null)
  61. {
  62. using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
  63. {
  64. this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
  65. }
  66. }
  67. else
  68. {
  69. this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs);
  70. }
  71. }
  72. this.target = GetOverridenTarget(host, options);
  73. }
  74. /// <summary>
  75. /// Creates a channel that connects to a specific host and port.
  76. /// </summary>
  77. /// <param name="host">DNS name or IP address</param>
  78. /// <param name="port">the port</param>
  79. /// <param name="credentials">Optional credentials to create a secure channel.</param>
  80. /// <param name="options">Channel options.</param>
  81. public Channel(string host, int port, Credentials credentials = null, IEnumerable<ChannelOption> options = null) :
  82. this(string.Format("{0}:{1}", host, port), credentials, options)
  83. {
  84. }
  85. public void Dispose()
  86. {
  87. Dispose(true);
  88. GC.SuppressFinalize(this);
  89. }
  90. internal string Target
  91. {
  92. get
  93. {
  94. return target;
  95. }
  96. }
  97. internal ChannelSafeHandle Handle
  98. {
  99. get
  100. {
  101. return this.handle;
  102. }
  103. }
  104. internal CompletionQueueSafeHandle CompletionQueue
  105. {
  106. get
  107. {
  108. return this.environment.CompletionQueue;
  109. }
  110. }
  111. internal CompletionRegistry CompletionRegistry
  112. {
  113. get
  114. {
  115. return this.environment.CompletionRegistry;
  116. }
  117. }
  118. internal GrpcEnvironment Environment
  119. {
  120. get
  121. {
  122. return this.environment;
  123. }
  124. }
  125. protected virtual void Dispose(bool disposing)
  126. {
  127. if (disposing && handle != null && !disposed)
  128. {
  129. disposed = true;
  130. handle.Dispose();
  131. }
  132. }
  133. /// <summary>
  134. /// Look for SslTargetNameOverride option and return its value instead of originalTarget
  135. /// if found.
  136. /// </summary>
  137. private static string GetOverridenTarget(string originalTarget, IEnumerable<ChannelOption> options)
  138. {
  139. if (options == null)
  140. {
  141. return originalTarget;
  142. }
  143. foreach (var option in options)
  144. {
  145. if (option.Type == ChannelOption.OptionType.String
  146. && option.Name == ChannelOptions.SslTargetNameOverride)
  147. {
  148. return option.StringValue;
  149. }
  150. }
  151. return originalTarget;
  152. }
  153. }
  154. }