• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 namespace Grpc.Core.Internal
20 {
21     /// <summary>
22     /// grpc_server from grpc/grpc.h
23     /// </summary>
24     internal sealed class ServerSafeHandle : SafeHandleZeroIsInvalid
25     {
26         static readonly NativeMethods Native = NativeMethods.Get();
27 
ServerSafeHandle()28         private ServerSafeHandle()
29         {
30         }
31 
NewServer(ChannelArgsSafeHandle args)32         public static ServerSafeHandle NewServer(ChannelArgsSafeHandle args)
33         {
34             // Increment reference count for the native gRPC environment to make sure we don't do grpc_shutdown() before destroying the server handle.
35             // Doing so would make object finalizer crash if we end up abandoning the handle.
36             GrpcEnvironment.GrpcNativeInit();
37             return Native.grpcsharp_server_create(args);
38         }
39 
RegisterCompletionQueue(CompletionQueueSafeHandle cq)40         public void RegisterCompletionQueue(CompletionQueueSafeHandle cq)
41         {
42             using (cq.NewScope())
43             {
44                 Native.grpcsharp_server_register_completion_queue(this, cq);
45             }
46         }
47 
AddInsecurePort(string addr)48         public int AddInsecurePort(string addr)
49         {
50             return Native.grpcsharp_server_add_insecure_http2_port(this, addr);
51         }
52 
AddSecurePort(string addr, ServerCredentialsSafeHandle credentials)53         public int AddSecurePort(string addr, ServerCredentialsSafeHandle credentials)
54         {
55             return Native.grpcsharp_server_add_secure_http2_port(this, addr, credentials);
56         }
57 
Start()58         public void Start()
59         {
60             Native.grpcsharp_server_start(this);
61         }
62 
ShutdownAndNotify(BatchCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)63         public void ShutdownAndNotify(BatchCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)
64         {
65             using (completionQueue.NewScope())
66             {
67                 // TODO(jtattermusch): delegate allocation by caller can be avoided by utilizing the "state" object,
68                 // but server shutdown isn't worth optimizing right now.
69                 var ctx = completionQueue.CompletionRegistry.RegisterBatchCompletion(callback, null);
70                 Native.grpcsharp_server_shutdown_and_notify_callback(this, completionQueue, ctx);
71             }
72         }
73 
RequestCall(RequestCallCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)74         public void RequestCall(RequestCallCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)
75         {
76             using (completionQueue.NewScope())
77             {
78                 var ctx = completionQueue.CompletionRegistry.RegisterRequestCallCompletion(callback);
79                 Native.grpcsharp_server_request_call(this, completionQueue, ctx).CheckOk();
80             }
81         }
82 
ReleaseHandle()83         protected override bool ReleaseHandle()
84         {
85             Native.grpcsharp_server_destroy(handle);
86             GrpcEnvironment.GrpcNativeShutdown();
87             return true;
88         }
89 
90         // Only to be called after ShutdownAndNotify.
CancelAllCalls()91         public void CancelAllCalls()
92         {
93             Native.grpcsharp_server_cancel_all_calls(this);
94         }
95     }
96 }
97