blob: 3a42dac1d7a54fe8610b5153f69faec12785bbe3 [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;
32using System.Runtime.InteropServices;
33using System.Threading;
34using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080035using Grpc.Core.Internal;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080036
Jan Tattermusch30868622015-02-19 09:22:33 -080037namespace Grpc.Core
Jan Tattermuscha7608b02015-02-03 17:54:38 -080038{
Jan Tattermusch286975f2015-03-12 14:04:36 -070039 /// <summary>
40 /// gRPC Channel
41 /// </summary>
Jan Tattermusch15329232015-03-02 15:32:47 -080042 public class Channel : IDisposable
43 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -080044 readonly ChannelSafeHandle handle;
Jan Tattermusch075dde42015-03-11 18:21:00 -070045 readonly string target;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080046
Jan Tattermusch15329232015-03-02 15:32:47 -080047 /// <summary>
48 /// Creates a channel.
49 /// </summary>
50 public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null)
51 {
52 using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs))
53 {
54 if (credentials != null)
55 {
56 using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
57 {
58 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs);
59 }
60 }
61 else
62 {
63 this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs);
64 }
65 }
66 this.target = GetOverridenTarget(target, channelArgs);
67 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080068
69 internal ChannelSafeHandle Handle
70 {
71 get
72 {
73 return this.handle;
74 }
75 }
76
77 public string Target
78 {
79 get
80 {
81 return this.target;
82 }
83 }
84
85 public void Dispose()
86 {
87 Dispose(true);
88 GC.SuppressFinalize(this);
89 }
90
91 protected virtual void Dispose(bool disposing)
92 {
93 if (handle != null && !handle.IsInvalid)
94 {
95 handle.Dispose();
96 }
97 }
Jan Tattermusch15329232015-03-02 15:32:47 -080098
99 private static string GetOverridenTarget(string target, ChannelArgs args)
100 {
101 if (args != null && !string.IsNullOrEmpty(args.GetSslTargetNameOverride()))
102 {
103 return args.GetSslTargetNameOverride();
104 }
105 return target;
106 }
107
108 private static ChannelArgsSafeHandle CreateNativeChannelArgs(ChannelArgs args)
109 {
110 if (args == null)
111 {
112 return ChannelArgsSafeHandle.CreateNull();
113 }
114 return args.ToNativeChannelArgs();
115 }
116 }
Craig Tiller190d3602015-02-18 09:23:38 -0800117}