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 | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 40 | using Grpc.Core.Logging; |
| 41 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 43 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 45 | /// <summary> |
| 46 | /// gRPC Channel |
| 47 | /// </summary> |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 48 | public class Channel : IDisposable |
| 49 | { |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 50 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Channel>(); |
| 51 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 52 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 53 | readonly ChannelSafeHandle handle; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 54 | readonly List<ChannelOption> options; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 55 | readonly string target; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 56 | bool disposed; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 57 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 58 | /// <summary> |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 59 | /// Creates a channel that connects to a specific host. |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 60 | /// Port will default to 80 for an unsecure channel and to 443 for a secure channel. |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 61 | /// </summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 62 | /// <param name="host">The name or IP address of the host.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 63 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 64 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 65 | public Channel(string host, Credentials credentials, IEnumerable<ChannelOption> options = null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 66 | { |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 67 | Preconditions.CheckNotNull(host); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 68 | this.environment = GrpcEnvironment.GetInstance(); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 69 | this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); |
| 70 | |
| 71 | EnsureUserAgentChannelOption(this.options); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 72 | using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 73 | using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options)) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 74 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 75 | if (nativeCredentials != null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 76 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 77 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 78 | } |
| 79 | else |
| 80 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 81 | this.handle = ChannelSafeHandle.CreateInsecure(host, nativeChannelArgs); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 84 | this.target = GetOverridenTarget(host, this.options); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 85 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 86 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 87 | /// <summary> |
| 88 | /// Creates a channel that connects to a specific host and port. |
| 89 | /// </summary> |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 90 | /// <param name="host">The name or IP address of the host.</param> |
| 91 | /// <param name="port">The port.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 92 | /// <param name="credentials">Credentials to secure the channel.</param> |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 93 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 94 | public Channel(string host, int port, Credentials credentials, IEnumerable<ChannelOption> options = null) : |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 95 | this(string.Format("{0}:{1}", host, port), credentials, options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 96 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jan Tattermusch | d8bbdea | 2015-07-22 12:51:06 -0700 | [diff] [blame^] | 99 | /// <summary> |
| 100 | /// Gets current connectivity state of this channel. |
| 101 | /// </summary> |
| 102 | public ChannelState State |
| 103 | { |
| 104 | get |
| 105 | { |
| 106 | return handle.CheckConnectivityState(false); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Returned tasks completes once channel state has become different from |
| 112 | /// given lastObservedState. |
| 113 | /// If deadline is reached or and error occurs, returned task is cancelled. |
| 114 | /// </summary> |
| 115 | public Task WaitForStateChangedAsync(ChannelState lastObservedState, DateTime? deadline = null) |
| 116 | { |
| 117 | Preconditions.CheckArgument(lastObservedState != ChannelState.FatalFailure, |
| 118 | "FatalFailure is a terminal state. No further state changes can occur."); |
| 119 | var tcs = new TaskCompletionSource<object>(); |
| 120 | var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; |
| 121 | var handler = new BatchCompletionDelegate((success, ctx) => |
| 122 | { |
| 123 | if (success) |
| 124 | { |
| 125 | tcs.SetResult(null); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | tcs.SetCanceled(); |
| 130 | } |
| 131 | }); |
| 132 | handle.WatchConnectivityState(lastObservedState, deadlineTimespec, environment.CompletionQueue, environment.CompletionRegistry, handler); |
| 133 | return tcs.Task; |
| 134 | } |
| 135 | |
| 136 | /// <summary> |
| 137 | /// Allows explicitly requesting channel to connect without starting an RPC. |
| 138 | /// Returned task completes once state Ready was seen. If the deadline is reached, |
| 139 | /// or channel enters the FatalFailure state, the task is cancelled. |
| 140 | /// There is no need to call this explicitly unless your use case requires that. |
| 141 | /// Starting an RPC on a new channel will request connection implicitly. |
| 142 | /// </summary> |
| 143 | public async Task ConnectAsync(DateTime? deadline = null) |
| 144 | { |
| 145 | var currentState = handle.CheckConnectivityState(true); |
| 146 | while (currentState != ChannelState.Ready) |
| 147 | { |
| 148 | if (currentState == ChannelState.FatalFailure) |
| 149 | { |
| 150 | throw new OperationCanceledException("Channel has reached FatalFailure state."); |
| 151 | } |
| 152 | await WaitForStateChangedAsync(currentState, deadline); |
| 153 | currentState = handle.CheckConnectivityState(false); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// <summary> |
| 158 | /// Destroys the underlying channel. |
| 159 | /// </summary> |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 160 | public void Dispose() |
| 161 | { |
| 162 | Dispose(true); |
| 163 | GC.SuppressFinalize(this); |
| 164 | } |
| 165 | |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 166 | internal string Target |
| 167 | { |
| 168 | get |
| 169 | { |
| 170 | return target; |
| 171 | } |
| 172 | } |
| 173 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 174 | internal ChannelSafeHandle Handle |
| 175 | { |
| 176 | get |
| 177 | { |
| 178 | return this.handle; |
| 179 | } |
| 180 | } |
| 181 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 182 | internal CompletionQueueSafeHandle CompletionQueue |
| 183 | { |
| 184 | get |
| 185 | { |
| 186 | return this.environment.CompletionQueue; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | internal CompletionRegistry CompletionRegistry |
| 191 | { |
| 192 | get |
| 193 | { |
| 194 | return this.environment.CompletionRegistry; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | internal GrpcEnvironment Environment |
| 199 | { |
| 200 | get |
| 201 | { |
| 202 | return this.environment; |
| 203 | } |
| 204 | } |
| 205 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 206 | protected virtual void Dispose(bool disposing) |
| 207 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 208 | if (disposing && handle != null && !disposed) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 209 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 210 | disposed = true; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 211 | handle.Dispose(); |
| 212 | } |
| 213 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 214 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 215 | private static void EnsureUserAgentChannelOption(List<ChannelOption> options) |
| 216 | { |
| 217 | if (!options.Any((option) => option.Name == ChannelOptions.PrimaryUserAgentString)) |
| 218 | { |
| 219 | options.Add(new ChannelOption(ChannelOptions.PrimaryUserAgentString, GetUserAgentString())); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private static string GetUserAgentString() |
| 224 | { |
| 225 | // TODO(jtattermusch): it would be useful to also provide .NET/mono version. |
| 226 | return string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); |
| 227 | } |
| 228 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 229 | /// <summary> |
| 230 | /// Look for SslTargetNameOverride option and return its value instead of originalTarget |
| 231 | /// if found. |
| 232 | /// </summary> |
| 233 | private static string GetOverridenTarget(string originalTarget, IEnumerable<ChannelOption> options) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 234 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 235 | if (options == null) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 236 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 237 | return originalTarget; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 238 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 239 | foreach (var option in options) |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 240 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 241 | if (option.Type == ChannelOption.OptionType.String |
| 242 | && option.Name == ChannelOptions.SslTargetNameOverride) |
| 243 | { |
| 244 | return option.StringValue; |
| 245 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 246 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 247 | return originalTarget; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 250 | } |