blob: be1426feb47bb2b750e9f5105446b394d000b1ed [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08002// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08003// All rights reserved.
Jan Tattermusch337a2dd2015-02-13 15:41:41 -08004//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Jan Tattermusch337a2dd2015-02-13 15:41:41 -08008//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08009// * 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.
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080018//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080019// 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
31#endregion
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032using System;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080033using System.Diagnostics;
Jan Tattermusch30868622015-02-19 09:22:33 -080034using System.Runtime.InteropServices;
35using Grpc.Core;
Jan Tattermusch97e294a2015-04-23 14:30:59 -070036using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080037
Jan Tattermusch30868622015-02-19 09:22:33 -080038namespace Grpc.Core.Internal
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039{
Jan Tattermuscha7608b02015-02-03 17:54:38 -080040 /// <summary>
41 /// grpc_call from <grpc/grpc.h>
42 /// </summary>
Jan Tattermusch13cd1252015-03-06 10:11:50 -080043 internal class CallSafeHandle : SafeHandleZeroIsInvalid
44 {
Jan Tattermusch075dde42015-03-11 18:21:00 -070045 const uint GRPC_WRITE_BUFFER_HINT = 1;
Jan Tattermusch04eb89c2015-06-12 13:03:05 -070046 CompletionRegistry completionRegistry;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080047
Jan Tattermusche1e11872015-02-12 10:23:37 -080048 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche1e11872015-02-12 10:23:37 -080049 static extern GRPCCallError grpcsharp_call_cancel(CallSafeHandle call);
50
51 [DllImport("grpc_csharp_ext.dll")]
52 static extern GRPCCallError grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, string description);
53
54 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080055 static extern GRPCCallError grpcsharp_call_start_unary(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070056 BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusch13cd1252015-03-06 10:11:50 -080057
58 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080059 static extern GRPCCallError grpcsharp_call_start_client_streaming(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070060 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusche1e11872015-02-12 10:23:37 -080061
62 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080063 static extern GRPCCallError grpcsharp_call_start_server_streaming(CallSafeHandle call,
Jan Tattermuschd3677482015-06-01 19:27:40 -070064 BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070065 MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusche1e11872015-02-12 10:23:37 -080066
67 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080068 static extern GRPCCallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070069 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080070
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080071 [DllImport("grpc_csharp_ext.dll")]
72 static extern GRPCCallError grpcsharp_call_send_message(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070073 BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len, WriteFlags writeFlags);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080074
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080075 [DllImport("grpc_csharp_ext.dll")]
76 static extern GRPCCallError grpcsharp_call_send_close_from_client(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070077 BatchContextSafeHandle ctx, WriteFlags writeFlags);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080078
79 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070080 static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call,
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070081 BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080082
83 [DllImport("grpc_csharp_ext.dll")]
84 static extern GRPCCallError grpcsharp_call_recv_message(CallSafeHandle call,
Jan Tattermuschd3677482015-06-01 19:27:40 -070085 BatchContextSafeHandle ctx);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080086
87 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch13cd1252015-03-06 10:11:50 -080088 static extern GRPCCallError grpcsharp_call_start_serverside(CallSafeHandle call,
Jan Tattermuschd3677482015-06-01 19:27:40 -070089 BatchContextSafeHandle ctx);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080090
Jan Tattermusch13cd1252015-03-06 10:11:50 -080091 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschbff90ac2015-08-06 21:30:26 -070092 static extern GRPCCallError grpcsharp_call_send_initial_metadata(CallSafeHandle call,
93 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
94
95 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch062c3292015-07-23 20:28:42 -070096 static extern CStringSafeHandle grpcsharp_call_get_peer(CallSafeHandle call);
97
98 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch13cd1252015-03-06 10:11:50 -080099 static extern void grpcsharp_call_destroy(IntPtr call);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800100
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800101 private CallSafeHandle()
102 {
103 }
104
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700105 public void SetCompletionRegistry(CompletionRegistry completionRegistry)
106 {
107 this.completionRegistry = completionRegistry;
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800108 }
109
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700110 public void StartUnary(BatchCompletionDelegate callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Craig Tiller190d3602015-02-18 09:23:38 -0800111 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700112 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700113 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700114 grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags)
Jan Tattermuschd3677482015-06-01 19:27:40 -0700115 .CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800116 }
117
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700118 public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800119 {
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700120 grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags)
Jan Tattermuschd3677482015-06-01 19:27:40 -0700121 .CheckOk();
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800122 }
123
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700124 public void StartClientStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Craig Tiller190d3602015-02-18 09:23:38 -0800125 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700126 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700127 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700128 grpcsharp_call_start_client_streaming(this, ctx, metadataArray, writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800129 }
130
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700131 public void StartServerStreaming(BatchCompletionDelegate callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800132 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700133 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700134 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700135 grpcsharp_call_start_server_streaming(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800136 }
137
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700138 public void StartDuplexStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800139 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700140 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700141 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700142 grpcsharp_call_start_duplex_streaming(this, ctx, metadataArray, writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800143 }
144
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700145 public void StartSendMessage(BatchCompletionDelegate callback, byte[] payload, WriteFlags writeFlags)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800146 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700147 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700148 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700149 grpcsharp_call_send_message(this, ctx, payload, new UIntPtr((ulong)payload.Length), writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800150 }
151
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700152 public void StartSendCloseFromClient(BatchCompletionDelegate callback, WriteFlags writeFlags)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800153 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700154 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700155 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700156 grpcsharp_call_send_close_from_client(this, ctx, writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800157 }
158
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700159 public void StartSendStatusFromServer(BatchCompletionDelegate callback, Status status, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800160 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700161 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700162 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700163 grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail, metadataArray, writeFlags).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800164 }
165
Jan Tattermuschd3677482015-06-01 19:27:40 -0700166 public void StartReceiveMessage(BatchCompletionDelegate callback)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800167 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700168 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700169 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschd3677482015-06-01 19:27:40 -0700170 grpcsharp_call_recv_message(this, ctx).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800171 }
172
Jan Tattermuschd3677482015-06-01 19:27:40 -0700173 public void StartServerSide(BatchCompletionDelegate callback)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800174 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700175 var ctx = BatchContextSafeHandle.Create();
Jan Tattermusch04eb89c2015-06-12 13:03:05 -0700176 completionRegistry.RegisterBatchCompletion(ctx, callback);
Jan Tattermuschd3677482015-06-01 19:27:40 -0700177 grpcsharp_call_start_serverside(this, ctx).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800178 }
179
Jan Tattermuschbff90ac2015-08-06 21:30:26 -0700180 public void SendInitialMetadata(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
181 {
182 var ctx = BatchContextSafeHandle.Create();
183 completionRegistry.RegisterBatchCompletion(ctx, callback);
184 grpcsharp_call_send_initial_metadata(this, ctx, metadataArray, writeFlags).CheckOk();
185 }
186
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800187 public void Cancel()
188 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700189 grpcsharp_call_cancel(this).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800190 }
191
192 public void CancelWithStatus(Status status)
193 {
Jan Tattermuschd3677482015-06-01 19:27:40 -0700194 grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail).CheckOk();
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800195 }
196
Jan Tattermusch062c3292015-07-23 20:28:42 -0700197 public string GetPeer()
198 {
199 using (var cstring = grpcsharp_call_get_peer(this))
200 {
201 return cstring.GetValue();
202 }
203 }
204
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800205 protected override bool ReleaseHandle()
206 {
Jan Tattermusche1e11872015-02-12 10:23:37 -0800207 grpcsharp_call_destroy(handle);
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800208 return true;
209 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800210
Jan Tattermusch075dde42015-03-11 18:21:00 -0700211 private static uint GetFlags(bool buffered)
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800212 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800213 return buffered ? 0 : GRPC_WRITE_BUFFER_HINT;
214 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800215 }
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800216}