blob: 65607ed12042e74760df19a70e5be7db7b2dfc61 [file] [log] [blame]
Jan Tattermuschafab5412016-01-12 17:57:25 -08001#region Copyright notice and license
2
Craig Tiller6169d5f2016-03-31 07:46:18 -07003// Copyright 2015, Google Inc.
Jan Tattermuschafab5412016-01-12 17:57:25 -08004// 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
34using System;
35using System.Collections.Concurrent;
36using System.Diagnostics;
37using System.IO;
38using System.Reflection;
39using System.Runtime.InteropServices;
40using System.Threading;
41
42using Grpc.Core.Logging;
43using Grpc.Core.Utils;
44
45namespace Grpc.Core.Internal
46{
47 /// <summary>
48 /// Provides access to all native methods provided by <c>NativeExtension</c>.
49 /// An extra level of indirection is added to P/Invoke calls to allow intelligent loading
50 /// of the right configuration of the native extension based on current platform, architecture etc.
51 /// </summary>
52 internal class NativeMethods
53 {
54 #region Native methods
55
56 public readonly Delegates.grpcsharp_init_delegate grpcsharp_init;
57 public readonly Delegates.grpcsharp_shutdown_delegate grpcsharp_shutdown;
58 public readonly Delegates.grpcsharp_version_string_delegate grpcsharp_version_string;
59
60 public readonly Delegates.grpcsharp_batch_context_create_delegate grpcsharp_batch_context_create;
61 public readonly Delegates.grpcsharp_batch_context_recv_initial_metadata_delegate grpcsharp_batch_context_recv_initial_metadata;
62 public readonly Delegates.grpcsharp_batch_context_recv_message_length_delegate grpcsharp_batch_context_recv_message_length;
63 public readonly Delegates.grpcsharp_batch_context_recv_message_to_buffer_delegate grpcsharp_batch_context_recv_message_to_buffer;
64 public readonly Delegates.grpcsharp_batch_context_recv_status_on_client_status_delegate grpcsharp_batch_context_recv_status_on_client_status;
65 public readonly Delegates.grpcsharp_batch_context_recv_status_on_client_details_delegate grpcsharp_batch_context_recv_status_on_client_details;
66 public readonly Delegates.grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate grpcsharp_batch_context_recv_status_on_client_trailing_metadata;
67 public readonly Delegates.grpcsharp_batch_context_server_rpc_new_call_delegate grpcsharp_batch_context_server_rpc_new_call;
68 public readonly Delegates.grpcsharp_batch_context_server_rpc_new_method_delegate grpcsharp_batch_context_server_rpc_new_method;
69 public readonly Delegates.grpcsharp_batch_context_server_rpc_new_host_delegate grpcsharp_batch_context_server_rpc_new_host;
70 public readonly Delegates.grpcsharp_batch_context_server_rpc_new_deadline_delegate grpcsharp_batch_context_server_rpc_new_deadline;
71 public readonly Delegates.grpcsharp_batch_context_server_rpc_new_request_metadata_delegate grpcsharp_batch_context_server_rpc_new_request_metadata;
72 public readonly Delegates.grpcsharp_batch_context_recv_close_on_server_cancelled_delegate grpcsharp_batch_context_recv_close_on_server_cancelled;
73 public readonly Delegates.grpcsharp_batch_context_destroy_delegate grpcsharp_batch_context_destroy;
74
75 public readonly Delegates.grpcsharp_composite_call_credentials_create_delegate grpcsharp_composite_call_credentials_create;
76 public readonly Delegates.grpcsharp_call_credentials_release_delegate grpcsharp_call_credentials_release;
77
78 public readonly Delegates.grpcsharp_call_cancel_delegate grpcsharp_call_cancel;
79 public readonly Delegates.grpcsharp_call_cancel_with_status_delegate grpcsharp_call_cancel_with_status;
80 public readonly Delegates.grpcsharp_call_start_unary_delegate grpcsharp_call_start_unary;
81 public readonly Delegates.grpcsharp_call_start_client_streaming_delegate grpcsharp_call_start_client_streaming;
82 public readonly Delegates.grpcsharp_call_start_server_streaming_delegate grpcsharp_call_start_server_streaming;
83 public readonly Delegates.grpcsharp_call_start_duplex_streaming_delegate grpcsharp_call_start_duplex_streaming;
84 public readonly Delegates.grpcsharp_call_send_message_delegate grpcsharp_call_send_message;
85 public readonly Delegates.grpcsharp_call_send_close_from_client_delegate grpcsharp_call_send_close_from_client;
86 public readonly Delegates.grpcsharp_call_send_status_from_server_delegate grpcsharp_call_send_status_from_server;
87 public readonly Delegates.grpcsharp_call_recv_message_delegate grpcsharp_call_recv_message;
88 public readonly Delegates.grpcsharp_call_recv_initial_metadata_delegate grpcsharp_call_recv_initial_metadata;
89 public readonly Delegates.grpcsharp_call_start_serverside_delegate grpcsharp_call_start_serverside;
90 public readonly Delegates.grpcsharp_call_send_initial_metadata_delegate grpcsharp_call_send_initial_metadata;
91 public readonly Delegates.grpcsharp_call_set_credentials_delegate grpcsharp_call_set_credentials;
92 public readonly Delegates.grpcsharp_call_get_peer_delegate grpcsharp_call_get_peer;
93 public readonly Delegates.grpcsharp_call_destroy_delegate grpcsharp_call_destroy;
94
95 public readonly Delegates.grpcsharp_channel_args_create_delegate grpcsharp_channel_args_create;
96 public readonly Delegates.grpcsharp_channel_args_set_string_delegate grpcsharp_channel_args_set_string;
97 public readonly Delegates.grpcsharp_channel_args_set_integer_delegate grpcsharp_channel_args_set_integer;
98 public readonly Delegates.grpcsharp_channel_args_destroy_delegate grpcsharp_channel_args_destroy;
99
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800100 public readonly Delegates.grpcsharp_override_default_ssl_roots grpcsharp_override_default_ssl_roots;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800101 public readonly Delegates.grpcsharp_ssl_credentials_create_delegate grpcsharp_ssl_credentials_create;
102 public readonly Delegates.grpcsharp_composite_channel_credentials_create_delegate grpcsharp_composite_channel_credentials_create;
103 public readonly Delegates.grpcsharp_channel_credentials_release_delegate grpcsharp_channel_credentials_release;
104
105 public readonly Delegates.grpcsharp_insecure_channel_create_delegate grpcsharp_insecure_channel_create;
106 public readonly Delegates.grpcsharp_secure_channel_create_delegate grpcsharp_secure_channel_create;
107 public readonly Delegates.grpcsharp_channel_create_call_delegate grpcsharp_channel_create_call;
108 public readonly Delegates.grpcsharp_channel_check_connectivity_state_delegate grpcsharp_channel_check_connectivity_state;
109 public readonly Delegates.grpcsharp_channel_watch_connectivity_state_delegate grpcsharp_channel_watch_connectivity_state;
110 public readonly Delegates.grpcsharp_channel_get_target_delegate grpcsharp_channel_get_target;
111 public readonly Delegates.grpcsharp_channel_destroy_delegate grpcsharp_channel_destroy;
112
113 public readonly Delegates.grpcsharp_sizeof_grpc_event_delegate grpcsharp_sizeof_grpc_event;
114
115 public readonly Delegates.grpcsharp_completion_queue_create_delegate grpcsharp_completion_queue_create;
116 public readonly Delegates.grpcsharp_completion_queue_shutdown_delegate grpcsharp_completion_queue_shutdown;
117 public readonly Delegates.grpcsharp_completion_queue_next_delegate grpcsharp_completion_queue_next;
118 public readonly Delegates.grpcsharp_completion_queue_pluck_delegate grpcsharp_completion_queue_pluck;
119 public readonly Delegates.grpcsharp_completion_queue_destroy_delegate grpcsharp_completion_queue_destroy;
120
121 public readonly Delegates.gprsharp_free_delegate gprsharp_free;
122
123 public readonly Delegates.grpcsharp_metadata_array_create_delegate grpcsharp_metadata_array_create;
124 public readonly Delegates.grpcsharp_metadata_array_add_delegate grpcsharp_metadata_array_add;
125 public readonly Delegates.grpcsharp_metadata_array_count_delegate grpcsharp_metadata_array_count;
126 public readonly Delegates.grpcsharp_metadata_array_get_key_delegate grpcsharp_metadata_array_get_key;
127 public readonly Delegates.grpcsharp_metadata_array_get_value_delegate grpcsharp_metadata_array_get_value;
128 public readonly Delegates.grpcsharp_metadata_array_get_value_length_delegate grpcsharp_metadata_array_get_value_length;
129 public readonly Delegates.grpcsharp_metadata_array_destroy_full_delegate grpcsharp_metadata_array_destroy_full;
130
131 public readonly Delegates.grpcsharp_redirect_log_delegate grpcsharp_redirect_log;
132
133 public readonly Delegates.grpcsharp_metadata_credentials_create_from_plugin_delegate grpcsharp_metadata_credentials_create_from_plugin;
134 public readonly Delegates.grpcsharp_metadata_credentials_notify_from_plugin_delegate grpcsharp_metadata_credentials_notify_from_plugin;
135
136 public readonly Delegates.grpcsharp_ssl_server_credentials_create_delegate grpcsharp_ssl_server_credentials_create;
137 public readonly Delegates.grpcsharp_server_credentials_release_delegate grpcsharp_server_credentials_release;
138
139 public readonly Delegates.grpcsharp_server_create_delegate grpcsharp_server_create;
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400140 public readonly Delegates.grpcsharp_server_register_completion_queue_delegate grpcsharp_server_register_completion_queue;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800141 public readonly Delegates.grpcsharp_server_add_insecure_http2_port_delegate grpcsharp_server_add_insecure_http2_port;
142 public readonly Delegates.grpcsharp_server_add_secure_http2_port_delegate grpcsharp_server_add_secure_http2_port;
143 public readonly Delegates.grpcsharp_server_start_delegate grpcsharp_server_start;
144 public readonly Delegates.grpcsharp_server_request_call_delegate grpcsharp_server_request_call;
145 public readonly Delegates.grpcsharp_server_cancel_all_calls_delegate grpcsharp_server_cancel_all_calls;
146 public readonly Delegates.grpcsharp_server_shutdown_and_notify_callback_delegate grpcsharp_server_shutdown_and_notify_callback;
147 public readonly Delegates.grpcsharp_server_destroy_delegate grpcsharp_server_destroy;
148
149 public readonly Delegates.gprsharp_now_delegate gprsharp_now;
150 public readonly Delegates.gprsharp_inf_future_delegate gprsharp_inf_future;
151 public readonly Delegates.gprsharp_inf_past_delegate gprsharp_inf_past;
152 public readonly Delegates.gprsharp_convert_clock_type_delegate gprsharp_convert_clock_type;
153 public readonly Delegates.gprsharp_sizeof_timespec_delegate gprsharp_sizeof_timespec;
154
155 public readonly Delegates.grpcsharp_test_callback_delegate grpcsharp_test_callback;
156 public readonly Delegates.grpcsharp_test_nop_delegate grpcsharp_test_nop;
157
158 #endregion
159
160 public NativeMethods(UnmanagedLibrary library)
161 {
162 if (PlatformApis.IsLinux || PlatformApis.IsMacOSX)
163 {
164 this.grpcsharp_init = GetMethodDelegate<Delegates.grpcsharp_init_delegate>(library);
165 this.grpcsharp_shutdown = GetMethodDelegate<Delegates.grpcsharp_shutdown_delegate>(library);
166 this.grpcsharp_version_string = GetMethodDelegate<Delegates.grpcsharp_version_string_delegate>(library);
167
168 this.grpcsharp_batch_context_create = GetMethodDelegate<Delegates.grpcsharp_batch_context_create_delegate>(library);
169 this.grpcsharp_batch_context_recv_initial_metadata = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_initial_metadata_delegate>(library);
170 this.grpcsharp_batch_context_recv_message_length = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_message_length_delegate>(library);
171 this.grpcsharp_batch_context_recv_message_to_buffer = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_message_to_buffer_delegate>(library);
172 this.grpcsharp_batch_context_recv_status_on_client_status = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_status_on_client_status_delegate>(library);
173 this.grpcsharp_batch_context_recv_status_on_client_details = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_status_on_client_details_delegate>(library);
174 this.grpcsharp_batch_context_recv_status_on_client_trailing_metadata = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate>(library);
175 this.grpcsharp_batch_context_server_rpc_new_call = GetMethodDelegate<Delegates.grpcsharp_batch_context_server_rpc_new_call_delegate>(library);
176 this.grpcsharp_batch_context_server_rpc_new_method = GetMethodDelegate<Delegates.grpcsharp_batch_context_server_rpc_new_method_delegate>(library);
177 this.grpcsharp_batch_context_server_rpc_new_host = GetMethodDelegate<Delegates.grpcsharp_batch_context_server_rpc_new_host_delegate>(library);
178 this.grpcsharp_batch_context_server_rpc_new_deadline = GetMethodDelegate<Delegates.grpcsharp_batch_context_server_rpc_new_deadline_delegate>(library);
179 this.grpcsharp_batch_context_server_rpc_new_request_metadata = GetMethodDelegate<Delegates.grpcsharp_batch_context_server_rpc_new_request_metadata_delegate>(library);
180 this.grpcsharp_batch_context_recv_close_on_server_cancelled = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_close_on_server_cancelled_delegate>(library);
181 this.grpcsharp_batch_context_destroy = GetMethodDelegate<Delegates.grpcsharp_batch_context_destroy_delegate>(library);
182
183 this.grpcsharp_composite_call_credentials_create = GetMethodDelegate<Delegates.grpcsharp_composite_call_credentials_create_delegate>(library);
184 this.grpcsharp_call_credentials_release = GetMethodDelegate<Delegates.grpcsharp_call_credentials_release_delegate>(library);
185
186 this.grpcsharp_call_cancel = GetMethodDelegate<Delegates.grpcsharp_call_cancel_delegate>(library);
187 this.grpcsharp_call_cancel_with_status = GetMethodDelegate<Delegates.grpcsharp_call_cancel_with_status_delegate>(library);
188 this.grpcsharp_call_start_unary = GetMethodDelegate<Delegates.grpcsharp_call_start_unary_delegate>(library);
189 this.grpcsharp_call_start_client_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_client_streaming_delegate>(library);
190 this.grpcsharp_call_start_server_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_server_streaming_delegate>(library);
191 this.grpcsharp_call_start_duplex_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_duplex_streaming_delegate>(library);
192 this.grpcsharp_call_send_message = GetMethodDelegate<Delegates.grpcsharp_call_send_message_delegate>(library);
193 this.grpcsharp_call_send_close_from_client = GetMethodDelegate<Delegates.grpcsharp_call_send_close_from_client_delegate>(library);
194 this.grpcsharp_call_send_status_from_server = GetMethodDelegate<Delegates.grpcsharp_call_send_status_from_server_delegate>(library);
195 this.grpcsharp_call_recv_message = GetMethodDelegate<Delegates.grpcsharp_call_recv_message_delegate>(library);
196 this.grpcsharp_call_recv_initial_metadata = GetMethodDelegate<Delegates.grpcsharp_call_recv_initial_metadata_delegate>(library);
197 this.grpcsharp_call_start_serverside = GetMethodDelegate<Delegates.grpcsharp_call_start_serverside_delegate>(library);
198 this.grpcsharp_call_send_initial_metadata = GetMethodDelegate<Delegates.grpcsharp_call_send_initial_metadata_delegate>(library);
199 this.grpcsharp_call_set_credentials = GetMethodDelegate<Delegates.grpcsharp_call_set_credentials_delegate>(library);
200 this.grpcsharp_call_get_peer = GetMethodDelegate<Delegates.grpcsharp_call_get_peer_delegate>(library);
201 this.grpcsharp_call_destroy = GetMethodDelegate<Delegates.grpcsharp_call_destroy_delegate>(library);
202
203 this.grpcsharp_channel_args_create = GetMethodDelegate<Delegates.grpcsharp_channel_args_create_delegate>(library);
204 this.grpcsharp_channel_args_set_string = GetMethodDelegate<Delegates.grpcsharp_channel_args_set_string_delegate>(library);
205 this.grpcsharp_channel_args_set_integer = GetMethodDelegate<Delegates.grpcsharp_channel_args_set_integer_delegate>(library);
206 this.grpcsharp_channel_args_destroy = GetMethodDelegate<Delegates.grpcsharp_channel_args_destroy_delegate>(library);
207
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800208 this.grpcsharp_override_default_ssl_roots = GetMethodDelegate<Delegates.grpcsharp_override_default_ssl_roots>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800209 this.grpcsharp_ssl_credentials_create = GetMethodDelegate<Delegates.grpcsharp_ssl_credentials_create_delegate>(library);
210 this.grpcsharp_composite_channel_credentials_create = GetMethodDelegate<Delegates.grpcsharp_composite_channel_credentials_create_delegate>(library);
211 this.grpcsharp_channel_credentials_release = GetMethodDelegate<Delegates.grpcsharp_channel_credentials_release_delegate>(library);
212
213 this.grpcsharp_insecure_channel_create = GetMethodDelegate<Delegates.grpcsharp_insecure_channel_create_delegate>(library);
214 this.grpcsharp_secure_channel_create = GetMethodDelegate<Delegates.grpcsharp_secure_channel_create_delegate>(library);
215 this.grpcsharp_channel_create_call = GetMethodDelegate<Delegates.grpcsharp_channel_create_call_delegate>(library);
216 this.grpcsharp_channel_check_connectivity_state = GetMethodDelegate<Delegates.grpcsharp_channel_check_connectivity_state_delegate>(library);
217 this.grpcsharp_channel_watch_connectivity_state = GetMethodDelegate<Delegates.grpcsharp_channel_watch_connectivity_state_delegate>(library);
218 this.grpcsharp_channel_get_target = GetMethodDelegate<Delegates.grpcsharp_channel_get_target_delegate>(library);
219 this.grpcsharp_channel_destroy = GetMethodDelegate<Delegates.grpcsharp_channel_destroy_delegate>(library);
220
221 this.grpcsharp_sizeof_grpc_event = GetMethodDelegate<Delegates.grpcsharp_sizeof_grpc_event_delegate>(library);
222
223 this.grpcsharp_completion_queue_create = GetMethodDelegate<Delegates.grpcsharp_completion_queue_create_delegate>(library);
224 this.grpcsharp_completion_queue_shutdown = GetMethodDelegate<Delegates.grpcsharp_completion_queue_shutdown_delegate>(library);
225 this.grpcsharp_completion_queue_next = GetMethodDelegate<Delegates.grpcsharp_completion_queue_next_delegate>(library);
226 this.grpcsharp_completion_queue_pluck = GetMethodDelegate<Delegates.grpcsharp_completion_queue_pluck_delegate>(library);
227 this.grpcsharp_completion_queue_destroy = GetMethodDelegate<Delegates.grpcsharp_completion_queue_destroy_delegate>(library);
228
229 this.gprsharp_free = GetMethodDelegate<Delegates.gprsharp_free_delegate>(library);
230
231 this.grpcsharp_metadata_array_create = GetMethodDelegate<Delegates.grpcsharp_metadata_array_create_delegate>(library);
232 this.grpcsharp_metadata_array_add = GetMethodDelegate<Delegates.grpcsharp_metadata_array_add_delegate>(library);
233 this.grpcsharp_metadata_array_count = GetMethodDelegate<Delegates.grpcsharp_metadata_array_count_delegate>(library);
234 this.grpcsharp_metadata_array_get_key = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_key_delegate>(library);
235 this.grpcsharp_metadata_array_get_value = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_value_delegate>(library);
236 this.grpcsharp_metadata_array_get_value_length = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_value_length_delegate>(library);
237 this.grpcsharp_metadata_array_destroy_full = GetMethodDelegate<Delegates.grpcsharp_metadata_array_destroy_full_delegate>(library);
238
239 this.grpcsharp_redirect_log = GetMethodDelegate<Delegates.grpcsharp_redirect_log_delegate>(library);
240
241 this.grpcsharp_metadata_credentials_create_from_plugin = GetMethodDelegate<Delegates.grpcsharp_metadata_credentials_create_from_plugin_delegate>(library);
242 this.grpcsharp_metadata_credentials_notify_from_plugin = GetMethodDelegate<Delegates.grpcsharp_metadata_credentials_notify_from_plugin_delegate>(library);
243
244 this.grpcsharp_ssl_server_credentials_create = GetMethodDelegate<Delegates.grpcsharp_ssl_server_credentials_create_delegate>(library);
245 this.grpcsharp_server_credentials_release = GetMethodDelegate<Delegates.grpcsharp_server_credentials_release_delegate>(library);
246
247 this.grpcsharp_server_create = GetMethodDelegate<Delegates.grpcsharp_server_create_delegate>(library);
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400248 this.grpcsharp_server_register_completion_queue = GetMethodDelegate<Delegates.grpcsharp_server_register_completion_queue_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800249 this.grpcsharp_server_add_insecure_http2_port = GetMethodDelegate<Delegates.grpcsharp_server_add_insecure_http2_port_delegate>(library);
250 this.grpcsharp_server_add_secure_http2_port = GetMethodDelegate<Delegates.grpcsharp_server_add_secure_http2_port_delegate>(library);
251 this.grpcsharp_server_start = GetMethodDelegate<Delegates.grpcsharp_server_start_delegate>(library);
252 this.grpcsharp_server_request_call = GetMethodDelegate<Delegates.grpcsharp_server_request_call_delegate>(library);
253 this.grpcsharp_server_cancel_all_calls = GetMethodDelegate<Delegates.grpcsharp_server_cancel_all_calls_delegate>(library);
254 this.grpcsharp_server_shutdown_and_notify_callback = GetMethodDelegate<Delegates.grpcsharp_server_shutdown_and_notify_callback_delegate>(library);
255 this.grpcsharp_server_destroy = GetMethodDelegate<Delegates.grpcsharp_server_destroy_delegate>(library);
256
257 this.gprsharp_now = GetMethodDelegate<Delegates.gprsharp_now_delegate>(library);
258 this.gprsharp_inf_future = GetMethodDelegate<Delegates.gprsharp_inf_future_delegate>(library);
259 this.gprsharp_inf_past = GetMethodDelegate<Delegates.gprsharp_inf_past_delegate>(library);
260 this.gprsharp_convert_clock_type = GetMethodDelegate<Delegates.gprsharp_convert_clock_type_delegate>(library);
261 this.gprsharp_sizeof_timespec = GetMethodDelegate<Delegates.gprsharp_sizeof_timespec_delegate>(library);
262
263 this.grpcsharp_test_callback = GetMethodDelegate<Delegates.grpcsharp_test_callback_delegate>(library);
264 this.grpcsharp_test_nop = GetMethodDelegate<Delegates.grpcsharp_test_nop_delegate>(library);
265 }
266 else
267 {
268 // Windows or fallback
269 this.grpcsharp_init = PInvokeMethods.grpcsharp_init;
270 this.grpcsharp_shutdown = PInvokeMethods.grpcsharp_shutdown;
271 this.grpcsharp_version_string = PInvokeMethods.grpcsharp_version_string;
272
273 this.grpcsharp_batch_context_create = PInvokeMethods.grpcsharp_batch_context_create;
274 this.grpcsharp_batch_context_recv_initial_metadata = PInvokeMethods.grpcsharp_batch_context_recv_initial_metadata;
275 this.grpcsharp_batch_context_recv_message_length = PInvokeMethods.grpcsharp_batch_context_recv_message_length;
276 this.grpcsharp_batch_context_recv_message_to_buffer = PInvokeMethods.grpcsharp_batch_context_recv_message_to_buffer;
277 this.grpcsharp_batch_context_recv_status_on_client_status = PInvokeMethods.grpcsharp_batch_context_recv_status_on_client_status;
278 this.grpcsharp_batch_context_recv_status_on_client_details = PInvokeMethods.grpcsharp_batch_context_recv_status_on_client_details;
279 this.grpcsharp_batch_context_recv_status_on_client_trailing_metadata = PInvokeMethods.grpcsharp_batch_context_recv_status_on_client_trailing_metadata;
280 this.grpcsharp_batch_context_server_rpc_new_call = PInvokeMethods.grpcsharp_batch_context_server_rpc_new_call;
281 this.grpcsharp_batch_context_server_rpc_new_method = PInvokeMethods.grpcsharp_batch_context_server_rpc_new_method;
282 this.grpcsharp_batch_context_server_rpc_new_host = PInvokeMethods.grpcsharp_batch_context_server_rpc_new_host;
283 this.grpcsharp_batch_context_server_rpc_new_deadline = PInvokeMethods.grpcsharp_batch_context_server_rpc_new_deadline;
284 this.grpcsharp_batch_context_server_rpc_new_request_metadata = PInvokeMethods.grpcsharp_batch_context_server_rpc_new_request_metadata;
285 this.grpcsharp_batch_context_recv_close_on_server_cancelled = PInvokeMethods.grpcsharp_batch_context_recv_close_on_server_cancelled;
286 this.grpcsharp_batch_context_destroy = PInvokeMethods.grpcsharp_batch_context_destroy;
287
288 this.grpcsharp_composite_call_credentials_create = PInvokeMethods.grpcsharp_composite_call_credentials_create;
289 this.grpcsharp_call_credentials_release = PInvokeMethods.grpcsharp_call_credentials_release;
290
291 this.grpcsharp_call_cancel = PInvokeMethods.grpcsharp_call_cancel;
292 this.grpcsharp_call_cancel_with_status = PInvokeMethods.grpcsharp_call_cancel_with_status;
293 this.grpcsharp_call_start_unary = PInvokeMethods.grpcsharp_call_start_unary;
294 this.grpcsharp_call_start_client_streaming = PInvokeMethods.grpcsharp_call_start_client_streaming;
295 this.grpcsharp_call_start_server_streaming = PInvokeMethods.grpcsharp_call_start_server_streaming;
296 this.grpcsharp_call_start_duplex_streaming = PInvokeMethods.grpcsharp_call_start_duplex_streaming;
297 this.grpcsharp_call_send_message = PInvokeMethods.grpcsharp_call_send_message;
298 this.grpcsharp_call_send_close_from_client = PInvokeMethods.grpcsharp_call_send_close_from_client;
299 this.grpcsharp_call_send_status_from_server = PInvokeMethods.grpcsharp_call_send_status_from_server;
300 this.grpcsharp_call_recv_message = PInvokeMethods.grpcsharp_call_recv_message;
301 this.grpcsharp_call_recv_initial_metadata = PInvokeMethods.grpcsharp_call_recv_initial_metadata;
302 this.grpcsharp_call_start_serverside = PInvokeMethods.grpcsharp_call_start_serverside;
303 this.grpcsharp_call_send_initial_metadata = PInvokeMethods.grpcsharp_call_send_initial_metadata;
304 this.grpcsharp_call_set_credentials = PInvokeMethods.grpcsharp_call_set_credentials;
305 this.grpcsharp_call_get_peer = PInvokeMethods.grpcsharp_call_get_peer;
306 this.grpcsharp_call_destroy = PInvokeMethods.grpcsharp_call_destroy;
307
308 this.grpcsharp_channel_args_create = PInvokeMethods.grpcsharp_channel_args_create;
309 this.grpcsharp_channel_args_set_string = PInvokeMethods.grpcsharp_channel_args_set_string;
310 this.grpcsharp_channel_args_set_integer = PInvokeMethods.grpcsharp_channel_args_set_integer;
311 this.grpcsharp_channel_args_destroy = PInvokeMethods.grpcsharp_channel_args_destroy;
312
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800313 this.grpcsharp_override_default_ssl_roots = PInvokeMethods.grpcsharp_override_default_ssl_roots;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800314 this.grpcsharp_ssl_credentials_create = PInvokeMethods.grpcsharp_ssl_credentials_create;
315 this.grpcsharp_composite_channel_credentials_create = PInvokeMethods.grpcsharp_composite_channel_credentials_create;
316 this.grpcsharp_channel_credentials_release = PInvokeMethods.grpcsharp_channel_credentials_release;
317
318 this.grpcsharp_insecure_channel_create = PInvokeMethods.grpcsharp_insecure_channel_create;
319 this.grpcsharp_secure_channel_create = PInvokeMethods.grpcsharp_secure_channel_create;
320 this.grpcsharp_channel_create_call = PInvokeMethods.grpcsharp_channel_create_call;
321 this.grpcsharp_channel_check_connectivity_state = PInvokeMethods.grpcsharp_channel_check_connectivity_state;
322 this.grpcsharp_channel_watch_connectivity_state = PInvokeMethods.grpcsharp_channel_watch_connectivity_state;
323 this.grpcsharp_channel_get_target = PInvokeMethods.grpcsharp_channel_get_target;
324 this.grpcsharp_channel_destroy = PInvokeMethods.grpcsharp_channel_destroy;
325
326 this.grpcsharp_sizeof_grpc_event = PInvokeMethods.grpcsharp_sizeof_grpc_event;
327
328 this.grpcsharp_completion_queue_create = PInvokeMethods.grpcsharp_completion_queue_create;
329 this.grpcsharp_completion_queue_shutdown = PInvokeMethods.grpcsharp_completion_queue_shutdown;
330 this.grpcsharp_completion_queue_next = PInvokeMethods.grpcsharp_completion_queue_next;
331 this.grpcsharp_completion_queue_pluck = PInvokeMethods.grpcsharp_completion_queue_pluck;
332 this.grpcsharp_completion_queue_destroy = PInvokeMethods.grpcsharp_completion_queue_destroy;
333
334 this.gprsharp_free = PInvokeMethods.gprsharp_free;
335
336 this.grpcsharp_metadata_array_create = PInvokeMethods.grpcsharp_metadata_array_create;
337 this.grpcsharp_metadata_array_add = PInvokeMethods.grpcsharp_metadata_array_add;
338 this.grpcsharp_metadata_array_count = PInvokeMethods.grpcsharp_metadata_array_count;
339 this.grpcsharp_metadata_array_get_key = PInvokeMethods.grpcsharp_metadata_array_get_key;
340 this.grpcsharp_metadata_array_get_value = PInvokeMethods.grpcsharp_metadata_array_get_value;
341 this.grpcsharp_metadata_array_get_value_length = PInvokeMethods.grpcsharp_metadata_array_get_value_length;
342 this.grpcsharp_metadata_array_destroy_full = PInvokeMethods.grpcsharp_metadata_array_destroy_full;
343
344 this.grpcsharp_redirect_log = PInvokeMethods.grpcsharp_redirect_log;
345
346 this.grpcsharp_metadata_credentials_create_from_plugin = PInvokeMethods.grpcsharp_metadata_credentials_create_from_plugin;
347 this.grpcsharp_metadata_credentials_notify_from_plugin = PInvokeMethods.grpcsharp_metadata_credentials_notify_from_plugin;
348
349 this.grpcsharp_ssl_server_credentials_create = PInvokeMethods.grpcsharp_ssl_server_credentials_create;
350 this.grpcsharp_server_credentials_release = PInvokeMethods.grpcsharp_server_credentials_release;
351
352 this.grpcsharp_server_create = PInvokeMethods.grpcsharp_server_create;
Jan Tattermusch138156a2016-05-26 04:43:50 -0700353 this.grpcsharp_server_register_completion_queue = PInvokeMethods.grpcsharp_server_register_completion_queue;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800354 this.grpcsharp_server_add_insecure_http2_port = PInvokeMethods.grpcsharp_server_add_insecure_http2_port;
355 this.grpcsharp_server_add_secure_http2_port = PInvokeMethods.grpcsharp_server_add_secure_http2_port;
356 this.grpcsharp_server_start = PInvokeMethods.grpcsharp_server_start;
357 this.grpcsharp_server_request_call = PInvokeMethods.grpcsharp_server_request_call;
358 this.grpcsharp_server_cancel_all_calls = PInvokeMethods.grpcsharp_server_cancel_all_calls;
359 this.grpcsharp_server_shutdown_and_notify_callback = PInvokeMethods.grpcsharp_server_shutdown_and_notify_callback;
360 this.grpcsharp_server_destroy = PInvokeMethods.grpcsharp_server_destroy;
361
362 this.gprsharp_now = PInvokeMethods.gprsharp_now;
363 this.gprsharp_inf_future = PInvokeMethods.gprsharp_inf_future;
364 this.gprsharp_inf_past = PInvokeMethods.gprsharp_inf_past;
365 this.gprsharp_convert_clock_type = PInvokeMethods.gprsharp_convert_clock_type;
366 this.gprsharp_sizeof_timespec = PInvokeMethods.gprsharp_sizeof_timespec;
367
368 this.grpcsharp_test_callback = PInvokeMethods.grpcsharp_test_callback;
369 this.grpcsharp_test_nop = PInvokeMethods.grpcsharp_test_nop;
370 }
371 }
372
373 /// <summary>
374 /// Gets singleton instance of this class.
375 /// </summary>
376 public static NativeMethods Get()
377 {
378 return NativeExtension.Get().NativeMethods;
379 }
380
381 static T GetMethodDelegate<T>(UnmanagedLibrary library)
382 where T : class
383 {
384 var methodName = RemoveStringSuffix(typeof(T).Name, "_delegate");
385 return library.GetNativeMethodDelegate<T>(methodName);
386 }
387
388 static string RemoveStringSuffix(string str, string toRemove)
389 {
390 if (!str.EndsWith(toRemove))
391 {
392 return str;
393 }
394 return str.Substring(0, str.Length - toRemove.Length);
395 }
396
397 /// <summary>
398 /// Delegate types for all published native methods. Declared under inner class to prevent scope pollution.
399 /// </summary>
400 public class Delegates
401 {
402 public delegate void grpcsharp_init_delegate();
403 public delegate void grpcsharp_shutdown_delegate();
404 public delegate IntPtr grpcsharp_version_string_delegate(); // returns not-owned const char*
405
406 public delegate BatchContextSafeHandle grpcsharp_batch_context_create_delegate();
407 public delegate IntPtr grpcsharp_batch_context_recv_initial_metadata_delegate(BatchContextSafeHandle ctx);
408 public delegate IntPtr grpcsharp_batch_context_recv_message_length_delegate(BatchContextSafeHandle ctx);
409 public delegate void grpcsharp_batch_context_recv_message_to_buffer_delegate(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen);
410 public delegate StatusCode grpcsharp_batch_context_recv_status_on_client_status_delegate(BatchContextSafeHandle ctx);
411 public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx); // returns const char*
412 public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate(BatchContextSafeHandle ctx);
413 public delegate CallSafeHandle grpcsharp_batch_context_server_rpc_new_call_delegate(BatchContextSafeHandle ctx);
414 public delegate IntPtr grpcsharp_batch_context_server_rpc_new_method_delegate(BatchContextSafeHandle ctx); // returns const char*
415 public delegate IntPtr grpcsharp_batch_context_server_rpc_new_host_delegate(BatchContextSafeHandle ctx); // returns const char*
416 public delegate Timespec grpcsharp_batch_context_server_rpc_new_deadline_delegate(BatchContextSafeHandle ctx);
417 public delegate IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata_delegate(BatchContextSafeHandle ctx);
418 public delegate int grpcsharp_batch_context_recv_close_on_server_cancelled_delegate(BatchContextSafeHandle ctx);
419 public delegate void grpcsharp_batch_context_destroy_delegate(IntPtr ctx);
420
421 public delegate CallCredentialsSafeHandle grpcsharp_composite_call_credentials_create_delegate(CallCredentialsSafeHandle creds1, CallCredentialsSafeHandle creds2);
422 public delegate void grpcsharp_call_credentials_release_delegate(IntPtr credentials);
423
Jan Tattermusche0af2862016-05-18 21:02:03 -0700424 public delegate CallError grpcsharp_call_cancel_delegate(CallSafeHandle call);
425 public delegate CallError grpcsharp_call_cancel_with_status_delegate(CallSafeHandle call, StatusCode status, string description);
426 public delegate CallError grpcsharp_call_start_unary_delegate(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700427 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700428 public delegate CallError grpcsharp_call_start_client_streaming_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800429 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700430 public delegate CallError grpcsharp_call_start_server_streaming_delegate(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700431 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800432 MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700433 public delegate CallError grpcsharp_call_start_duplex_streaming_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800434 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700435 public delegate CallError grpcsharp_call_send_message_delegate(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700436 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, bool sendEmptyInitialMetadata);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700437 public delegate CallError grpcsharp_call_send_close_from_client_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800438 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700439 public delegate CallError grpcsharp_call_send_status_from_server_delegate(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700440 BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata,
441 byte[] optionalSendBuffer, UIntPtr optionalSendBufferLen, WriteFlags writeFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700442 public delegate CallError grpcsharp_call_recv_message_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800443 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700444 public delegate CallError grpcsharp_call_recv_initial_metadata_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800445 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700446 public delegate CallError grpcsharp_call_start_serverside_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800447 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700448 public delegate CallError grpcsharp_call_send_initial_metadata_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800449 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700450 public delegate CallError grpcsharp_call_set_credentials_delegate(CallSafeHandle call, CallCredentialsSafeHandle credentials);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800451 public delegate CStringSafeHandle grpcsharp_call_get_peer_delegate(CallSafeHandle call);
452 public delegate void grpcsharp_call_destroy_delegate(IntPtr call);
453
454 public delegate ChannelArgsSafeHandle grpcsharp_channel_args_create_delegate(UIntPtr numArgs);
455 public delegate void grpcsharp_channel_args_set_string_delegate(ChannelArgsSafeHandle args, UIntPtr index, string key, string value);
456 public delegate void grpcsharp_channel_args_set_integer_delegate(ChannelArgsSafeHandle args, UIntPtr index, string key, int value);
457 public delegate void grpcsharp_channel_args_destroy_delegate(IntPtr args);
458
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800459 public delegate void grpcsharp_override_default_ssl_roots(string pemRootCerts);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800460 public delegate ChannelCredentialsSafeHandle grpcsharp_ssl_credentials_create_delegate(string pemRootCerts, string keyCertPairCertChain, string keyCertPairPrivateKey);
461 public delegate ChannelCredentialsSafeHandle grpcsharp_composite_channel_credentials_create_delegate(ChannelCredentialsSafeHandle channelCreds, CallCredentialsSafeHandle callCreds);
462 public delegate void grpcsharp_channel_credentials_release_delegate(IntPtr credentials);
463
464 public delegate ChannelSafeHandle grpcsharp_insecure_channel_create_delegate(string target, ChannelArgsSafeHandle channelArgs);
465 public delegate ChannelSafeHandle grpcsharp_secure_channel_create_delegate(ChannelCredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs);
466 public delegate CallSafeHandle grpcsharp_channel_create_call_delegate(ChannelSafeHandle channel, CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline);
467 public delegate ChannelState grpcsharp_channel_check_connectivity_state_delegate(ChannelSafeHandle channel, int tryToConnect);
468 public delegate void grpcsharp_channel_watch_connectivity_state_delegate(ChannelSafeHandle channel, ChannelState lastObservedState,
469 Timespec deadline, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
470 public delegate CStringSafeHandle grpcsharp_channel_get_target_delegate(ChannelSafeHandle call);
471 public delegate void grpcsharp_channel_destroy_delegate(IntPtr channel);
472
473 public delegate int grpcsharp_sizeof_grpc_event_delegate();
474
475 public delegate CompletionQueueSafeHandle grpcsharp_completion_queue_create_delegate();
476 public delegate void grpcsharp_completion_queue_shutdown_delegate(CompletionQueueSafeHandle cq);
477 public delegate CompletionQueueEvent grpcsharp_completion_queue_next_delegate(CompletionQueueSafeHandle cq);
478 public delegate CompletionQueueEvent grpcsharp_completion_queue_pluck_delegate(CompletionQueueSafeHandle cq, IntPtr tag);
479 public delegate void grpcsharp_completion_queue_destroy_delegate(IntPtr cq);
480
481 public delegate void gprsharp_free_delegate(IntPtr ptr);
482
483 public delegate MetadataArraySafeHandle grpcsharp_metadata_array_create_delegate(UIntPtr capacity);
484 public delegate void grpcsharp_metadata_array_add_delegate(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength);
485 public delegate UIntPtr grpcsharp_metadata_array_count_delegate(IntPtr metadataArray);
486 public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index);
487 public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index);
488 public delegate UIntPtr grpcsharp_metadata_array_get_value_length_delegate(IntPtr metadataArray, UIntPtr index);
489 public delegate void grpcsharp_metadata_array_destroy_full_delegate(IntPtr array);
490
491 public delegate void grpcsharp_redirect_log_delegate(GprLogDelegate callback);
492
493 public delegate CallCredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin_delegate(NativeMetadataInterceptor interceptor);
494 public delegate void grpcsharp_metadata_credentials_notify_from_plugin_delegate(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails);
495
496 public delegate ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create_delegate(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs, bool forceClientAuth);
497 public delegate void grpcsharp_server_credentials_release_delegate(IntPtr credentials);
498
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400499 public delegate ServerSafeHandle grpcsharp_server_create_delegate(ChannelArgsSafeHandle args);
500 public delegate void grpcsharp_server_register_completion_queue_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800501 public delegate int grpcsharp_server_add_insecure_http2_port_delegate(ServerSafeHandle server, string addr);
502 public delegate int grpcsharp_server_add_secure_http2_port_delegate(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds);
503 public delegate void grpcsharp_server_start_delegate(ServerSafeHandle server);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700504 public delegate CallError grpcsharp_server_request_call_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800505 public delegate void grpcsharp_server_cancel_all_calls_delegate(ServerSafeHandle server);
506 public delegate void grpcsharp_server_shutdown_and_notify_callback_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
507 public delegate void grpcsharp_server_destroy_delegate(IntPtr server);
508
Jan Tattermusche0af2862016-05-18 21:02:03 -0700509 public delegate Timespec gprsharp_now_delegate(ClockType clockType);
510 public delegate Timespec gprsharp_inf_future_delegate(ClockType clockType);
511 public delegate Timespec gprsharp_inf_past_delegate(ClockType clockType);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800512
Jan Tattermusche0af2862016-05-18 21:02:03 -0700513 public delegate Timespec gprsharp_convert_clock_type_delegate(Timespec t, ClockType targetClock);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800514 public delegate int gprsharp_sizeof_timespec_delegate();
515
Jan Tattermusche0af2862016-05-18 21:02:03 -0700516 public delegate CallError grpcsharp_test_callback_delegate([MarshalAs(UnmanagedType.FunctionPtr)] OpCompletionDelegate callback);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800517 public delegate IntPtr grpcsharp_test_nop_delegate(IntPtr ptr);
518 }
519
520 /// <summary>
521 /// Default PInvoke bindings for native methods that are used on Windows.
522 /// Alternatively, they can also be used as a fallback on Mono
523 /// (if libgrpc_csharp_ext is installed on your system, or is made accessible through e.g. LD_LIBRARY_PATH environment variable
524 /// or using Mono's dllMap feature).
525 /// </summary>
526 private class PInvokeMethods
527 {
528 // Environment
529
530 [DllImport("grpc_csharp_ext.dll")]
531 public static extern void grpcsharp_init();
532
533 [DllImport("grpc_csharp_ext.dll")]
534 public static extern void grpcsharp_shutdown();
535
536 [DllImport("grpc_csharp_ext.dll")]
537 public static extern IntPtr grpcsharp_version_string(); // returns not-owned const char*
538
539 // BatchContextSafeHandle
540
541 [DllImport("grpc_csharp_ext.dll")]
542 public static extern BatchContextSafeHandle grpcsharp_batch_context_create();
543
544 [DllImport("grpc_csharp_ext.dll")]
545 public static extern IntPtr grpcsharp_batch_context_recv_initial_metadata(BatchContextSafeHandle ctx);
546
547 [DllImport("grpc_csharp_ext.dll")]
548 public static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx);
549
550 [DllImport("grpc_csharp_ext.dll")]
551 public static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen);
552
553 [DllImport("grpc_csharp_ext.dll")]
554 public static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandle ctx);
555
556 [DllImport("grpc_csharp_ext.dll")]
557 public static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx); // returns const char*
558
559 [DllImport("grpc_csharp_ext.dll")]
560 public static extern IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata(BatchContextSafeHandle ctx);
561
562 [DllImport("grpc_csharp_ext.dll")]
563 public static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandle ctx);
564
565 [DllImport("grpc_csharp_ext.dll")]
566 public static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char*
567
568 [DllImport("grpc_csharp_ext.dll")]
569 public static extern IntPtr grpcsharp_batch_context_server_rpc_new_host(BatchContextSafeHandle ctx); // returns const char*
570
571 [DllImport("grpc_csharp_ext.dll")]
572 public static extern Timespec grpcsharp_batch_context_server_rpc_new_deadline(BatchContextSafeHandle ctx);
573
574 [DllImport("grpc_csharp_ext.dll")]
575 public static extern IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata(BatchContextSafeHandle ctx);
576
577 [DllImport("grpc_csharp_ext.dll")]
578 public static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandle ctx);
579
580 [DllImport("grpc_csharp_ext.dll")]
581 public static extern void grpcsharp_batch_context_destroy(IntPtr ctx);
582
583 // CallCredentialsSafeHandle
584
585 [DllImport("grpc_csharp_ext.dll")]
586 public static extern CallCredentialsSafeHandle grpcsharp_composite_call_credentials_create(CallCredentialsSafeHandle creds1, CallCredentialsSafeHandle creds2);
587
588 [DllImport("grpc_csharp_ext.dll")]
589 public static extern void grpcsharp_call_credentials_release(IntPtr credentials);
590
591 // CallSafeHandle
592
593 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700594 public static extern CallError grpcsharp_call_cancel(CallSafeHandle call);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800595
596 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700597 public static extern CallError grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, string description);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800598
599 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700600 public static extern CallError grpcsharp_call_start_unary(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700601 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800602
603 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700604 public static extern CallError grpcsharp_call_start_client_streaming(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800605 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
606
607 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700608 public static extern CallError grpcsharp_call_start_server_streaming(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700609 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800610 MetadataArraySafeHandle metadataArray, WriteFlags writeFlags);
611
612 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700613 public static extern CallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800614 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
615
616 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700617 public static extern CallError grpcsharp_call_send_message(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700618 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, bool sendEmptyInitialMetadata);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800619
620 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700621 public static extern CallError grpcsharp_call_send_close_from_client(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800622 BatchContextSafeHandle ctx);
623
624 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700625 public static extern CallError grpcsharp_call_send_status_from_server(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700626 BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata,
627 byte[] optionalSendBuffer, UIntPtr optionalSendBufferLen, WriteFlags writeFlags);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800628
629 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700630 public static extern CallError grpcsharp_call_recv_message(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800631 BatchContextSafeHandle ctx);
632
633 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700634 public static extern CallError grpcsharp_call_recv_initial_metadata(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800635 BatchContextSafeHandle ctx);
636
637 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700638 public static extern CallError grpcsharp_call_start_serverside(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800639 BatchContextSafeHandle ctx);
640
641 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700642 public static extern CallError grpcsharp_call_send_initial_metadata(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800643 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
644
645 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700646 public static extern CallError grpcsharp_call_set_credentials(CallSafeHandle call, CallCredentialsSafeHandle credentials);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800647
648 [DllImport("grpc_csharp_ext.dll")]
649 public static extern CStringSafeHandle grpcsharp_call_get_peer(CallSafeHandle call);
650
651 [DllImport("grpc_csharp_ext.dll")]
652 public static extern void grpcsharp_call_destroy(IntPtr call);
653
654 // ChannelArgsSafeHandle
655
656 [DllImport("grpc_csharp_ext.dll")]
657 public static extern ChannelArgsSafeHandle grpcsharp_channel_args_create(UIntPtr numArgs);
658
659 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
660 public static extern void grpcsharp_channel_args_set_string(ChannelArgsSafeHandle args, UIntPtr index, string key, string value);
661
662 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
663 public static extern void grpcsharp_channel_args_set_integer(ChannelArgsSafeHandle args, UIntPtr index, string key, int value);
664
665 [DllImport("grpc_csharp_ext.dll")]
666 public static extern void grpcsharp_channel_args_destroy(IntPtr args);
667
668 // ChannelCredentialsSafeHandle
669
670 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800671 public static extern void grpcsharp_override_default_ssl_roots(string pemRootCerts);
672
673 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
Jan Tattermuschafab5412016-01-12 17:57:25 -0800674 public static extern ChannelCredentialsSafeHandle grpcsharp_ssl_credentials_create(string pemRootCerts, string keyCertPairCertChain, string keyCertPairPrivateKey);
675
676 [DllImport("grpc_csharp_ext.dll")]
677 public static extern ChannelCredentialsSafeHandle grpcsharp_composite_channel_credentials_create(ChannelCredentialsSafeHandle channelCreds, CallCredentialsSafeHandle callCreds);
678
679 [DllImport("grpc_csharp_ext.dll")]
680 public static extern void grpcsharp_channel_credentials_release(IntPtr credentials);
681
682 // ChannelSafeHandle
683
684 [DllImport("grpc_csharp_ext.dll")]
685 public static extern ChannelSafeHandle grpcsharp_insecure_channel_create(string target, ChannelArgsSafeHandle channelArgs);
686
687 [DllImport("grpc_csharp_ext.dll")]
688 public static extern ChannelSafeHandle grpcsharp_secure_channel_create(ChannelCredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs);
689
690 [DllImport("grpc_csharp_ext.dll")]
691 public static extern CallSafeHandle grpcsharp_channel_create_call(ChannelSafeHandle channel, CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline);
692
693 [DllImport("grpc_csharp_ext.dll")]
694 public static extern ChannelState grpcsharp_channel_check_connectivity_state(ChannelSafeHandle channel, int tryToConnect);
695
696 [DllImport("grpc_csharp_ext.dll")]
697 public static extern void grpcsharp_channel_watch_connectivity_state(ChannelSafeHandle channel, ChannelState lastObservedState,
698 Timespec deadline, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
699
700 [DllImport("grpc_csharp_ext.dll")]
701 public static extern CStringSafeHandle grpcsharp_channel_get_target(ChannelSafeHandle call);
702
703 [DllImport("grpc_csharp_ext.dll")]
704 public static extern void grpcsharp_channel_destroy(IntPtr channel);
705
706 // CompletionQueueEvent
707
708 [DllImport("grpc_csharp_ext.dll")]
709 public static extern int grpcsharp_sizeof_grpc_event();
710
711 // CompletionQueueSafeHandle
712
713 [DllImport("grpc_csharp_ext.dll")]
714 public static extern CompletionQueueSafeHandle grpcsharp_completion_queue_create();
715
716 [DllImport("grpc_csharp_ext.dll")]
717 public static extern void grpcsharp_completion_queue_shutdown(CompletionQueueSafeHandle cq);
718
719 [DllImport("grpc_csharp_ext.dll")]
720 public static extern CompletionQueueEvent grpcsharp_completion_queue_next(CompletionQueueSafeHandle cq);
721
722 [DllImport("grpc_csharp_ext.dll")]
723 public static extern CompletionQueueEvent grpcsharp_completion_queue_pluck(CompletionQueueSafeHandle cq, IntPtr tag);
724
725 [DllImport("grpc_csharp_ext.dll")]
726 public static extern void grpcsharp_completion_queue_destroy(IntPtr cq);
727
728 // CStringSafeHandle
729
730 [DllImport("grpc_csharp_ext.dll")]
731 public static extern void gprsharp_free(IntPtr ptr);
732
733 // MetadataArraySafeHandle
734
735 [DllImport("grpc_csharp_ext.dll")]
736 public static extern MetadataArraySafeHandle grpcsharp_metadata_array_create(UIntPtr capacity);
737
738 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
739 public static extern void grpcsharp_metadata_array_add(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength);
740
741 [DllImport("grpc_csharp_ext.dll")]
742 public static extern UIntPtr grpcsharp_metadata_array_count(IntPtr metadataArray);
743
744 [DllImport("grpc_csharp_ext.dll")]
745 public static extern IntPtr grpcsharp_metadata_array_get_key(IntPtr metadataArray, UIntPtr index);
746
747 [DllImport("grpc_csharp_ext.dll")]
748 public static extern IntPtr grpcsharp_metadata_array_get_value(IntPtr metadataArray, UIntPtr index);
749
750 [DllImport("grpc_csharp_ext.dll")]
751 public static extern UIntPtr grpcsharp_metadata_array_get_value_length(IntPtr metadataArray, UIntPtr index);
752
753 [DllImport("grpc_csharp_ext.dll")]
754 public static extern void grpcsharp_metadata_array_destroy_full(IntPtr array);
755
756 // NativeLogRedirector
757
758 [DllImport("grpc_csharp_ext.dll")]
759 public static extern void grpcsharp_redirect_log(GprLogDelegate callback);
760
761 // NativeMetadataCredentialsPlugin
762
763 [DllImport("grpc_csharp_ext.dll")]
764 public static extern CallCredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin(NativeMetadataInterceptor interceptor);
765
766 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
767 public static extern void grpcsharp_metadata_credentials_notify_from_plugin(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails);
768
769 // ServerCredentialsSafeHandle
770
771 [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
772 public static extern ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs, bool forceClientAuth);
773
774 [DllImport("grpc_csharp_ext.dll")]
775 public static extern void grpcsharp_server_credentials_release(IntPtr credentials);
776
777 // ServerSafeHandle
778
779 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400780 public static extern ServerSafeHandle grpcsharp_server_create(ChannelArgsSafeHandle args);
781
782 [DllImport("grpc_csharp_ext.dll")]
783 public static extern void grpcsharp_server_register_completion_queue(ServerSafeHandle server, CompletionQueueSafeHandle cq);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800784
785 [DllImport("grpc_csharp_ext.dll")]
786 public static extern int grpcsharp_server_add_insecure_http2_port(ServerSafeHandle server, string addr);
787
788 [DllImport("grpc_csharp_ext.dll")]
789 public static extern int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds);
790
791 [DllImport("grpc_csharp_ext.dll")]
792 public static extern void grpcsharp_server_start(ServerSafeHandle server);
793
794 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700795 public static extern CallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800796
797 [DllImport("grpc_csharp_ext.dll")]
798 public static extern void grpcsharp_server_cancel_all_calls(ServerSafeHandle server);
799
800 [DllImport("grpc_csharp_ext.dll")]
801 public static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
802
803 [DllImport("grpc_csharp_ext.dll")]
804 public static extern void grpcsharp_server_destroy(IntPtr server);
805
806 // Timespec
807
808 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700809 public static extern Timespec gprsharp_now(ClockType clockType);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800810
811 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700812 public static extern Timespec gprsharp_inf_future(ClockType clockType);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800813
814 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700815 public static extern Timespec gprsharp_inf_past(ClockType clockType);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800816
817 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700818 public static extern Timespec gprsharp_convert_clock_type(Timespec t, ClockType targetClock);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800819
820 [DllImport("grpc_csharp_ext.dll")]
821 public static extern int gprsharp_sizeof_timespec();
822
823 // Testing
824
825 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusche0af2862016-05-18 21:02:03 -0700826 public static extern CallError grpcsharp_test_callback([MarshalAs(UnmanagedType.FunctionPtr)] OpCompletionDelegate callback);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800827
828 [DllImport("grpc_csharp_ext.dll")]
829 public static extern IntPtr grpcsharp_test_nop(IntPtr ptr);
830 }
831 }
832}