Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 2 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 3 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 18 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 30 | #endregion |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 31 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 32 | using System; |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 33 | using System.Collections.Generic; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 34 | using System.Linq; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | using System.Runtime.InteropServices; |
| 36 | using System.Threading; |
| 37 | using System.Threading.Tasks; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 38 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core.Internal; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 41 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 43 | /// <summary> |
| 44 | /// gRPC Channel |
| 45 | /// </summary> |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 46 | public class Channel : IDisposable |
| 47 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 48 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | readonly ChannelSafeHandle handle; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 50 | readonly List<ChannelOption> options; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 51 | readonly string target; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 52 | bool disposed; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 53 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 54 | /// <summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 55 | /// Creates a channel that connects to a specific host. |
| 56 | /// Port will default to 80 for an unsecure channel and to 443 a secure channel. |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 57 | /// </summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 58 | /// <param name="host">The DNS name of IP address of the host.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 59 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 60 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 61 | public Channel(string host, Credentials credentials, IEnumerable<ChannelOption> options = null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 62 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 63 | this.environment = GrpcEnvironment.GetInstance(); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 64 | this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); |
| 65 | |
| 66 | EnsureUserAgentChannelOption(this.options); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 67 | using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 68 | using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options)) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 69 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 70 | if (nativeCredentials != null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 71 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 72 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 73 | } |
| 74 | else |
| 75 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 76 | this.handle = ChannelSafeHandle.CreateInsecure(host, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 79 | this.target = GetOverridenTarget(host, this.options); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 80 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 81 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 82 | /// <summary> |
| 83 | /// Creates a channel that connects to a specific host and port. |
| 84 | /// </summary> |
| 85 | /// <param name="host">DNS name or IP address</param> |
| 86 | /// <param name="port">the port</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 87 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 88 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 89 | public Channel(string host, int port, Credentials credentials, IEnumerable<ChannelOption> options = null) : |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 90 | this(string.Format("{0}:{1}", host, port), credentials, options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 91 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public void Dispose() |
| 95 | { |
| 96 | Dispose(true); |
| 97 | GC.SuppressFinalize(this); |
| 98 | } |
| 99 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 100 | internal string Target |
| 101 | { |
| 102 | get |
| 103 | { |
| 104 | return target; |
| 105 | } |
| 106 | } |
| 107 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 108 | internal ChannelSafeHandle Handle |
| 109 | { |
| 110 | get |
| 111 | { |
| 112 | return this.handle; |
| 113 | } |
| 114 | } |
| 115 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 116 | internal CompletionQueueSafeHandle CompletionQueue |
| 117 | { |
| 118 | get |
| 119 | { |
| 120 | return this.environment.CompletionQueue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | internal CompletionRegistry CompletionRegistry |
| 125 | { |
| 126 | get |
| 127 | { |
| 128 | return this.environment.CompletionRegistry; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | internal GrpcEnvironment Environment |
| 133 | { |
| 134 | get |
| 135 | { |
| 136 | return this.environment; |
| 137 | } |
| 138 | } |
| 139 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 140 | protected virtual void Dispose(bool disposing) |
| 141 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 142 | if (disposing && handle != null && !disposed) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 143 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 144 | disposed = true; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 145 | handle.Dispose(); |
| 146 | } |
| 147 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 148 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 149 | private static void EnsureUserAgentChannelOption(List<ChannelOption> options) |
| 150 | { |
| 151 | if (!options.Any((option) => option.Name == ChannelOptions.PrimaryUserAgentString)) |
| 152 | { |
| 153 | options.Add(new ChannelOption(ChannelOptions.PrimaryUserAgentString, GetUserAgentString())); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private static string GetUserAgentString() |
| 158 | { |
| 159 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 160 | return string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 161 | } |
| 162 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 163 | /// <summary> |
| 164 | /// Look for SslTargetNameOverride option and return its value instead of originalTarget |
| 165 | /// if found. |
| 166 | /// </summary> |
| 167 | private static string GetOverridenTarget(string originalTarget, IEnumerable<ChannelOption> options) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 168 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 169 | if (options == null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 170 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 171 | return originalTarget; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 172 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 173 | foreach (var option in options) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 174 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 175 | if (option.Type == ChannelOption.OptionType.String |
| 176 | && option.Name == ChannelOptions.SslTargetNameOverride) |
| 177 | { |
| 178 | return option.StringValue; |
| 179 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 180 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 181 | return originalTarget; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 184 | } |