Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | using System.Collections.Concurrent; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using System.Diagnostics; |
| 37 | using System.Runtime.InteropServices; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 38 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 39 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 40 | namespace Grpc.Core.Internal |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 41 | { |
| 42 | /// <summary> |
| 43 | /// grpc_server from grpc/grpc.h |
| 44 | /// </summary> |
| 45 | internal sealed class ServerSafeHandle : SafeHandleZeroIsInvalid |
| 46 | { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 47 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 48 | static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 50 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 51 | static extern int grpcsharp_server_add_http2_port(ServerSafeHandle server, string addr); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 52 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 53 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 54 | static extern int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 55 | |
| 56 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 57 | static extern void grpcsharp_server_start(ServerSafeHandle server); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 58 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 59 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 60 | static extern GRPCCallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | |
Jan Tattermusch | 1215c33 | 2015-05-04 19:39:59 -0700 | [diff] [blame] | 62 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 63 | static extern void grpcsharp_server_cancel_all_calls(ServerSafeHandle server); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 64 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 65 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 9bfedd6 | 2015-06-09 17:47:56 -0700 | [diff] [blame] | 66 | static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 67 | |
Jan Tattermusch | 20b738d | 2015-02-11 16:26:12 -0800 | [diff] [blame] | 68 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 69 | static extern void grpcsharp_server_destroy(IntPtr server); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 70 | |
| 71 | private ServerSafeHandle() |
| 72 | { |
| 73 | } |
| 74 | |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 75 | public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 76 | { |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 77 | return grpcsharp_server_create(cq, args); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 80 | public int AddListeningPort(string addr) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 81 | { |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 82 | return grpcsharp_server_add_http2_port(this, addr); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 85 | public int AddListeningPort(string addr, ServerCredentialsSafeHandle credentials) |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 86 | { |
| 87 | return grpcsharp_server_add_secure_http2_port(this, addr, credentials); |
| 88 | } |
| 89 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 90 | public void Start() |
| 91 | { |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 92 | grpcsharp_server_start(this); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 93 | } |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 94 | |
| 95 | public void ShutdownAndNotify(BatchCompletionDelegate callback, GrpcEnvironment environment) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 96 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 97 | var ctx = BatchContextSafeHandle.Create(); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 98 | environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); |
| 99 | grpcsharp_server_shutdown_and_notify_callback(this, environment.CompletionQueue, ctx); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 102 | public void RequestCall(BatchCompletionDelegate callback, GrpcEnvironment environment) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 103 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 104 | var ctx = BatchContextSafeHandle.Create(); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 105 | environment.CompletionRegistry.RegisterBatchCompletion(ctx, callback); |
| 106 | grpcsharp_server_request_call(this, environment.CompletionQueue, ctx).CheckOk(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | protected override bool ReleaseHandle() |
| 110 | { |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 111 | grpcsharp_server_destroy(handle); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 112 | return true; |
| 113 | } |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 114 | |
| 115 | // Only to be called after ShutdownAndNotify. |
| 116 | public void CancelAllCalls() |
| 117 | { |
| 118 | grpcsharp_server_cancel_all_calls(this); |
| 119 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 120 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 121 | } |