blob: d6bfbb7bc458aea5dbcb143bd7f4e492dcab7143 [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 Tattermuscha7608b02015-02-03 17:54:38 -080031using System;
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070032using System.Collections.Generic;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080033using System.Runtime.InteropServices;
34using System.Threading;
35using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080036using Grpc.Core.Internal;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080037
Jan Tattermusch30868622015-02-19 09:22:33 -080038namespace Grpc.Core
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039{
Jan Tattermusch286975f2015-03-12 14:04:36 -070040 /// <summary>
41 /// gRPC Channel
42 /// </summary>
Jan Tattermusch15329232015-03-02 15:32:47 -080043 public class Channel : IDisposable
44 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -080045 readonly ChannelSafeHandle handle;
Jan Tattermusch075dde42015-03-11 18:21:00 -070046 readonly string target;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080047
Jan Tattermusch15329232015-03-02 15:32:47 -080048 /// <summary>
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070049 /// 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.
Jan Tattermusch15329232015-03-02 15:32:47 -080051 /// </summary>
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070052 /// <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>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070054 /// <param name="options">Channel options.</param>
55 public Channel(string host, Credentials credentials = null, IEnumerable<ChannelOption> options = null)
Jan Tattermusch15329232015-03-02 15:32:47 -080056 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070057 using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(options))
Jan Tattermusch15329232015-03-02 15:32:47 -080058 {
59 if (credentials != null)
60 {
61 using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
62 {
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070063 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
Jan Tattermusch15329232015-03-02 15:32:47 -080064 }
65 }
66 else
67 {
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070068 this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs);
Jan Tattermusch15329232015-03-02 15:32:47 -080069 }
70 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070071 this.target = GetOverridenTarget(host, options);
Jan Tattermusch15329232015-03-02 15:32:47 -080072 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080073
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070074 /// <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>
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -070080 /// <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)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080083 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -080084 }
85
86 public void Dispose()
87 {
88 Dispose(true);
89 GC.SuppressFinalize(this);
90 }
91
Jan Tattermuschda71a4d2015-06-08 15:36:53 -070092 internal string Target
93 {
94 get
95 {
96 return target;
97 }
98 }
99
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700100 internal ChannelSafeHandle Handle
101 {
102 get
103 {
104 return this.handle;
105 }
106 }
107
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800108 protected virtual void Dispose(bool disposing)
109 {
110 if (handle != null && !handle.IsInvalid)
111 {
112 handle.Dispose();
113 }
114 }
Jan Tattermusch15329232015-03-02 15:32:47 -0800115
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700116 /// <summary>
117 /// Look for SslTargetNameOverride option and return its value instead of originalTarget
118 /// if found.
119 /// </summary>
120 private static string GetOverridenTarget(string originalTarget, IEnumerable<ChannelOption> options)
Jan Tattermusch15329232015-03-02 15:32:47 -0800121 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700122 if (options == null)
Jan Tattermusch15329232015-03-02 15:32:47 -0800123 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700124 return originalTarget;
Jan Tattermusch15329232015-03-02 15:32:47 -0800125 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700126 foreach (var option in options)
Jan Tattermusch15329232015-03-02 15:32:47 -0800127 {
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700128 if (option.Type == ChannelOption.OptionType.String
129 && option.Name == ChannelOptions.SslTargetNameOverride)
130 {
131 return option.StringValue;
132 }
Jan Tattermusch15329232015-03-02 15:32:47 -0800133 }
Jan Tattermusch2ddb5a62015-06-08 17:51:36 -0700134 return originalTarget;
Jan Tattermusch15329232015-03-02 15:32:47 -0800135 }
136 }
Craig Tiller190d3602015-02-18 09:23:38 -0800137}