blob: 18e6f2fda578ddc5f7b89958a8b1ff6c93a7abea [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08002// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08003// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08009// * 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 Tiller190d3602015-02-18 09:23:38 -080018//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080019// 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 Tattermuscha7fff862015-02-13 11:08:08 -080030#endregion
Jan Tattermusch766d72b2015-07-21 20:09:25 -070031
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032using System;
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070033using System.Collections.Generic;
Jan Tattermusch766d72b2015-07-21 20:09:25 -070034using System.Linq;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080035using System.Runtime.InteropServices;
36using System.Threading;
37using System.Threading.Tasks;
Jan Tattermusch766d72b2015-07-21 20:09:25 -070038
Jan Tattermusch30868622015-02-19 09:22:33 -080039using Grpc.Core.Internal;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080040
Jan Tattermusch30868622015-02-19 09:22:33 -080041namespace Grpc.Core
Jan Tattermuscha7608b02015-02-03 17:54:38 -080042{
Jan Tattermusch286975f2015-03-12 14:04:36 -070043 /// <summary>
44 /// gRPC Channel
45 /// </summary>
Jan Tattermusch15329232015-03-02 15:32:47 -080046 public class Channel : IDisposable
47 {
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070048 readonly GrpcEnvironment environment;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080049 readonly ChannelSafeHandle handle;
Jan Tattermusch766d72b2015-07-21 20:09:25 -070050 readonly List<ChannelOption> options;
Jan Tattermusch075dde42015-03-11 18:21:00 -070051 readonly string target;
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070052 bool disposed;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080053
Jan Tattermusch15329232015-03-02 15:32:47 -080054 /// <summary>
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070055 /// 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 Tattermusch15329232015-03-02 15:32:47 -080057 /// </summary>
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070058 /// <param name="host">The DNS name of IP address of the host.</param>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070059 /// <param name="credentials">Credentials to secure the channel.</param>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070060 /// <param name="options">Channel options.</param>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070061 public Channel(string host, Credentials credentials, IEnumerable<ChannelOption> options = null)
Jan Tattermusch15329232015-03-02 15:32:47 -080062 {
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070063 this.environment = GrpcEnvironment.GetInstance();
Jan Tattermusch766d72b2015-07-21 20:09:25 -070064 this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>();
65
66 EnsureUserAgentChannelOption(this.options);
Jan Tattermuscha96ac052015-07-24 14:49:30 -070067 using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
Jan Tattermusch766d72b2015-07-21 20:09:25 -070068 using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options))
Jan Tattermusch15329232015-03-02 15:32:47 -080069 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070070 if (nativeCredentials != null)
Jan Tattermusch15329232015-03-02 15:32:47 -080071 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070072 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
Jan Tattermusch15329232015-03-02 15:32:47 -080073 }
74 else
75 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070076 this.handle = ChannelSafeHandle.CreateInsecure(host, nativeChannelArgs);
Jan Tattermusch15329232015-03-02 15:32:47 -080077 }
78 }
Jan Tattermusch766d72b2015-07-21 20:09:25 -070079 this.target = GetOverridenTarget(host, this.options);
Jan Tattermusch15329232015-03-02 15:32:47 -080080 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080081
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070082 /// <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 Tattermuscha96ac052015-07-24 14:49:30 -070087 /// <param name="credentials">Credentials to secure the channel.</param>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070088 /// <param name="options">Channel options.</param>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070089 public Channel(string host, int port, Credentials credentials, IEnumerable<ChannelOption> options = null) :
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070090 this(string.Format("{0}:{1}", host, port), credentials, options)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080091 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -080092 }
93
94 public void Dispose()
95 {
96 Dispose(true);
97 GC.SuppressFinalize(this);
98 }
99
Jan Tattermuschda71a4d2015-06-08 15:36:53 -0700100 internal string Target
101 {
102 get
103 {
104 return target;
105 }
106 }
107
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700108 internal ChannelSafeHandle Handle
109 {
110 get
111 {
112 return this.handle;
113 }
114 }
115
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700116 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 Tattermuscha7608b02015-02-03 17:54:38 -0800140 protected virtual void Dispose(bool disposing)
141 {
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700142 if (disposing && handle != null && !disposed)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800143 {
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700144 disposed = true;
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800145 handle.Dispose();
146 }
147 }
Jan Tattermusch15329232015-03-02 15:32:47 -0800148
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700149 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 Tattermusch2ddb5a62015-06-08 17:51:36 -0700163 /// <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 Tattermusch15329232015-03-02 15:32:47 -0800168 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700169 if (options == null)
Jan Tattermusch15329232015-03-02 15:32:47 -0800170 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700171 return originalTarget;
Jan Tattermusch15329232015-03-02 15:32:47 -0800172 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700173 foreach (var option in options)
Jan Tattermusch15329232015-03-02 15:32:47 -0800174 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700175 if (option.Type == ChannelOption.OptionType.String
176 && option.Name == ChannelOptions.SslTargetNameOverride)
177 {
178 return option.StringValue;
179 }
Jan Tattermusch15329232015-03-02 15:32:47 -0800180 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700181 return originalTarget;
Jan Tattermusch15329232015-03-02 15:32:47 -0800182 }
183 }
Craig Tiller190d3602015-02-18 09:23:38 -0800184}