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 | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 31 | using System; |
| 32 | using System.Runtime.InteropServices; |
| 33 | using System.Threading; |
| 34 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 35 | using Grpc.Core.Internal; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 36 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | { |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame^] | 39 | public class Channel : IDisposable |
| 40 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 41 | readonly ChannelSafeHandle handle; |
| 42 | readonly String target; |
| 43 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame^] | 44 | /// <summary> |
| 45 | /// Creates a channel. |
| 46 | /// </summary> |
| 47 | public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null) |
| 48 | { |
| 49 | using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs)) |
| 50 | { |
| 51 | if (credentials != null) |
| 52 | { |
| 53 | using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) |
| 54 | { |
| 55 | this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); |
| 56 | } |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs); |
| 61 | } |
| 62 | } |
| 63 | this.target = GetOverridenTarget(target, channelArgs); |
| 64 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 65 | |
| 66 | internal ChannelSafeHandle Handle |
| 67 | { |
| 68 | get |
| 69 | { |
| 70 | return this.handle; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public string Target |
| 75 | { |
| 76 | get |
| 77 | { |
| 78 | return this.target; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public void Dispose() |
| 83 | { |
| 84 | Dispose(true); |
| 85 | GC.SuppressFinalize(this); |
| 86 | } |
| 87 | |
| 88 | protected virtual void Dispose(bool disposing) |
| 89 | { |
| 90 | if (handle != null && !handle.IsInvalid) |
| 91 | { |
| 92 | handle.Dispose(); |
| 93 | } |
| 94 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame^] | 95 | |
| 96 | private static string GetOverridenTarget(string target, ChannelArgs args) |
| 97 | { |
| 98 | if (args != null && !string.IsNullOrEmpty(args.GetSslTargetNameOverride())) |
| 99 | { |
| 100 | return args.GetSslTargetNameOverride(); |
| 101 | } |
| 102 | return target; |
| 103 | } |
| 104 | |
| 105 | private static ChannelArgsSafeHandle CreateNativeChannelArgs(ChannelArgs args) |
| 106 | { |
| 107 | if (args == null) |
| 108 | { |
| 109 | return ChannelArgsSafeHandle.CreateNull(); |
| 110 | } |
| 111 | return args.ToNativeChannelArgs(); |
| 112 | } |
| 113 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 114 | } |