OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 #endregion |
| 32 using System; |
| 33 using System.Diagnostics; |
| 34 using System.Runtime.InteropServices; |
| 35 using Grpc.Core; |
| 36 using Grpc.Core.Utils; |
| 37 using Grpc.Core.Profiling; |
| 38 |
| 39 namespace Grpc.Core.Internal |
| 40 { |
| 41 /// <summary> |
| 42 /// grpc_call from <c>grpc/grpc.h</c> |
| 43 /// </summary> |
| 44 internal class CallSafeHandle : SafeHandleZeroIsInvalid, INativeCall |
| 45 { |
| 46 public static readonly CallSafeHandle NullInstance = new CallSafeHandle(
); |
| 47 static readonly NativeMethods Native = NativeMethods.Get(); |
| 48 |
| 49 const uint GRPC_WRITE_BUFFER_HINT = 1; |
| 50 CompletionRegistry completionRegistry; |
| 51 CompletionQueueSafeHandle completionQueue; |
| 52 |
| 53 private CallSafeHandle() |
| 54 { |
| 55 } |
| 56 |
| 57 public void Initialize(CompletionRegistry completionRegistry, Completion
QueueSafeHandle completionQueue) |
| 58 { |
| 59 this.completionRegistry = completionRegistry; |
| 60 this.completionQueue = completionQueue; |
| 61 } |
| 62 |
| 63 public void SetCredentials(CallCredentialsSafeHandle credentials) |
| 64 { |
| 65 Native.grpcsharp_call_set_credentials(this, credentials).CheckOk(); |
| 66 } |
| 67 |
| 68 public void StartUnary(UnaryResponseClientHandler callback, byte[] paylo
ad, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
| 69 { |
| 70 using (completionQueue.NewScope()) |
| 71 { |
| 72 var ctx = BatchContextSafeHandle.Create(); |
| 73 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedStatusOnClient(), context.GetReceived
Message(), context.GetReceivedInitialMetadata())); |
| 74 Native.grpcsharp_call_start_unary(this, ctx, payload, new UIntPt
r((ulong)payload.Length), metadataArray, writeFlags) |
| 75 .CheckOk(); |
| 76 } |
| 77 } |
| 78 |
| 79 public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, Metad
ataArraySafeHandle metadataArray, WriteFlags writeFlags) |
| 80 { |
| 81 using (Profilers.ForCurrentThread().NewScope("CallSafeHandle.StartUn
ary")) |
| 82 { |
| 83 Native.grpcsharp_call_start_unary(this, ctx, payload, new UIntPt
r((ulong)payload.Length), metadataArray, writeFlags) |
| 84 .CheckOk(); |
| 85 } |
| 86 } |
| 87 |
| 88 public void StartClientStreaming(UnaryResponseClientHandler callback, Me
tadataArraySafeHandle metadataArray) |
| 89 { |
| 90 using (completionQueue.NewScope()) |
| 91 { |
| 92 var ctx = BatchContextSafeHandle.Create(); |
| 93 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedStatusOnClient(), context.GetReceived
Message(), context.GetReceivedInitialMetadata())); |
| 94 Native.grpcsharp_call_start_client_streaming(this, ctx, metadata
Array).CheckOk(); |
| 95 } |
| 96 } |
| 97 |
| 98 public void StartServerStreaming(ReceivedStatusOnClientHandler callback,
byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
| 99 { |
| 100 using (completionQueue.NewScope()) |
| 101 { |
| 102 var ctx = BatchContextSafeHandle.Create(); |
| 103 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedStatusOnClient())); |
| 104 Native.grpcsharp_call_start_server_streaming(this, ctx, payload,
new UIntPtr((ulong)payload.Length), metadataArray, writeFlags).CheckOk(); |
| 105 } |
| 106 } |
| 107 |
| 108 public void StartDuplexStreaming(ReceivedStatusOnClientHandler callback,
MetadataArraySafeHandle metadataArray) |
| 109 { |
| 110 using (completionQueue.NewScope()) |
| 111 { |
| 112 var ctx = BatchContextSafeHandle.Create(); |
| 113 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedStatusOnClient())); |
| 114 Native.grpcsharp_call_start_duplex_streaming(this, ctx, metadata
Array).CheckOk(); |
| 115 } |
| 116 } |
| 117 |
| 118 public void StartSendMessage(SendCompletionHandler callback, byte[] payl
oad, WriteFlags writeFlags, bool sendEmptyInitialMetadata) |
| 119 { |
| 120 using (completionQueue.NewScope()) |
| 121 { |
| 122 var ctx = BatchContextSafeHandle.Create(); |
| 123 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success)); |
| 124 Native.grpcsharp_call_send_message(this, ctx, payload, new UIntP
tr((ulong)payload.Length), writeFlags, sendEmptyInitialMetadata).CheckOk(); |
| 125 } |
| 126 } |
| 127 |
| 128 public void StartSendCloseFromClient(SendCompletionHandler callback) |
| 129 { |
| 130 using (completionQueue.NewScope()) |
| 131 { |
| 132 var ctx = BatchContextSafeHandle.Create(); |
| 133 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success)); |
| 134 Native.grpcsharp_call_send_close_from_client(this, ctx).CheckOk(
); |
| 135 } |
| 136 } |
| 137 |
| 138 public void StartSendStatusFromServer(SendCompletionHandler callback, St
atus status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadat
a) |
| 139 { |
| 140 using (completionQueue.NewScope()) |
| 141 { |
| 142 var ctx = BatchContextSafeHandle.Create(); |
| 143 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success)); |
| 144 Native.grpcsharp_call_send_status_from_server(this, ctx, status.
StatusCode, status.Detail, metadataArray, sendEmptyInitialMetadata).CheckOk(); |
| 145 } |
| 146 } |
| 147 |
| 148 public void StartReceiveMessage(ReceivedMessageHandler callback) |
| 149 { |
| 150 using (completionQueue.NewScope()) |
| 151 { |
| 152 var ctx = BatchContextSafeHandle.Create(); |
| 153 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedMessage())); |
| 154 Native.grpcsharp_call_recv_message(this, ctx).CheckOk(); |
| 155 } |
| 156 } |
| 157 |
| 158 public void StartReceiveInitialMetadata(ReceivedResponseHeadersHandler c
allback) |
| 159 { |
| 160 using (completionQueue.NewScope()) |
| 161 { |
| 162 var ctx = BatchContextSafeHandle.Create(); |
| 163 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedInitialMetadata())); |
| 164 Native.grpcsharp_call_recv_initial_metadata(this, ctx).CheckOk()
; |
| 165 } |
| 166 } |
| 167 |
| 168 public void StartServerSide(ReceivedCloseOnServerHandler callback) |
| 169 { |
| 170 using (completionQueue.NewScope()) |
| 171 { |
| 172 var ctx = BatchContextSafeHandle.Create(); |
| 173 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success, context.GetReceivedCloseOnServerCancelled())); |
| 174 Native.grpcsharp_call_start_serverside(this, ctx).CheckOk(); |
| 175 } |
| 176 } |
| 177 |
| 178 public void StartSendInitialMetadata(SendCompletionHandler callback, Met
adataArraySafeHandle metadataArray) |
| 179 { |
| 180 using (completionQueue.NewScope()) |
| 181 { |
| 182 var ctx = BatchContextSafeHandle.Create(); |
| 183 completionRegistry.RegisterBatchCompletion(ctx, (success, contex
t) => callback(success)); |
| 184 Native.grpcsharp_call_send_initial_metadata(this, ctx, metadataA
rray).CheckOk(); |
| 185 } |
| 186 } |
| 187 |
| 188 public void Cancel() |
| 189 { |
| 190 Native.grpcsharp_call_cancel(this).CheckOk(); |
| 191 } |
| 192 |
| 193 public void CancelWithStatus(Status status) |
| 194 { |
| 195 Native.grpcsharp_call_cancel_with_status(this, status.StatusCode, st
atus.Detail).CheckOk(); |
| 196 } |
| 197 |
| 198 public string GetPeer() |
| 199 { |
| 200 using (var cstring = Native.grpcsharp_call_get_peer(this)) |
| 201 { |
| 202 return cstring.GetValue(); |
| 203 } |
| 204 } |
| 205 |
| 206 protected override bool ReleaseHandle() |
| 207 { |
| 208 Native.grpcsharp_call_destroy(handle); |
| 209 return true; |
| 210 } |
| 211 |
| 212 private static uint GetFlags(bool buffered) |
| 213 { |
| 214 return buffered ? 0 : GRPC_WRITE_BUFFER_HINT; |
| 215 } |
| 216 } |
| 217 } |
OLD | NEW |