blob: 6a2add54db583d7f897f5a233ab372e8f58e103a [file] [log] [blame]
Jan Tattermusch337a2dd2015-02-13 15:41:41 -08001#region Copyright notice and license
2
3// Copyright 2015, Google Inc.
4// 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.Runtime.InteropServices;
Jan Tattermusch30868622015-02-19 09:22:33 -080036using Grpc.Core;
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080037
Jan Tattermusch30868622015-02-19 09:22:33 -080038namespace Grpc.Core.Internal
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080039{
40 /// <summary>
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080041 /// grpcsharp_batch_context
42 /// </summary>
Jan Tattermuschd3677482015-06-01 19:27:40 -070043 internal class BatchContextSafeHandle : SafeHandleZeroIsInvalid
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080044 {
45 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070046 static extern BatchContextSafeHandle grpcsharp_batch_context_create();
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080047
48 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch77415b62015-07-20 20:18:26 -070049 static extern IntPtr grpcsharp_batch_context_recv_initial_metadata(BatchContextSafeHandle ctx);
Jan Tattermuschec50f282015-07-20 19:44:29 -070050
51 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070052 static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080053
54 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070055 static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080056
57 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070058 static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandle ctx);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080059
60 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070061 static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx); // returns const char*
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080062
63 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschec50f282015-07-20 19:44:29 -070064 static extern IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata(BatchContextSafeHandle ctx);
65
66 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070067 static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandle ctx);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080068
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -070069 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070070 static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char*
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -070071
Jan Tattermuschd3677482015-06-01 19:27:40 -070072 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermusch77415b62015-07-20 20:18:26 -070073 static extern IntPtr grpcsharp_batch_context_server_rpc_new_host(BatchContextSafeHandle ctx); // returns const char*
74
75 [DllImport("grpc_csharp_ext.dll")]
76 static extern Timespec grpcsharp_batch_context_server_rpc_new_deadline(BatchContextSafeHandle ctx);
77
78 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschec50f282015-07-20 19:44:29 -070079 static extern IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata(BatchContextSafeHandle ctx);
80
81 [DllImport("grpc_csharp_ext.dll")]
Jan Tattermuschd3677482015-06-01 19:27:40 -070082 static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandle ctx);
83
84 [DllImport("grpc_csharp_ext.dll")]
85 static extern void grpcsharp_batch_context_destroy(IntPtr ctx);
86
87 private BatchContextSafeHandle()
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080088 {
Jan Tattermuschd3677482015-06-01 19:27:40 -070089 }
90
91 public static BatchContextSafeHandle Create()
92 {
93 return grpcsharp_batch_context_create();
94 }
95
96 public IntPtr Handle
97 {
98 get
99 {
100 return handle;
101 }
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800102 }
103
Jan Tattermusch77415b62015-07-20 20:18:26 -0700104 // Gets data of recv_initial_metadata completion.
Jan Tattermuschec50f282015-07-20 19:44:29 -0700105 public Metadata GetReceivedInitialMetadata()
106 {
Jan Tattermusch77415b62015-07-20 20:18:26 -0700107 IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_initial_metadata(this);
Jan Tattermuschec50f282015-07-20 19:44:29 -0700108 return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr);
109 }
Jan Tattermusch77415b62015-07-20 20:18:26 -0700110
111 // Gets data of recv_status_on_client completion.
112 public ClientSideStatus GetReceivedStatusOnClient()
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800113 {
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800114 string details = Marshal.PtrToStringAnsi(grpcsharp_batch_context_recv_status_on_client_details(this));
Jan Tattermusch77415b62015-07-20 20:18:26 -0700115 var status = new Status(grpcsharp_batch_context_recv_status_on_client_status(this), details);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800116
Jan Tattermuschec50f282015-07-20 19:44:29 -0700117 IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this);
Jan Tattermusch77415b62015-07-20 20:18:26 -0700118 var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr);
119
120 return new ClientSideStatus(status, metadata);
Jan Tattermuschec50f282015-07-20 19:44:29 -0700121 }
122
Jan Tattermusch77415b62015-07-20 20:18:26 -0700123 // Gets data of recv_message completion.
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800124 public byte[] GetReceivedMessage()
125 {
126 IntPtr len = grpcsharp_batch_context_recv_message_length(this);
127 if (len == new IntPtr(-1))
128 {
129 return null;
130 }
Jan Tattermusch075dde42015-03-11 18:21:00 -0700131 byte[] data = new byte[(int)len];
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800132 grpcsharp_batch_context_recv_message_to_buffer(this, data, new UIntPtr((ulong)data.Length));
133 return data;
134 }
135
Jan Tattermusch77415b62015-07-20 20:18:26 -0700136 // Gets data of server_rpc_new completion.
137 public ServerRpcNew GetServerRpcNew()
Jan Tattermusch075dde42015-03-11 18:21:00 -0700138 {
Jan Tattermusch77415b62015-07-20 20:18:26 -0700139 var call = grpcsharp_batch_context_server_rpc_new_call(this);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800140
Jan Tattermusch77415b62015-07-20 20:18:26 -0700141 var method = Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_method(this));
142 var host = Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_host(this));
143 var deadline = grpcsharp_batch_context_server_rpc_new_deadline(this);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700144
Jan Tattermuschec50f282015-07-20 19:44:29 -0700145 IntPtr metadataArrayPtr = grpcsharp_batch_context_server_rpc_new_request_metadata(this);
Jan Tattermusch77415b62015-07-20 20:18:26 -0700146 var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr);
147
148 return new ServerRpcNew(call, method, host, deadline, metadata);
Jan Tattermuschec50f282015-07-20 19:44:29 -0700149 }
150
Jan Tattermusch77415b62015-07-20 20:18:26 -0700151 // Gets data of receive_close_on_server completion.
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700152 public bool GetReceivedCloseOnServerCancelled()
153 {
154 return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0;
155 }
Jan Tattermuschd3677482015-06-01 19:27:40 -0700156
157 protected override bool ReleaseHandle()
158 {
159 grpcsharp_batch_context_destroy(handle);
160 return true;
161 }
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800162 }
Jan Tattermusch77415b62015-07-20 20:18:26 -0700163
164 /// <summary>
165 /// Status + metadata received on client side when call finishes.
166 /// (when receive_status_on_client operation finishes).
167 /// </summary>
168 internal struct ClientSideStatus
169 {
170 readonly Status status;
171 readonly Metadata trailers;
172
173 public ClientSideStatus(Status status, Metadata trailers)
174 {
175 this.status = status;
176 this.trailers = trailers;
177 }
178
179 public Status Status
180 {
181 get
182 {
183 return this.status;
184 }
185 }
186
187 public Metadata Trailers
188 {
189 get
190 {
191 return this.trailers;
192 }
193 }
194 }
195
196 /// <summary>
197 /// Details of a newly received RPC.
198 /// </summary>
199 internal struct ServerRpcNew
200 {
201 readonly CallSafeHandle call;
202 readonly string method;
203 readonly string host;
204 readonly Timespec deadline;
205 readonly Metadata requestMetadata;
206
207 public ServerRpcNew(CallSafeHandle call, string method, string host, Timespec deadline, Metadata requestMetadata)
208 {
209 this.call = call;
210 this.method = method;
211 this.host = host;
212 this.deadline = deadline;
213 this.requestMetadata = requestMetadata;
214 }
215
216 public CallSafeHandle Call
217 {
218 get
219 {
220 return this.call;
221 }
222 }
223
224 public string Method
225 {
226 get
227 {
228 return this.method;
229 }
230 }
231
232 public string Host
233 {
234 get
235 {
236 return this.host;
237 }
238 }
239
240 public Timespec Deadline
241 {
242 get
243 {
244 return this.deadline;
245 }
246 }
247
248 public Metadata RequestMetadata
249 {
250 get
251 {
252 return this.requestMetadata;
253 }
254 }
255 }
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800256}