blob: f9b44b1acfb60467b4f2d7dcc91bbced470ff2f0 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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
Jan Tattermuscha7608b02015-02-03 17:54:38 -080034using System;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080035using System.Collections.Concurrent;
Jan Tattermusch30868622015-02-19 09:22:33 -080036using System.Diagnostics;
37using System.Runtime.InteropServices;
Jan Tattermusch97e294a2015-04-23 14:30:59 -070038using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039
Jan Tattermusch30868622015-02-19 09:22:33 -080040namespace Grpc.Core.Internal
Jan Tattermuscha7608b02015-02-03 17:54:38 -080041{
42 /// <summary>
43 /// grpc_server from grpc/grpc.h
44 /// </summary>
45 internal sealed class ServerSafeHandle : SafeHandleZeroIsInvalid
46 {
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080047 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch6d53a5c2015-06-08 18:03:05 -070048 static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080049
Jan Tattermusche1e11872015-02-12 10:23:37 -080050 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuscha96ac052015-07-24 14:49:30 -070051 static extern int grpcsharp_server_add_insecure_http2_port(ServerSafeHandle server, string addr);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080052
Jan Tattermusche1e11872015-02-12 10:23:37 -080053 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch075dde42015-03-11 18:21:00 -070054 static extern int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080055
56 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch61f93b22015-02-12 12:45:52 -080057 static extern void grpcsharp_server_start(ServerSafeHandle server);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080058
Jan Tattermusche1e11872015-02-12 10:23:37 -080059 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070060 static extern GRPCCallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080061
Jan Tattermusch1215c332015-05-04 19:39:59 -070062 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschc4e81ad2015-05-29 17:39:07 -070063 static extern void grpcsharp_server_cancel_all_calls(ServerSafeHandle server);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080064
Jan Tattermusche1e11872015-02-12 10:23:37 -080065 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch9bfedd62015-06-09 17:47:56 -070066 static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080067
Jan Tattermusch20b738d2015-02-11 16:26:12 -080068 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch61f93b22015-02-12 12:45:52 -080069 static extern void grpcsharp_server_destroy(IntPtr server);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080070
71 private ServerSafeHandle()
72 {
73 }
74
Jan Tattermusch6d53a5c2015-06-08 18:03:05 -070075 public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080076 {
Jan Tattermusch61f93b22015-02-12 12:45:52 -080077 return grpcsharp_server_create(cq, args);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080078 }
79
Jan Tattermuscha96ac052015-07-24 14:49:30 -070080 public int AddInsecurePort(string addr)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080081 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070082 return grpcsharp_server_add_insecure_http2_port(this, addr);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080083 }
84
Jan Tattermuscha96ac052015-07-24 14:49:30 -070085 public int AddSecurePort(string addr, ServerCredentialsSafeHandle credentials)
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080086 {
87 return grpcsharp_server_add_secure_http2_port(this, addr, credentials);
88 }
89
Jan Tattermuscha7608b02015-02-03 17:54:38 -080090 public void Start()
91 {
Jan Tattermusch61f93b22015-02-12 12:45:52 -080092 grpcsharp_server_start(this);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080093 }
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070094
95 public void ShutdownAndNotify(BatchCompletionDelegate callback, GrpcEnvironment environment)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080096 {
Jan Tattermuschd3677482015-06-01 19:27:40 -070097 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070098 environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback);
99 grpcsharp_server_shutdown_and_notify_callback(this, environment.CompletionQueue, ctx);
Jan Tattermusch8ce5e8b2015-02-05 10:56:49 -0800100 }
101
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700102 public void RequestCall(BatchCompletionDelegate callback, GrpcEnvironment environment)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800103 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700104 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700105 environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback);
106 grpcsharp_server_request_call(this, environment.CompletionQueue, ctx).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800107 }
108
109 protected override bool ReleaseHandle()
110 {
Jan Tattermusch61f93b22015-02-12 12:45:52 -0800111 grpcsharp_server_destroy(handle);
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800112 return true;
113 }
Jan Tattermuschc4e81ad2015-05-29 17:39:07 -0700114
115 // Only to be called after ShutdownAndNotify.
116 public void CancelAllCalls()
117 {
118 grpcsharp_server_cancel_all_calls(this);
119 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800120 }
Craig Tiller190d3602015-02-18 09:23:38 -0800121}