blob: 20815efbd356c22e3619449871019803fd76b528 [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;
35
Jan Tattermusch30868622015-02-19 09:22:33 -080036namespace Grpc.Core.Internal
Jan Tattermuscha7608b02015-02-03 17:54:38 -080037{
38 /// <summary>
39 /// grpc_channel from <grpc/grpc.h>
40 /// </summary>
Jan Tattermusch15329232015-03-02 15:32:47 -080041 internal class ChannelSafeHandle : SafeHandleZeroIsInvalid
42 {
Jan Tattermusche1e11872015-02-12 10:23:37 -080043 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuscha96ac052015-07-24 14:49:30 -070044 static extern ChannelSafeHandle grpcsharp_insecure_channel_create(string target, ChannelArgsSafeHandle channelArgs);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080045
Jan Tattermusch15329232015-03-02 15:32:47 -080046 [DllImport("grpc_csharp_ext.dll")]
47 static extern ChannelSafeHandle grpcsharp_secure_channel_create(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs);
48
49 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch0846b682015-07-23 17:02:12 -070050 static extern CallSafeHandle grpcsharp_channel_create_call(ChannelSafeHandle channel, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline);
51
52 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch15329232015-03-02 15:32:47 -080053 static extern void grpcsharp_channel_destroy(IntPtr channel);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080054
55 private ChannelSafeHandle()
56 {
57 }
58
Jan Tattermuscha96ac052015-07-24 14:49:30 -070059 public static ChannelSafeHandle CreateInsecure(string target, ChannelArgsSafeHandle channelArgs)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080060 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070061 return grpcsharp_insecure_channel_create(target, channelArgs);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080062 }
63
Jan Tattermusch15329232015-03-02 15:32:47 -080064 public static ChannelSafeHandle CreateSecure(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs)
65 {
66 return grpcsharp_secure_channel_create(credentials, target, channelArgs);
67 }
68
Jan Tattermusch0846b682015-07-23 17:02:12 -070069 public CallSafeHandle CreateCall(CompletionRegistry registry, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline)
70 {
71 var result = grpcsharp_channel_create_call(this, cq, method, host, deadline);
72 result.SetCompletionRegistry(registry);
73 return result;
74 }
75
Jan Tattermusch15329232015-03-02 15:32:47 -080076 protected override bool ReleaseHandle()
77 {
78 grpcsharp_channel_destroy(handle);
79 return true;
80 }
81 }
Craig Tiller190d3602015-02-18 09:23:38 -080082}