OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 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. |
| 18 // |
| 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. |
| 30 #endregion |
| 31 using System; |
| 32 using System.Runtime.InteropServices; |
| 33 using System.Threading; |
| 34 using System.Threading.Tasks; |
| 35 using Grpc.Core.Profiling; |
| 36 |
| 37 namespace Grpc.Core.Internal |
| 38 { |
| 39 /// <summary> |
| 40 /// grpc_channel from <c>grpc/grpc.h</c> |
| 41 /// </summary> |
| 42 internal class ChannelSafeHandle : SafeHandleZeroIsInvalid |
| 43 { |
| 44 static readonly NativeMethods Native = NativeMethods.Get(); |
| 45 |
| 46 private ChannelSafeHandle() |
| 47 { |
| 48 } |
| 49 |
| 50 public static ChannelSafeHandle CreateInsecure(string target, ChannelArg
sSafeHandle channelArgs) |
| 51 { |
| 52 // Increment reference count for the native gRPC environment to make
sure we don't do grpc_shutdown() before destroying the server handle. |
| 53 // Doing so would make object finalizer crash if we end up abandonin
g the handle. |
| 54 GrpcEnvironment.GrpcNativeInit(); |
| 55 return Native.grpcsharp_insecure_channel_create(target, channelArgs)
; |
| 56 } |
| 57 |
| 58 public static ChannelSafeHandle CreateSecure(ChannelCredentialsSafeHandl
e credentials, string target, ChannelArgsSafeHandle channelArgs) |
| 59 { |
| 60 // Increment reference count for the native gRPC environment to make
sure we don't do grpc_shutdown() before destroying the server handle. |
| 61 // Doing so would make object finalizer crash if we end up abandonin
g the handle. |
| 62 GrpcEnvironment.GrpcNativeInit(); |
| 63 return Native.grpcsharp_secure_channel_create(credentials, target, c
hannelArgs); |
| 64 } |
| 65 |
| 66 public CallSafeHandle CreateCall(CompletionRegistry registry, CallSafeHa
ndle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHan
dle cq, string method, string host, Timespec deadline, CallCredentialsSafeHandle
credentials) |
| 67 { |
| 68 using (Profilers.ForCurrentThread().NewScope("ChannelSafeHandle.Crea
teCall")) |
| 69 { |
| 70 var result = Native.grpcsharp_channel_create_call(this, parentCa
ll, propagationMask, cq, method, host, deadline); |
| 71 if (credentials != null) |
| 72 { |
| 73 result.SetCredentials(credentials); |
| 74 } |
| 75 result.Initialize(registry, cq); |
| 76 return result; |
| 77 } |
| 78 } |
| 79 |
| 80 public ChannelState CheckConnectivityState(bool tryToConnect) |
| 81 { |
| 82 return Native.grpcsharp_channel_check_connectivity_state(this, tryTo
Connect ? 1 : 0); |
| 83 } |
| 84 |
| 85 public void WatchConnectivityState(ChannelState lastObservedState, Times
pec deadline, CompletionQueueSafeHandle cq, |
| 86 CompletionRegistry completionRegistry, BatchCompletionDelegate callb
ack) |
| 87 { |
| 88 var ctx = BatchContextSafeHandle.Create(); |
| 89 completionRegistry.RegisterBatchCompletion(ctx, callback); |
| 90 Native.grpcsharp_channel_watch_connectivity_state(this, lastObserved
State, deadline, cq, ctx); |
| 91 } |
| 92 |
| 93 public string GetTarget() |
| 94 { |
| 95 using (var cstring = Native.grpcsharp_channel_get_target(this)) |
| 96 { |
| 97 return cstring.GetValue(); |
| 98 } |
| 99 } |
| 100 |
| 101 protected override bool ReleaseHandle() |
| 102 { |
| 103 Native.grpcsharp_channel_destroy(handle); |
| 104 GrpcEnvironment.GrpcNativeShutdown(); |
| 105 return true; |
| 106 } |
| 107 } |
| 108 } |
OLD | NEW |