blob: aff9550e8d24b6612be00815f025e4a3d18e489c [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;
Jan Tattermuschafab5412016-01-12 17:57:25 -080067 public readonly Delegates.grpcsharp_batch_context_recv_close_on_server_cancelled_delegate grpcsharp_batch_context_recv_close_on_server_cancelled;
68 public readonly Delegates.grpcsharp_batch_context_destroy_delegate grpcsharp_batch_context_destroy;
69
Jan Tattermusch6e901712016-10-21 00:42:37 +020070 public readonly Delegates.grpcsharp_request_call_context_create_delegate grpcsharp_request_call_context_create;
71 public readonly Delegates.grpcsharp_request_call_context_call_delegate grpcsharp_request_call_context_call;
72 public readonly Delegates.grpcsharp_request_call_context_method_delegate grpcsharp_request_call_context_method;
73 public readonly Delegates.grpcsharp_request_call_context_host_delegate grpcsharp_request_call_context_host;
74 public readonly Delegates.grpcsharp_request_call_context_deadline_delegate grpcsharp_request_call_context_deadline;
75 public readonly Delegates.grpcsharp_request_call_context_request_metadata_delegate grpcsharp_request_call_context_request_metadata;
76 public readonly Delegates.grpcsharp_request_call_context_destroy_delegate grpcsharp_request_call_context_destroy;
77
Jan Tattermuschafab5412016-01-12 17:57:25 -080078 public readonly Delegates.grpcsharp_composite_call_credentials_create_delegate grpcsharp_composite_call_credentials_create;
79 public readonly Delegates.grpcsharp_call_credentials_release_delegate grpcsharp_call_credentials_release;
80
81 public readonly Delegates.grpcsharp_call_cancel_delegate grpcsharp_call_cancel;
82 public readonly Delegates.grpcsharp_call_cancel_with_status_delegate grpcsharp_call_cancel_with_status;
83 public readonly Delegates.grpcsharp_call_start_unary_delegate grpcsharp_call_start_unary;
84 public readonly Delegates.grpcsharp_call_start_client_streaming_delegate grpcsharp_call_start_client_streaming;
85 public readonly Delegates.grpcsharp_call_start_server_streaming_delegate grpcsharp_call_start_server_streaming;
86 public readonly Delegates.grpcsharp_call_start_duplex_streaming_delegate grpcsharp_call_start_duplex_streaming;
87 public readonly Delegates.grpcsharp_call_send_message_delegate grpcsharp_call_send_message;
88 public readonly Delegates.grpcsharp_call_send_close_from_client_delegate grpcsharp_call_send_close_from_client;
89 public readonly Delegates.grpcsharp_call_send_status_from_server_delegate grpcsharp_call_send_status_from_server;
90 public readonly Delegates.grpcsharp_call_recv_message_delegate grpcsharp_call_recv_message;
91 public readonly Delegates.grpcsharp_call_recv_initial_metadata_delegate grpcsharp_call_recv_initial_metadata;
92 public readonly Delegates.grpcsharp_call_start_serverside_delegate grpcsharp_call_start_serverside;
93 public readonly Delegates.grpcsharp_call_send_initial_metadata_delegate grpcsharp_call_send_initial_metadata;
94 public readonly Delegates.grpcsharp_call_set_credentials_delegate grpcsharp_call_set_credentials;
95 public readonly Delegates.grpcsharp_call_get_peer_delegate grpcsharp_call_get_peer;
96 public readonly Delegates.grpcsharp_call_destroy_delegate grpcsharp_call_destroy;
97
98 public readonly Delegates.grpcsharp_channel_args_create_delegate grpcsharp_channel_args_create;
99 public readonly Delegates.grpcsharp_channel_args_set_string_delegate grpcsharp_channel_args_set_string;
100 public readonly Delegates.grpcsharp_channel_args_set_integer_delegate grpcsharp_channel_args_set_integer;
101 public readonly Delegates.grpcsharp_channel_args_destroy_delegate grpcsharp_channel_args_destroy;
102
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800103 public readonly Delegates.grpcsharp_override_default_ssl_roots grpcsharp_override_default_ssl_roots;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800104 public readonly Delegates.grpcsharp_ssl_credentials_create_delegate grpcsharp_ssl_credentials_create;
105 public readonly Delegates.grpcsharp_composite_channel_credentials_create_delegate grpcsharp_composite_channel_credentials_create;
106 public readonly Delegates.grpcsharp_channel_credentials_release_delegate grpcsharp_channel_credentials_release;
107
108 public readonly Delegates.grpcsharp_insecure_channel_create_delegate grpcsharp_insecure_channel_create;
109 public readonly Delegates.grpcsharp_secure_channel_create_delegate grpcsharp_secure_channel_create;
110 public readonly Delegates.grpcsharp_channel_create_call_delegate grpcsharp_channel_create_call;
111 public readonly Delegates.grpcsharp_channel_check_connectivity_state_delegate grpcsharp_channel_check_connectivity_state;
112 public readonly Delegates.grpcsharp_channel_watch_connectivity_state_delegate grpcsharp_channel_watch_connectivity_state;
113 public readonly Delegates.grpcsharp_channel_get_target_delegate grpcsharp_channel_get_target;
114 public readonly Delegates.grpcsharp_channel_destroy_delegate grpcsharp_channel_destroy;
115
116 public readonly Delegates.grpcsharp_sizeof_grpc_event_delegate grpcsharp_sizeof_grpc_event;
117
118 public readonly Delegates.grpcsharp_completion_queue_create_delegate grpcsharp_completion_queue_create;
119 public readonly Delegates.grpcsharp_completion_queue_shutdown_delegate grpcsharp_completion_queue_shutdown;
120 public readonly Delegates.grpcsharp_completion_queue_next_delegate grpcsharp_completion_queue_next;
121 public readonly Delegates.grpcsharp_completion_queue_pluck_delegate grpcsharp_completion_queue_pluck;
122 public readonly Delegates.grpcsharp_completion_queue_destroy_delegate grpcsharp_completion_queue_destroy;
123
124 public readonly Delegates.gprsharp_free_delegate gprsharp_free;
125
126 public readonly Delegates.grpcsharp_metadata_array_create_delegate grpcsharp_metadata_array_create;
127 public readonly Delegates.grpcsharp_metadata_array_add_delegate grpcsharp_metadata_array_add;
128 public readonly Delegates.grpcsharp_metadata_array_count_delegate grpcsharp_metadata_array_count;
129 public readonly Delegates.grpcsharp_metadata_array_get_key_delegate grpcsharp_metadata_array_get_key;
130 public readonly Delegates.grpcsharp_metadata_array_get_value_delegate grpcsharp_metadata_array_get_value;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800131 public readonly Delegates.grpcsharp_metadata_array_destroy_full_delegate grpcsharp_metadata_array_destroy_full;
132
133 public readonly Delegates.grpcsharp_redirect_log_delegate grpcsharp_redirect_log;
134
135 public readonly Delegates.grpcsharp_metadata_credentials_create_from_plugin_delegate grpcsharp_metadata_credentials_create_from_plugin;
136 public readonly Delegates.grpcsharp_metadata_credentials_notify_from_plugin_delegate grpcsharp_metadata_credentials_notify_from_plugin;
137
138 public readonly Delegates.grpcsharp_ssl_server_credentials_create_delegate grpcsharp_ssl_server_credentials_create;
139 public readonly Delegates.grpcsharp_server_credentials_release_delegate grpcsharp_server_credentials_release;
140
141 public readonly Delegates.grpcsharp_server_create_delegate grpcsharp_server_create;
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400142 public readonly Delegates.grpcsharp_server_register_completion_queue_delegate grpcsharp_server_register_completion_queue;
Jan Tattermuschafab5412016-01-12 17:57:25 -0800143 public readonly Delegates.grpcsharp_server_add_insecure_http2_port_delegate grpcsharp_server_add_insecure_http2_port;
144 public readonly Delegates.grpcsharp_server_add_secure_http2_port_delegate grpcsharp_server_add_secure_http2_port;
145 public readonly Delegates.grpcsharp_server_start_delegate grpcsharp_server_start;
146 public readonly Delegates.grpcsharp_server_request_call_delegate grpcsharp_server_request_call;
147 public readonly Delegates.grpcsharp_server_cancel_all_calls_delegate grpcsharp_server_cancel_all_calls;
148 public readonly Delegates.grpcsharp_server_shutdown_and_notify_callback_delegate grpcsharp_server_shutdown_and_notify_callback;
149 public readonly Delegates.grpcsharp_server_destroy_delegate grpcsharp_server_destroy;
150
151 public readonly Delegates.gprsharp_now_delegate gprsharp_now;
152 public readonly Delegates.gprsharp_inf_future_delegate gprsharp_inf_future;
153 public readonly Delegates.gprsharp_inf_past_delegate gprsharp_inf_past;
154 public readonly Delegates.gprsharp_convert_clock_type_delegate gprsharp_convert_clock_type;
155 public readonly Delegates.gprsharp_sizeof_timespec_delegate gprsharp_sizeof_timespec;
156
157 public readonly Delegates.grpcsharp_test_callback_delegate grpcsharp_test_callback;
158 public readonly Delegates.grpcsharp_test_nop_delegate grpcsharp_test_nop;
159
160 #endregion
161
162 public NativeMethods(UnmanagedLibrary library)
163 {
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100164 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);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800167
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100168 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);
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100175 this.grpcsharp_batch_context_recv_close_on_server_cancelled = GetMethodDelegate<Delegates.grpcsharp_batch_context_recv_close_on_server_cancelled_delegate>(library);
176 this.grpcsharp_batch_context_destroy = GetMethodDelegate<Delegates.grpcsharp_batch_context_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800177
Jan Tattermusch6e901712016-10-21 00:42:37 +0200178 this.grpcsharp_request_call_context_create = GetMethodDelegate<Delegates.grpcsharp_request_call_context_create_delegate>(library);
179 this.grpcsharp_request_call_context_call = GetMethodDelegate<Delegates.grpcsharp_request_call_context_call_delegate>(library);
180 this.grpcsharp_request_call_context_method = GetMethodDelegate<Delegates.grpcsharp_request_call_context_method_delegate>(library);
181 this.grpcsharp_request_call_context_host = GetMethodDelegate<Delegates.grpcsharp_request_call_context_host_delegate>(library);
182 this.grpcsharp_request_call_context_deadline = GetMethodDelegate<Delegates.grpcsharp_request_call_context_deadline_delegate>(library);
183 this.grpcsharp_request_call_context_request_metadata = GetMethodDelegate<Delegates.grpcsharp_request_call_context_request_metadata_delegate>(library);
184 this.grpcsharp_request_call_context_destroy = GetMethodDelegate<Delegates.grpcsharp_request_call_context_destroy_delegate>(library);
185
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100186 this.grpcsharp_composite_call_credentials_create = GetMethodDelegate<Delegates.grpcsharp_composite_call_credentials_create_delegate>(library);
187 this.grpcsharp_call_credentials_release = GetMethodDelegate<Delegates.grpcsharp_call_credentials_release_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800188
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100189 this.grpcsharp_call_cancel = GetMethodDelegate<Delegates.grpcsharp_call_cancel_delegate>(library);
190 this.grpcsharp_call_cancel_with_status = GetMethodDelegate<Delegates.grpcsharp_call_cancel_with_status_delegate>(library);
191 this.grpcsharp_call_start_unary = GetMethodDelegate<Delegates.grpcsharp_call_start_unary_delegate>(library);
192 this.grpcsharp_call_start_client_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_client_streaming_delegate>(library);
193 this.grpcsharp_call_start_server_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_server_streaming_delegate>(library);
194 this.grpcsharp_call_start_duplex_streaming = GetMethodDelegate<Delegates.grpcsharp_call_start_duplex_streaming_delegate>(library);
195 this.grpcsharp_call_send_message = GetMethodDelegate<Delegates.grpcsharp_call_send_message_delegate>(library);
196 this.grpcsharp_call_send_close_from_client = GetMethodDelegate<Delegates.grpcsharp_call_send_close_from_client_delegate>(library);
197 this.grpcsharp_call_send_status_from_server = GetMethodDelegate<Delegates.grpcsharp_call_send_status_from_server_delegate>(library);
198 this.grpcsharp_call_recv_message = GetMethodDelegate<Delegates.grpcsharp_call_recv_message_delegate>(library);
199 this.grpcsharp_call_recv_initial_metadata = GetMethodDelegate<Delegates.grpcsharp_call_recv_initial_metadata_delegate>(library);
200 this.grpcsharp_call_start_serverside = GetMethodDelegate<Delegates.grpcsharp_call_start_serverside_delegate>(library);
201 this.grpcsharp_call_send_initial_metadata = GetMethodDelegate<Delegates.grpcsharp_call_send_initial_metadata_delegate>(library);
202 this.grpcsharp_call_set_credentials = GetMethodDelegate<Delegates.grpcsharp_call_set_credentials_delegate>(library);
203 this.grpcsharp_call_get_peer = GetMethodDelegate<Delegates.grpcsharp_call_get_peer_delegate>(library);
204 this.grpcsharp_call_destroy = GetMethodDelegate<Delegates.grpcsharp_call_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800205
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100206 this.grpcsharp_channel_args_create = GetMethodDelegate<Delegates.grpcsharp_channel_args_create_delegate>(library);
207 this.grpcsharp_channel_args_set_string = GetMethodDelegate<Delegates.grpcsharp_channel_args_set_string_delegate>(library);
208 this.grpcsharp_channel_args_set_integer = GetMethodDelegate<Delegates.grpcsharp_channel_args_set_integer_delegate>(library);
209 this.grpcsharp_channel_args_destroy = GetMethodDelegate<Delegates.grpcsharp_channel_args_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800210
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100211 this.grpcsharp_override_default_ssl_roots = GetMethodDelegate<Delegates.grpcsharp_override_default_ssl_roots>(library);
212 this.grpcsharp_ssl_credentials_create = GetMethodDelegate<Delegates.grpcsharp_ssl_credentials_create_delegate>(library);
213 this.grpcsharp_composite_channel_credentials_create = GetMethodDelegate<Delegates.grpcsharp_composite_channel_credentials_create_delegate>(library);
214 this.grpcsharp_channel_credentials_release = GetMethodDelegate<Delegates.grpcsharp_channel_credentials_release_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800215
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100216 this.grpcsharp_insecure_channel_create = GetMethodDelegate<Delegates.grpcsharp_insecure_channel_create_delegate>(library);
217 this.grpcsharp_secure_channel_create = GetMethodDelegate<Delegates.grpcsharp_secure_channel_create_delegate>(library);
218 this.grpcsharp_channel_create_call = GetMethodDelegate<Delegates.grpcsharp_channel_create_call_delegate>(library);
219 this.grpcsharp_channel_check_connectivity_state = GetMethodDelegate<Delegates.grpcsharp_channel_check_connectivity_state_delegate>(library);
220 this.grpcsharp_channel_watch_connectivity_state = GetMethodDelegate<Delegates.grpcsharp_channel_watch_connectivity_state_delegate>(library);
221 this.grpcsharp_channel_get_target = GetMethodDelegate<Delegates.grpcsharp_channel_get_target_delegate>(library);
222 this.grpcsharp_channel_destroy = GetMethodDelegate<Delegates.grpcsharp_channel_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800223
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100224 this.grpcsharp_sizeof_grpc_event = GetMethodDelegate<Delegates.grpcsharp_sizeof_grpc_event_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800225
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100226 this.grpcsharp_completion_queue_create = GetMethodDelegate<Delegates.grpcsharp_completion_queue_create_delegate>(library);
227 this.grpcsharp_completion_queue_shutdown = GetMethodDelegate<Delegates.grpcsharp_completion_queue_shutdown_delegate>(library);
228 this.grpcsharp_completion_queue_next = GetMethodDelegate<Delegates.grpcsharp_completion_queue_next_delegate>(library);
229 this.grpcsharp_completion_queue_pluck = GetMethodDelegate<Delegates.grpcsharp_completion_queue_pluck_delegate>(library);
230 this.grpcsharp_completion_queue_destroy = GetMethodDelegate<Delegates.grpcsharp_completion_queue_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800231
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100232 this.gprsharp_free = GetMethodDelegate<Delegates.gprsharp_free_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800233
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100234 this.grpcsharp_metadata_array_create = GetMethodDelegate<Delegates.grpcsharp_metadata_array_create_delegate>(library);
235 this.grpcsharp_metadata_array_add = GetMethodDelegate<Delegates.grpcsharp_metadata_array_add_delegate>(library);
236 this.grpcsharp_metadata_array_count = GetMethodDelegate<Delegates.grpcsharp_metadata_array_count_delegate>(library);
237 this.grpcsharp_metadata_array_get_key = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_key_delegate>(library);
238 this.grpcsharp_metadata_array_get_value = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_value_delegate>(library);
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100239 this.grpcsharp_metadata_array_destroy_full = GetMethodDelegate<Delegates.grpcsharp_metadata_array_destroy_full_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800240
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100241 this.grpcsharp_redirect_log = GetMethodDelegate<Delegates.grpcsharp_redirect_log_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800242
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100243 this.grpcsharp_metadata_credentials_create_from_plugin = GetMethodDelegate<Delegates.grpcsharp_metadata_credentials_create_from_plugin_delegate>(library);
244 this.grpcsharp_metadata_credentials_notify_from_plugin = GetMethodDelegate<Delegates.grpcsharp_metadata_credentials_notify_from_plugin_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800245
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100246 this.grpcsharp_ssl_server_credentials_create = GetMethodDelegate<Delegates.grpcsharp_ssl_server_credentials_create_delegate>(library);
247 this.grpcsharp_server_credentials_release = GetMethodDelegate<Delegates.grpcsharp_server_credentials_release_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800248
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100249 this.grpcsharp_server_create = GetMethodDelegate<Delegates.grpcsharp_server_create_delegate>(library);
250 this.grpcsharp_server_register_completion_queue = GetMethodDelegate<Delegates.grpcsharp_server_register_completion_queue_delegate>(library);
251 this.grpcsharp_server_add_insecure_http2_port = GetMethodDelegate<Delegates.grpcsharp_server_add_insecure_http2_port_delegate>(library);
252 this.grpcsharp_server_add_secure_http2_port = GetMethodDelegate<Delegates.grpcsharp_server_add_secure_http2_port_delegate>(library);
253 this.grpcsharp_server_start = GetMethodDelegate<Delegates.grpcsharp_server_start_delegate>(library);
254 this.grpcsharp_server_request_call = GetMethodDelegate<Delegates.grpcsharp_server_request_call_delegate>(library);
255 this.grpcsharp_server_cancel_all_calls = GetMethodDelegate<Delegates.grpcsharp_server_cancel_all_calls_delegate>(library);
256 this.grpcsharp_server_shutdown_and_notify_callback = GetMethodDelegate<Delegates.grpcsharp_server_shutdown_and_notify_callback_delegate>(library);
257 this.grpcsharp_server_destroy = GetMethodDelegate<Delegates.grpcsharp_server_destroy_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800258
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100259 this.gprsharp_now = GetMethodDelegate<Delegates.gprsharp_now_delegate>(library);
260 this.gprsharp_inf_future = GetMethodDelegate<Delegates.gprsharp_inf_future_delegate>(library);
261 this.gprsharp_inf_past = GetMethodDelegate<Delegates.gprsharp_inf_past_delegate>(library);
262 this.gprsharp_convert_clock_type = GetMethodDelegate<Delegates.gprsharp_convert_clock_type_delegate>(library);
263 this.gprsharp_sizeof_timespec = GetMethodDelegate<Delegates.gprsharp_sizeof_timespec_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800264
Jon Skeet3fcd20f2016-07-08 14:38:39 +0100265 this.grpcsharp_test_callback = GetMethodDelegate<Delegates.grpcsharp_test_callback_delegate>(library);
266 this.grpcsharp_test_nop = GetMethodDelegate<Delegates.grpcsharp_test_nop_delegate>(library);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800267 }
268
269 /// <summary>
270 /// Gets singleton instance of this class.
271 /// </summary>
272 public static NativeMethods Get()
273 {
274 return NativeExtension.Get().NativeMethods;
275 }
276
277 static T GetMethodDelegate<T>(UnmanagedLibrary library)
278 where T : class
279 {
280 var methodName = RemoveStringSuffix(typeof(T).Name, "_delegate");
281 return library.GetNativeMethodDelegate<T>(methodName);
282 }
283
284 static string RemoveStringSuffix(string str, string toRemove)
285 {
286 if (!str.EndsWith(toRemove))
287 {
288 return str;
289 }
290 return str.Substring(0, str.Length - toRemove.Length);
291 }
292
293 /// <summary>
294 /// Delegate types for all published native methods. Declared under inner class to prevent scope pollution.
295 /// </summary>
296 public class Delegates
297 {
298 public delegate void grpcsharp_init_delegate();
299 public delegate void grpcsharp_shutdown_delegate();
300 public delegate IntPtr grpcsharp_version_string_delegate(); // returns not-owned const char*
301
302 public delegate BatchContextSafeHandle grpcsharp_batch_context_create_delegate();
303 public delegate IntPtr grpcsharp_batch_context_recv_initial_metadata_delegate(BatchContextSafeHandle ctx);
304 public delegate IntPtr grpcsharp_batch_context_recv_message_length_delegate(BatchContextSafeHandle ctx);
305 public delegate void grpcsharp_batch_context_recv_message_to_buffer_delegate(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen);
306 public delegate StatusCode grpcsharp_batch_context_recv_status_on_client_status_delegate(BatchContextSafeHandle ctx);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800307 public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx, out UIntPtr detailsLength);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800308 public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate(BatchContextSafeHandle ctx);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800309 public delegate int grpcsharp_batch_context_recv_close_on_server_cancelled_delegate(BatchContextSafeHandle ctx);
310 public delegate void grpcsharp_batch_context_destroy_delegate(IntPtr ctx);
311
Jan Tattermusch6e901712016-10-21 00:42:37 +0200312 public delegate RequestCallContextSafeHandle grpcsharp_request_call_context_create_delegate();
313 public delegate CallSafeHandle grpcsharp_request_call_context_call_delegate(RequestCallContextSafeHandle ctx);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800314 public delegate IntPtr grpcsharp_request_call_context_method_delegate(RequestCallContextSafeHandle ctx, out UIntPtr methodLength);
315 public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx, out UIntPtr hostLength);
Jan Tattermusch6e901712016-10-21 00:42:37 +0200316 public delegate Timespec grpcsharp_request_call_context_deadline_delegate(RequestCallContextSafeHandle ctx);
317 public delegate IntPtr grpcsharp_request_call_context_request_metadata_delegate(RequestCallContextSafeHandle ctx);
318 public delegate void grpcsharp_request_call_context_destroy_delegate(IntPtr ctx);
319
Jan Tattermuschafab5412016-01-12 17:57:25 -0800320 public delegate CallCredentialsSafeHandle grpcsharp_composite_call_credentials_create_delegate(CallCredentialsSafeHandle creds1, CallCredentialsSafeHandle creds2);
321 public delegate void grpcsharp_call_credentials_release_delegate(IntPtr credentials);
322
Jan Tattermusche0af2862016-05-18 21:02:03 -0700323 public delegate CallError grpcsharp_call_cancel_delegate(CallSafeHandle call);
324 public delegate CallError grpcsharp_call_cancel_with_status_delegate(CallSafeHandle call, StatusCode status, string description);
325 public delegate CallError grpcsharp_call_start_unary_delegate(CallSafeHandle call,
Jan Tattermusch43416da2016-11-22 17:28:22 +0100326 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700327 public delegate CallError grpcsharp_call_start_client_streaming_delegate(CallSafeHandle call,
Jan Tattermusch43416da2016-11-22 17:28:22 +0100328 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700329 public delegate CallError grpcsharp_call_start_server_streaming_delegate(CallSafeHandle call,
Jan Tattermusch43416da2016-11-22 17:28:22 +0100330 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags,
331 MetadataArraySafeHandle metadataArray, CallFlags metadataFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700332 public delegate CallError grpcsharp_call_start_duplex_streaming_delegate(CallSafeHandle call,
Jan Tattermusch43416da2016-11-22 17:28:22 +0100333 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700334 public delegate CallError grpcsharp_call_send_message_delegate(CallSafeHandle call,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700335 BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, bool sendEmptyInitialMetadata);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700336 public delegate CallError grpcsharp_call_send_close_from_client_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800337 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700338 public delegate CallError grpcsharp_call_send_status_from_server_delegate(CallSafeHandle call,
Jan Tattermusch60324922017-01-18 21:15:12 +0100339 BatchContextSafeHandle ctx, StatusCode statusCode, byte[] statusMessage, UIntPtr statusMessageLen, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata,
Jan Tattermusch305ffd42016-05-04 10:26:24 -0700340 byte[] optionalSendBuffer, UIntPtr optionalSendBufferLen, WriteFlags writeFlags);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700341 public delegate CallError grpcsharp_call_recv_message_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800342 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700343 public delegate CallError grpcsharp_call_recv_initial_metadata_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800344 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700345 public delegate CallError grpcsharp_call_start_serverside_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800346 BatchContextSafeHandle ctx);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700347 public delegate CallError grpcsharp_call_send_initial_metadata_delegate(CallSafeHandle call,
Jan Tattermuschafab5412016-01-12 17:57:25 -0800348 BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
Jan Tattermusche0af2862016-05-18 21:02:03 -0700349 public delegate CallError grpcsharp_call_set_credentials_delegate(CallSafeHandle call, CallCredentialsSafeHandle credentials);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800350 public delegate CStringSafeHandle grpcsharp_call_get_peer_delegate(CallSafeHandle call);
351 public delegate void grpcsharp_call_destroy_delegate(IntPtr call);
352
353 public delegate ChannelArgsSafeHandle grpcsharp_channel_args_create_delegate(UIntPtr numArgs);
354 public delegate void grpcsharp_channel_args_set_string_delegate(ChannelArgsSafeHandle args, UIntPtr index, string key, string value);
355 public delegate void grpcsharp_channel_args_set_integer_delegate(ChannelArgsSafeHandle args, UIntPtr index, string key, int value);
356 public delegate void grpcsharp_channel_args_destroy_delegate(IntPtr args);
357
Jan Tattermusch660c15b2016-02-02 06:39:10 -0800358 public delegate void grpcsharp_override_default_ssl_roots(string pemRootCerts);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800359 public delegate ChannelCredentialsSafeHandle grpcsharp_ssl_credentials_create_delegate(string pemRootCerts, string keyCertPairCertChain, string keyCertPairPrivateKey);
360 public delegate ChannelCredentialsSafeHandle grpcsharp_composite_channel_credentials_create_delegate(ChannelCredentialsSafeHandle channelCreds, CallCredentialsSafeHandle callCreds);
361 public delegate void grpcsharp_channel_credentials_release_delegate(IntPtr credentials);
362
363 public delegate ChannelSafeHandle grpcsharp_insecure_channel_create_delegate(string target, ChannelArgsSafeHandle channelArgs);
364 public delegate ChannelSafeHandle grpcsharp_secure_channel_create_delegate(ChannelCredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs);
365 public delegate CallSafeHandle grpcsharp_channel_create_call_delegate(ChannelSafeHandle channel, CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline);
366 public delegate ChannelState grpcsharp_channel_check_connectivity_state_delegate(ChannelSafeHandle channel, int tryToConnect);
367 public delegate void grpcsharp_channel_watch_connectivity_state_delegate(ChannelSafeHandle channel, ChannelState lastObservedState,
368 Timespec deadline, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
369 public delegate CStringSafeHandle grpcsharp_channel_get_target_delegate(ChannelSafeHandle call);
370 public delegate void grpcsharp_channel_destroy_delegate(IntPtr channel);
371
372 public delegate int grpcsharp_sizeof_grpc_event_delegate();
373
374 public delegate CompletionQueueSafeHandle grpcsharp_completion_queue_create_delegate();
375 public delegate void grpcsharp_completion_queue_shutdown_delegate(CompletionQueueSafeHandle cq);
376 public delegate CompletionQueueEvent grpcsharp_completion_queue_next_delegate(CompletionQueueSafeHandle cq);
377 public delegate CompletionQueueEvent grpcsharp_completion_queue_pluck_delegate(CompletionQueueSafeHandle cq, IntPtr tag);
378 public delegate void grpcsharp_completion_queue_destroy_delegate(IntPtr cq);
379
380 public delegate void gprsharp_free_delegate(IntPtr ptr);
381
382 public delegate MetadataArraySafeHandle grpcsharp_metadata_array_create_delegate(UIntPtr capacity);
383 public delegate void grpcsharp_metadata_array_add_delegate(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength);
384 public delegate UIntPtr grpcsharp_metadata_array_count_delegate(IntPtr metadataArray);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800385 public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr keyLength);
386 public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr valueLength);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800387 public delegate void grpcsharp_metadata_array_destroy_full_delegate(IntPtr array);
388
389 public delegate void grpcsharp_redirect_log_delegate(GprLogDelegate callback);
390
391 public delegate CallCredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin_delegate(NativeMetadataInterceptor interceptor);
392 public delegate void grpcsharp_metadata_credentials_notify_from_plugin_delegate(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails);
393
394 public delegate ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create_delegate(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs, bool forceClientAuth);
395 public delegate void grpcsharp_server_credentials_release_delegate(IntPtr credentials);
396
Jan Tattermusch5ee8e772016-05-24 16:17:10 -0400397 public delegate ServerSafeHandle grpcsharp_server_create_delegate(ChannelArgsSafeHandle args);
398 public delegate void grpcsharp_server_register_completion_queue_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800399 public delegate int grpcsharp_server_add_insecure_http2_port_delegate(ServerSafeHandle server, string addr);
400 public delegate int grpcsharp_server_add_secure_http2_port_delegate(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds);
401 public delegate void grpcsharp_server_start_delegate(ServerSafeHandle server);
Jan Tattermusch6e901712016-10-21 00:42:37 +0200402 public delegate CallError grpcsharp_server_request_call_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq, RequestCallContextSafeHandle ctx);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800403 public delegate void grpcsharp_server_cancel_all_calls_delegate(ServerSafeHandle server);
404 public delegate void grpcsharp_server_shutdown_and_notify_callback_delegate(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
405 public delegate void grpcsharp_server_destroy_delegate(IntPtr server);
406
Jan Tattermusche0af2862016-05-18 21:02:03 -0700407 public delegate Timespec gprsharp_now_delegate(ClockType clockType);
408 public delegate Timespec gprsharp_inf_future_delegate(ClockType clockType);
409 public delegate Timespec gprsharp_inf_past_delegate(ClockType clockType);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800410
Jan Tattermusche0af2862016-05-18 21:02:03 -0700411 public delegate Timespec gprsharp_convert_clock_type_delegate(Timespec t, ClockType targetClock);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800412 public delegate int gprsharp_sizeof_timespec_delegate();
413
Jan Tattermusche0af2862016-05-18 21:02:03 -0700414 public delegate CallError grpcsharp_test_callback_delegate([MarshalAs(UnmanagedType.FunctionPtr)] OpCompletionDelegate callback);
Jan Tattermuschafab5412016-01-12 17:57:25 -0800415 public delegate IntPtr grpcsharp_test_nop_delegate(IntPtr ptr);
416 }
Jan Tattermuschafab5412016-01-12 17:57:25 -0800417 }
418}