OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Concurrent; |
| 36 using System.Diagnostics; |
| 37 using System.Runtime.InteropServices; |
| 38 using Grpc.Core.Utils; |
| 39 |
| 40 namespace Grpc.Core.Internal |
| 41 { |
| 42 /// <summary> |
| 43 /// grpc_server from grpc/grpc.h |
| 44 /// </summary> |
| 45 internal sealed class ServerSafeHandle : SafeHandleZeroIsInvalid |
| 46 { |
| 47 static readonly NativeMethods Native = NativeMethods.Get(); |
| 48 |
| 49 private ServerSafeHandle() |
| 50 { |
| 51 } |
| 52 |
| 53 public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, C
hannelArgsSafeHandle args) |
| 54 { |
| 55 // Increment reference count for the native gRPC environment to make
sure we don't do grpc_shutdown() before destroying the server handle. |
| 56 // Doing so would make object finalizer crash if we end up abandonin
g the handle. |
| 57 GrpcEnvironment.GrpcNativeInit(); |
| 58 return Native.grpcsharp_server_create(cq, args); |
| 59 } |
| 60 |
| 61 public int AddInsecurePort(string addr) |
| 62 { |
| 63 return Native.grpcsharp_server_add_insecure_http2_port(this, addr); |
| 64 } |
| 65 |
| 66 public int AddSecurePort(string addr, ServerCredentialsSafeHandle creden
tials) |
| 67 { |
| 68 return Native.grpcsharp_server_add_secure_http2_port(this, addr, cre
dentials); |
| 69 } |
| 70 |
| 71 public void Start() |
| 72 { |
| 73 Native.grpcsharp_server_start(this); |
| 74 } |
| 75 |
| 76 public void ShutdownAndNotify(BatchCompletionDelegate callback, GrpcEnvi
ronment environment) |
| 77 { |
| 78 var ctx = BatchContextSafeHandle.Create(); |
| 79 environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback
); |
| 80 Native.grpcsharp_server_shutdown_and_notify_callback(this, environme
nt.CompletionQueue, ctx); |
| 81 } |
| 82 |
| 83 public void RequestCall(BatchCompletionDelegate callback, GrpcEnvironmen
t environment) |
| 84 { |
| 85 var ctx = BatchContextSafeHandle.Create(); |
| 86 environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback
); |
| 87 Native.grpcsharp_server_request_call(this, environment.CompletionQue
ue, ctx).CheckOk(); |
| 88 } |
| 89 |
| 90 protected override bool ReleaseHandle() |
| 91 { |
| 92 Native.grpcsharp_server_destroy(handle); |
| 93 GrpcEnvironment.GrpcNativeShutdown(); |
| 94 return true; |
| 95 } |
| 96 |
| 97 // Only to be called after ShutdownAndNotify. |
| 98 public void CancelAllCalls() |
| 99 { |
| 100 Native.grpcsharp_server_cancel_all_calls(this); |
| 101 } |
| 102 } |
| 103 } |
OLD | NEW |