Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | using System.Diagnostics; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using System.Runtime.CompilerServices; |
| 37 | using System.Runtime.InteropServices; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | using System.Threading; |
| 39 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 40 | using Grpc.Core.Internal; |
Jan Tattermusch | 4d70326 | 2015-02-23 11:33:44 -0800 | [diff] [blame] | 41 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 43 | namespace Grpc.Core.Internal |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | { |
| 45 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 46 | /// Manages client side native call lifecycle. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 47 | /// </summary> |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 48 | internal class AsyncCall<TRequest, TResponse> : AsyncCallBase<TRequest, TResponse> |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 50 | Channel channel; |
| 51 | |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 52 | // Completion of a pending unary response if not null. |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 53 | TaskCompletionSource<TResponse> unaryResponseTcs; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 54 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 55 | // Set after status is received. Only used for streaming response calls. |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 56 | Status? finishedStatus; |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 57 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 58 | bool readObserverCompleted; // True if readObserver has already been completed. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 59 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 60 | public AsyncCall(Func<TRequest, byte[]> serializer, Func<byte[], TResponse> deserializer) : base(serializer, deserializer) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 64 | public void Initialize(Channel channel, CompletionQueueSafeHandle cq, string methodName) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 65 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 66 | this.channel = channel; |
| 67 | var call = CallSafeHandle.Create(channel.Handle, channel.CompletionRegistry, cq, methodName, channel.Target, Timespec.InfFuture); |
| 68 | channel.Environment.DebugStats.ActiveClientCalls.Increment(); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 69 | InitializeInternal(call); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 72 | // TODO: this method is not Async, so it shouldn't be in AsyncCall class, but |
| 73 | // it is reusing fair amount of code in this class, so we are leaving it here. |
| 74 | // TODO: for other calls, you need to call Initialize, this methods calls initialize |
| 75 | // on its own, so there's a usage inconsistency. |
| 76 | /// <summary> |
| 77 | /// Blocking unary request - unary response call. |
| 78 | /// </summary> |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 79 | public TResponse UnaryCall(Channel channel, string methodName, TRequest msg, Metadata headers) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 80 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 81 | using (CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Create()) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 82 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 83 | byte[] payload = UnsafeSerialize(msg); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 84 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 85 | unaryResponseTcs = new TaskCompletionSource<TResponse>(); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 86 | |
| 87 | lock (myLock) |
| 88 | { |
| 89 | Initialize(channel, cq, methodName); |
| 90 | started = true; |
| 91 | halfcloseRequested = true; |
| 92 | readingDone = true; |
| 93 | } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 94 | |
| 95 | using (var metadataArray = MetadataArraySafeHandle.Create(headers)) |
| 96 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 97 | using (var ctx = BatchContextSafeHandle.Create()) |
| 98 | { |
| 99 | call.StartUnary(payload, ctx, metadataArray); |
| 100 | var ev = cq.Pluck(ctx.Handle); |
| 101 | |
| 102 | bool success = (ev.success != 0); |
| 103 | try |
| 104 | { |
| 105 | HandleUnaryResponse(success, ctx); |
| 106 | } |
| 107 | catch (Exception e) |
| 108 | { |
| 109 | Console.WriteLine("Exception occured while invoking completion delegate: " + e); |
| 110 | } |
| 111 | } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 112 | } |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 113 | |
Jan Tattermusch | 4d70326 | 2015-02-23 11:33:44 -0800 | [diff] [blame] | 114 | try |
| 115 | { |
| 116 | // Once the blocking call returns, the result should be available synchronously. |
| 117 | return unaryResponseTcs.Task.Result; |
| 118 | } |
| 119 | catch (AggregateException ae) |
| 120 | { |
| 121 | throw ExceptionHelper.UnwrapRpcException(ae); |
| 122 | } |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 126 | /// <summary> |
| 127 | /// Starts a unary request - unary response call. |
| 128 | /// </summary> |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 129 | public Task<TResponse> UnaryCallAsync(TRequest msg, Metadata headers) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 130 | { |
| 131 | lock (myLock) |
| 132 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 133 | Preconditions.CheckNotNull(call); |
| 134 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 135 | started = true; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 136 | halfcloseRequested = true; |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 137 | readingDone = true; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 138 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 139 | byte[] payload = UnsafeSerialize(msg); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 140 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 141 | unaryResponseTcs = new TaskCompletionSource<TResponse>(); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 142 | using (var metadataArray = MetadataArraySafeHandle.Create(headers)) |
| 143 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 144 | call.StartUnary(payload, HandleUnaryResponse, metadataArray); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 145 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 146 | return unaryResponseTcs.Task; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 150 | /// <summary> |
| 151 | /// Starts a streamed request - unary response call. |
| 152 | /// Use StartSendMessage and StartSendCloseFromClient to stream requests. |
| 153 | /// </summary> |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 154 | public Task<TResponse> ClientStreamingCallAsync(Metadata headers) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 155 | { |
| 156 | lock (myLock) |
| 157 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 158 | Preconditions.CheckNotNull(call); |
| 159 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 160 | started = true; |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 161 | readingDone = true; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 162 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 163 | unaryResponseTcs = new TaskCompletionSource<TResponse>(); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 164 | using (var metadataArray = MetadataArraySafeHandle.Create(headers)) |
| 165 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 166 | call.StartClientStreaming(HandleUnaryResponse, metadataArray); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 167 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 168 | |
| 169 | return unaryResponseTcs.Task; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 173 | /// <summary> |
| 174 | /// Starts a unary request - streamed response call. |
| 175 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 176 | public void StartServerStreamingCall(TRequest msg, Metadata headers) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 177 | { |
| 178 | lock (myLock) |
| 179 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 180 | Preconditions.CheckNotNull(call); |
| 181 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 182 | started = true; |
| 183 | halfcloseRequested = true; |
Jan Tattermusch | 4788e78 | 2015-02-24 16:14:28 -0800 | [diff] [blame] | 184 | halfclosed = true; // halfclose not confirmed yet, but it will be once finishedHandler is called. |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 185 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 186 | byte[] payload = UnsafeSerialize(msg); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 187 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 188 | using (var metadataArray = MetadataArraySafeHandle.Create(headers)) |
| 189 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 190 | call.StartServerStreaming(payload, HandleFinished, metadataArray); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 191 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 195 | /// <summary> |
| 196 | /// Starts a streaming request - streaming response call. |
| 197 | /// Use StartSendMessage and StartSendCloseFromClient to stream requests. |
| 198 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 199 | public void StartDuplexStreamingCall(Metadata headers) |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 200 | { |
| 201 | lock (myLock) |
| 202 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 203 | Preconditions.CheckNotNull(call); |
| 204 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 205 | started = true; |
| 206 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 207 | using (var metadataArray = MetadataArraySafeHandle.Create(headers)) |
| 208 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 209 | call.StartDuplexStreaming(HandleFinished, metadataArray); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 210 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 214 | /// <summary> |
| 215 | /// Sends a streaming request. Only one pending send action is allowed at any given time. |
| 216 | /// completionDelegate is called when the operation finishes. |
| 217 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 218 | public void StartSendMessage(TRequest msg, AsyncCompletionDelegate<object> completionDelegate) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 219 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 220 | StartSendMessageInternal(msg, completionDelegate); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 221 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 222 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 223 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 224 | /// Receives a streaming response. Only one pending read action is allowed at any given time. |
| 225 | /// completionDelegate is called when the operation finishes. |
| 226 | /// </summary> |
| 227 | public void StartReadMessage(AsyncCompletionDelegate<TResponse> completionDelegate) |
| 228 | { |
| 229 | StartReadMessageInternal(completionDelegate); |
| 230 | } |
| 231 | |
| 232 | /// <summary> |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 233 | /// Sends halfclose, indicating client is done with streaming requests. |
| 234 | /// Only one pending send action is allowed at any given time. |
| 235 | /// completionDelegate is called when the operation finishes. |
| 236 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 237 | public void StartSendCloseFromClient(AsyncCompletionDelegate<object> completionDelegate) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 238 | { |
| 239 | lock (myLock) |
| 240 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 241 | Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); |
| 242 | CheckSendingAllowed(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 243 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 244 | call.StartSendCloseFromClient(HandleHalfclosed); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 245 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 246 | halfcloseRequested = true; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 247 | sendCompletionDelegate = completionDelegate; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 251 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 252 | /// On client-side, we only fire readCompletionDelegate once all messages have been read |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 253 | /// and status has been received. |
| 254 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 255 | protected override void ProcessLastRead(AsyncCompletionDelegate<TResponse> completionDelegate) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 256 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 257 | if (completionDelegate != null && readingDone && finishedStatus.HasValue) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 258 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 259 | bool shouldComplete; |
| 260 | lock (myLock) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 261 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 262 | shouldComplete = !readObserverCompleted; |
| 263 | readObserverCompleted = true; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 266 | if (shouldComplete) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 267 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 268 | var status = finishedStatus.Value; |
| 269 | if (status.StatusCode != StatusCode.OK) |
| 270 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 271 | FireCompletion(completionDelegate, default(TResponse), new RpcException(status)); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 272 | } |
| 273 | else |
| 274 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 275 | FireCompletion(completionDelegate, default(TResponse), null); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 276 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 277 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 281 | protected override void OnReleaseResources() |
| 282 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 283 | channel.Environment.DebugStats.ActiveClientCalls.Decrement(); |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 286 | /// <summary> |
| 287 | /// Handler for unary response completion. |
| 288 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 289 | private void HandleUnaryResponse(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 290 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 291 | lock (myLock) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 292 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 293 | finished = true; |
| 294 | halfclosed = true; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 295 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 296 | ReleaseResourcesIfPossible(); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 297 | } |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 298 | |
Jan Tattermusch | 1215c33 | 2015-05-04 19:39:59 -0700 | [diff] [blame] | 299 | if (!success) |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 300 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 301 | unaryResponseTcs.SetException(new RpcException(new Status(StatusCode.Internal, "Internal error occured."))); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
| 305 | var status = ctx.GetReceivedStatus(); |
| 306 | if (status.StatusCode != StatusCode.OK) |
| 307 | { |
| 308 | unaryResponseTcs.SetException(new RpcException(status)); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // TODO: handle deserialization error |
| 313 | TResponse msg; |
| 314 | TryDeserialize(ctx.GetReceivedMessage(), out msg); |
| 315 | |
| 316 | unaryResponseTcs.SetResult(msg); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 319 | /// <summary> |
| 320 | /// Handles receive status completion for calls with streaming response. |
| 321 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 322 | private void HandleFinished(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 323 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 324 | var status = ctx.GetReceivedStatus(); |
| 325 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 326 | AsyncCompletionDelegate<TResponse> origReadCompletionDelegate = null; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 327 | lock (myLock) |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 328 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 329 | finished = true; |
| 330 | finishedStatus = status; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 331 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 332 | origReadCompletionDelegate = readCompletionDelegate; |
| 333 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 334 | ReleaseResourcesIfPossible(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 335 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 336 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 337 | ProcessLastRead(origReadCompletionDelegate); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 338 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 339 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 340 | } |