1%YAML 1.2
2--- |
3  <%
4    native_method_signatures = [
5      'void grpcsharp_init()',
6      'void grpcsharp_shutdown()',
7      'IntPtr grpcsharp_version_string()  // returns not-owned const char*',
8      'BatchContextSafeHandle grpcsharp_batch_context_create()',
9      'IntPtr grpcsharp_batch_context_recv_initial_metadata(BatchContextSafeHandle ctx)',
10      'IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx)',
11      'void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen)',
12      'StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandle ctx)',
13      'IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx, out UIntPtr detailsLength)',
14      'IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata(BatchContextSafeHandle ctx)',
15      'int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandle ctx)',
16      'void grpcsharp_batch_context_reset(BatchContextSafeHandle ctx)',
17      'void grpcsharp_batch_context_destroy(IntPtr ctx)',
18      'RequestCallContextSafeHandle grpcsharp_request_call_context_create()',
19      'CallSafeHandle grpcsharp_request_call_context_call(RequestCallContextSafeHandle ctx)',
20      'IntPtr grpcsharp_request_call_context_method(RequestCallContextSafeHandle ctx, out UIntPtr methodLength)',
21      'IntPtr grpcsharp_request_call_context_host(RequestCallContextSafeHandle ctx, out UIntPtr hostLength)',
22      'Timespec grpcsharp_request_call_context_deadline(RequestCallContextSafeHandle ctx)',
23      'IntPtr grpcsharp_request_call_context_request_metadata(RequestCallContextSafeHandle ctx)',
24      'void grpcsharp_request_call_context_reset(RequestCallContextSafeHandle ctx)',
25      'void grpcsharp_request_call_context_destroy(IntPtr ctx)',
26      'CallCredentialsSafeHandle grpcsharp_composite_call_credentials_create(CallCredentialsSafeHandle creds1, CallCredentialsSafeHandle creds2)',
27      'void grpcsharp_call_credentials_release(IntPtr credentials)',
28      'CallError grpcsharp_call_cancel(CallSafeHandle call)',
29      'CallError grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, string description)',
30      'CallError grpcsharp_call_start_unary(CallSafeHandle call, BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags)',
31      'CallError grpcsharp_call_start_client_streaming(CallSafeHandle call, BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags)',
32      'CallError grpcsharp_call_start_server_streaming(CallSafeHandle call, BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags)',
33      'CallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call, BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray, CallFlags metadataFlags)',
34      'CallError grpcsharp_call_send_message(CallSafeHandle call, BatchContextSafeHandle ctx, byte[] sendBuffer, UIntPtr sendBufferLen, WriteFlags writeFlags, int sendEmptyInitialMetadata)',
35      'CallError grpcsharp_call_send_close_from_client(CallSafeHandle call, BatchContextSafeHandle ctx)',
36      'CallError grpcsharp_call_send_status_from_server(CallSafeHandle call, BatchContextSafeHandle ctx, StatusCode statusCode, byte[] statusMessage, UIntPtr statusMessageLen, MetadataArraySafeHandle metadataArray, int sendEmptyInitialMetadata, byte[] optionalSendBuffer, UIntPtr optionalSendBufferLen, WriteFlags writeFlags)',
37      'CallError grpcsharp_call_recv_message(CallSafeHandle call, BatchContextSafeHandle ctx)',
38      'CallError grpcsharp_call_recv_initial_metadata(CallSafeHandle call, BatchContextSafeHandle ctx)',
39      'CallError grpcsharp_call_start_serverside(CallSafeHandle call, BatchContextSafeHandle ctx)',
40      'CallError grpcsharp_call_send_initial_metadata(CallSafeHandle call, BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray)',
41      'CallError grpcsharp_call_set_credentials(CallSafeHandle call, CallCredentialsSafeHandle credentials)',
42      'CStringSafeHandle grpcsharp_call_get_peer(CallSafeHandle call)',
43      'void grpcsharp_call_destroy(IntPtr call)',
44      'ChannelArgsSafeHandle grpcsharp_channel_args_create(UIntPtr numArgs)',
45      'void grpcsharp_channel_args_set_string(ChannelArgsSafeHandle args, UIntPtr index, string key, string value)',
46      'void grpcsharp_channel_args_set_integer(ChannelArgsSafeHandle args, UIntPtr index, string key, int value)',
47      'void grpcsharp_channel_args_destroy(IntPtr args)',
48      'void grpcsharp_override_default_ssl_roots(string pemRootCerts)',
49      'ChannelCredentialsSafeHandle grpcsharp_ssl_credentials_create(string pemRootCerts, string keyCertPairCertChain, string keyCertPairPrivateKey)',
50      'ChannelCredentialsSafeHandle grpcsharp_composite_channel_credentials_create(ChannelCredentialsSafeHandle channelCreds, CallCredentialsSafeHandle callCreds)',
51      'void grpcsharp_channel_credentials_release(IntPtr credentials)',
52      'ChannelSafeHandle grpcsharp_insecure_channel_create(string target, ChannelArgsSafeHandle channelArgs)',
53      'ChannelSafeHandle grpcsharp_secure_channel_create(ChannelCredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs)',
54      'CallSafeHandle grpcsharp_channel_create_call(ChannelSafeHandle channel, CallSafeHandle parentCall, ContextPropagationFlags propagationMask, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline)',
55      'ChannelState grpcsharp_channel_check_connectivity_state(ChannelSafeHandle channel, int tryToConnect)',
56      'void grpcsharp_channel_watch_connectivity_state(ChannelSafeHandle channel, ChannelState lastObservedState, Timespec deadline, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx)',
57      'CStringSafeHandle grpcsharp_channel_get_target(ChannelSafeHandle call)',
58      'void grpcsharp_channel_destroy(IntPtr channel)',
59      'int grpcsharp_sizeof_grpc_event()',
60      'CompletionQueueSafeHandle grpcsharp_completion_queue_create_async()',
61      'CompletionQueueSafeHandle grpcsharp_completion_queue_create_sync()',
62      'void grpcsharp_completion_queue_shutdown(CompletionQueueSafeHandle cq)',
63      'CompletionQueueEvent grpcsharp_completion_queue_next(CompletionQueueSafeHandle cq)',
64      'CompletionQueueEvent grpcsharp_completion_queue_pluck(CompletionQueueSafeHandle cq, IntPtr tag)',
65      'void grpcsharp_completion_queue_destroy(IntPtr cq)',
66      'void gprsharp_free(IntPtr ptr)',
67      'MetadataArraySafeHandle grpcsharp_metadata_array_create(UIntPtr capacity)',
68      'void grpcsharp_metadata_array_add(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength)',
69      'UIntPtr grpcsharp_metadata_array_count(IntPtr metadataArray)',
70      'IntPtr grpcsharp_metadata_array_get_key(IntPtr metadataArray, UIntPtr index, out UIntPtr keyLength)',
71      'IntPtr grpcsharp_metadata_array_get_value(IntPtr metadataArray, UIntPtr index, out UIntPtr valueLength)',
72      'void grpcsharp_metadata_array_destroy_full(IntPtr array)',
73      'void grpcsharp_redirect_log(GprLogDelegate callback)',
74      'CallCredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin(NativeMetadataInterceptor interceptor)',
75      'void grpcsharp_metadata_credentials_notify_from_plugin(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails)',
76      'ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs, int forceClientAuth)',
77      'void grpcsharp_server_credentials_release(IntPtr credentials)',
78      'ServerSafeHandle grpcsharp_server_create(ChannelArgsSafeHandle args)',
79      'void grpcsharp_server_register_completion_queue(ServerSafeHandle server, CompletionQueueSafeHandle cq)',
80      'int grpcsharp_server_add_insecure_http2_port(ServerSafeHandle server, string addr)',
81      'int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds)',
82      'void grpcsharp_server_start(ServerSafeHandle server)',
83      'CallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, RequestCallContextSafeHandle ctx)',
84      'void grpcsharp_server_cancel_all_calls(ServerSafeHandle server)',
85      'void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx)',
86      'void grpcsharp_server_destroy(IntPtr server)',
87      'AuthContextSafeHandle grpcsharp_call_auth_context(CallSafeHandle call)',
88      'IntPtr grpcsharp_auth_context_peer_identity_property_name(AuthContextSafeHandle authContext)  // returns const char*',
89      'AuthContextSafeHandle.NativeAuthPropertyIterator grpcsharp_auth_context_property_iterator(AuthContextSafeHandle authContext)',
90      'IntPtr grpcsharp_auth_property_iterator_next(ref AuthContextSafeHandle.NativeAuthPropertyIterator iterator)  // returns const auth_property*',
91      'void grpcsharp_auth_context_release(IntPtr authContext)',
92      'Timespec gprsharp_now(ClockType clockType)',
93      'Timespec gprsharp_inf_future(ClockType clockType)',
94      'Timespec gprsharp_inf_past(ClockType clockType)',
95      'Timespec gprsharp_convert_clock_type(Timespec t, ClockType targetClock)',
96      'int gprsharp_sizeof_timespec()',
97      'CallError grpcsharp_test_callback([MarshalAs(UnmanagedType.FunctionPtr)] NativeCallbackTestDelegate callback)',
98      'IntPtr grpcsharp_test_nop(IntPtr ptr)',
99      'void grpcsharp_test_override_method(string methodName, string variant)',
100    ]
101
102    import re
103    native_methods = []
104    for signature in native_method_signatures:
105      match = re.match('([A-Za-z0-9_.]+) +([A-Za-z0-9_]+)\\((.*)\\)(.*)', signature)
106      if not match:
107        raise Exception('Malformed signature "%s"' % signature)
108      native_methods.append({'returntype': match.group(1), 'name': match.group(2), 'params': match.group(3), 'comment': match.group(4)})
109  %>
110  #region Copyright notice and license
111
112  // Copyright 2015 gRPC authors.
113  //
114  // Licensed under the Apache License, Version 2.0 (the "License");
115  // you may not use this file except in compliance with the License.
116  // You may obtain a copy of the License at
117  //
118  //     http://www.apache.org/licenses/LICENSE-2.0
119  //
120  // Unless required by applicable law or agreed to in writing, software
121  // distributed under the License is distributed on an "AS IS" BASIS,
122  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123  // See the License for the specific language governing permissions and
124  // limitations under the License.
125
126  #endregion
127
128  using System;
129  using System.Collections.Concurrent;
130  using System.Diagnostics;
131  using System.IO;
132  using System.Reflection;
133  using System.Runtime.InteropServices;
134  using System.Threading;
135
136  using Grpc.Core.Logging;
137  using Grpc.Core.Utils;
138
139  namespace Grpc.Core.Internal
140  {
141      internal partial class NativeMethods
142      {
143          #region Native methods
144
145          % for method in native_methods:
146          public readonly Delegates.${method['name']}_delegate ${method['name']};
147          % endfor
148
149          #endregion
150
151          public NativeMethods(UnmanagedLibrary library)
152          {
153              % for method in native_methods:
154              this.${method['name']} = GetMethodDelegate<Delegates.${method['name']}_delegate>(library);
155              % endfor
156          }
157
158          public NativeMethods(DllImportsFromStaticLib unusedInstance)
159          {
160              % for method in native_methods:
161              this.${method['name']} = DllImportsFromStaticLib.${method['name']};
162              % endfor
163          }
164
165          public NativeMethods(DllImportsFromSharedLib unusedInstance)
166          {
167              % for method in native_methods:
168              this.${method['name']} = DllImportsFromSharedLib.${method['name']};
169              % endfor
170          }
171
172          /// <summary>
173          /// Delegate types for all published native methods. Declared under inner class to prevent scope pollution.
174          /// </summary>
175          public class Delegates
176          {
177              % for method in native_methods:
178              public delegate ${method['returntype']} ${method['name']}_delegate(${method['params']});${method['comment']}
179              % endfor
180          }
181
182          /// <summary>
183          /// grpc_csharp_ext used as a static library (e.g Unity iOS).
184          /// </summary>
185          internal class DllImportsFromStaticLib
186          {
187              private const string ImportName = "__Internal";
188              % for method in native_methods:
189
190              [DllImport(ImportName)]
191              public static extern ${method['returntype']} ${method['name']}(${method['params']});
192              % endfor
193          }
194
195          /// <summary>
196          /// grpc_csharp_ext used a shared library (e.g on Unity Standalone and Android).
197          /// </summary>
198          internal class DllImportsFromSharedLib
199          {
200              private const string ImportName = "grpc_csharp_ext";
201              % for method in native_methods:
202
203              [DllImport(ImportName)]
204              public static extern ${method['returntype']} ${method['name']}(${method['params']});
205              % endfor
206          }
207      }
208  }
209