Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 1 | #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 | |
| 34 | using System; |
| 35 | using System.Runtime.InteropServices; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using Grpc.Core; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 37 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 38 | namespace Grpc.Core.Internal |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 39 | { |
| 40 | /// <summary> |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 41 | /// grpcsharp_batch_context |
| 42 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 43 | internal class BatchContextSafeHandle : SafeHandleZeroIsInvalid |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 44 | { |
| 45 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 46 | static extern BatchContextSafeHandle grpcsharp_batch_context_create(); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 47 | |
| 48 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 49 | static extern IntPtr grpcsharp_batch_context_recv_initial_metadata(BatchContextSafeHandle ctx); |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 50 | |
| 51 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 52 | static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 53 | |
| 54 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 55 | static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 56 | |
| 57 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 58 | static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandle ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 59 | |
| 60 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 61 | static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx); // returns const char* |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 62 | |
| 63 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 64 | static extern IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata(BatchContextSafeHandle ctx); |
| 65 | |
| 66 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 67 | static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandle ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 68 | |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 69 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 70 | static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char* |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 71 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 72 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 73 | 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 Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 79 | static extern IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata(BatchContextSafeHandle ctx); |
| 80 | |
| 81 | [DllImport("grpc_csharp_ext.dll")] |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 82 | 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 Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 88 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 89 | } |
| 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 Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 104 | // Gets data of recv_initial_metadata completion. |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 105 | public Metadata GetReceivedInitialMetadata() |
| 106 | { |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 107 | IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_initial_metadata(this); |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 108 | return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); |
| 109 | } |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 110 | |
| 111 | // Gets data of recv_status_on_client completion. |
| 112 | public ClientSideStatus GetReceivedStatusOnClient() |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 113 | { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 114 | string details = Marshal.PtrToStringAnsi(grpcsharp_batch_context_recv_status_on_client_details(this)); |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 115 | var status = new Status(grpcsharp_batch_context_recv_status_on_client_status(this), details); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 116 | |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 117 | IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this); |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 118 | var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); |
| 119 | |
| 120 | return new ClientSideStatus(status, metadata); |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 123 | // Gets data of recv_message completion. |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 124 | 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 Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 131 | byte[] data = new byte[(int)len]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 132 | grpcsharp_batch_context_recv_message_to_buffer(this, data, new UIntPtr((ulong)data.Length)); |
| 133 | return data; |
| 134 | } |
| 135 | |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 136 | // Gets data of server_rpc_new completion. |
| 137 | public ServerRpcNew GetServerRpcNew() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 138 | { |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 139 | var call = grpcsharp_batch_context_server_rpc_new_call(this); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 140 | |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 141 | 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 Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 144 | |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 145 | IntPtr metadataArrayPtr = grpcsharp_batch_context_server_rpc_new_request_metadata(this); |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 146 | var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); |
| 147 | |
| 148 | return new ServerRpcNew(call, method, host, deadline, metadata); |
Jan Tattermusch | ec50f28 | 2015-07-20 19:44:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 151 | // Gets data of receive_close_on_server completion. |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 152 | public bool GetReceivedCloseOnServerCancelled() |
| 153 | { |
| 154 | return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0; |
| 155 | } |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 156 | |
| 157 | protected override bool ReleaseHandle() |
| 158 | { |
| 159 | grpcsharp_batch_context_destroy(handle); |
| 160 | return true; |
| 161 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 162 | } |
Jan Tattermusch | 77415b6 | 2015-07-20 20:18:26 -0700 | [diff] [blame] | 163 | |
| 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 Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 256 | } |