Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -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.Diagnostics; |
| 36 | using System.Runtime.CompilerServices; |
| 37 | using System.Runtime.InteropServices; |
| 38 | using System.Threading; |
| 39 | using System.Threading.Tasks; |
| 40 | using Grpc.Core.Internal; |
| 41 | using Grpc.Core.Utils; |
| 42 | |
| 43 | namespace Grpc.Core.Internal |
| 44 | { |
| 45 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 46 | /// Manages server side native call lifecycle. |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 47 | /// </summary> |
| 48 | internal class AsyncCallServer<TRequest, TResponse> : AsyncCallBase<TResponse, TRequest> |
| 49 | { |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 50 | readonly TaskCompletionSource<object> finishedServersideTcs = new TaskCompletionSource<object>(); |
Jan Tattermusch | 0846b68 | 2015-07-23 17:02:12 -0700 | [diff] [blame] | 51 | readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 52 | readonly GrpcEnvironment environment; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 53 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 54 | public AsyncCallServer(Func<TResponse, byte[]> serializer, Func<byte[], TRequest> deserializer, GrpcEnvironment environment) : base(serializer, deserializer) |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 55 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 56 | this.environment = Preconditions.CheckNotNull(environment); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public void Initialize(CallSafeHandle call) |
| 60 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 61 | call.SetCompletionRegistry(environment.CompletionRegistry); |
| 62 | environment.DebugStats.ActiveServerCalls.Increment(); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 63 | InitializeInternal(call); |
| 64 | } |
| 65 | |
| 66 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 67 | /// Starts a server side call. |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 68 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 69 | public Task ServerSideCallAsync() |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 70 | { |
| 71 | lock (myLock) |
| 72 | { |
| 73 | Preconditions.CheckNotNull(call); |
| 74 | |
| 75 | started = true; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 76 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 77 | call.StartServerSide(HandleFinishedServerside); |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 78 | return finishedServersideTcs.Task; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
| 83 | /// Sends a streaming response. Only one pending send action is allowed at any given time. |
| 84 | /// completionDelegate is called when the operation finishes. |
| 85 | /// </summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 86 | public void StartSendMessage(TResponse msg, AsyncCompletionDelegate<object> completionDelegate) |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 87 | { |
| 88 | StartSendMessageInternal(msg, completionDelegate); |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 92 | /// Receives a streaming request. Only one pending read action is allowed at any given time. |
| 93 | /// completionDelegate is called when the operation finishes. |
| 94 | /// </summary> |
| 95 | public void StartReadMessage(AsyncCompletionDelegate<TRequest> completionDelegate) |
| 96 | { |
| 97 | StartReadMessageInternal(completionDelegate); |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 101 | /// Sends call result status, also indicating server is done with streaming responses. |
| 102 | /// Only one pending send action is allowed at any given time. |
| 103 | /// completionDelegate is called when the operation finishes. |
| 104 | /// </summary> |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 105 | public void StartSendStatusFromServer(Status status, Metadata trailers, AsyncCompletionDelegate<object> completionDelegate) |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 106 | { |
| 107 | lock (myLock) |
| 108 | { |
| 109 | Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); |
| 110 | CheckSendingAllowed(); |
| 111 | |
Jan Tattermusch | a0bb065 | 2015-07-20 22:34:19 -0700 | [diff] [blame] | 112 | using (var metadataArray = MetadataArraySafeHandle.Create(trailers)) |
| 113 | { |
| 114 | call.StartSendStatusFromServer(status, HandleHalfclosed, metadataArray); |
| 115 | } |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 116 | halfcloseRequested = true; |
Jan Tattermusch | 4ec975d | 2015-06-11 17:24:15 -0700 | [diff] [blame] | 117 | readingDone = true; |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 118 | sendCompletionDelegate = completionDelegate; |
| 119 | } |
| 120 | } |
| 121 | |
Jan Tattermusch | 0846b68 | 2015-07-23 17:02:12 -0700 | [diff] [blame] | 122 | /// <summary> |
| 123 | /// Gets cancellation token that gets cancelled once close completion |
| 124 | /// is received and the cancelled flag is set. |
| 125 | /// </summary> |
| 126 | public CancellationToken CancellationToken |
| 127 | { |
| 128 | get |
| 129 | { |
| 130 | return cancellationTokenSource.Token; |
| 131 | } |
| 132 | } |
| 133 | |
Jan Tattermusch | 062c329 | 2015-07-23 20:28:42 -0700 | [diff] [blame] | 134 | public string Peer |
| 135 | { |
| 136 | get |
| 137 | { |
| 138 | return call.GetPeer(); |
| 139 | } |
| 140 | } |
| 141 | |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 142 | protected override void OnReleaseResources() |
| 143 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 144 | environment.DebugStats.ActiveServerCalls.Decrement(); |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 147 | /// <summary> |
| 148 | /// Handles the server side close completion. |
| 149 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 150 | private void HandleFinishedServerside(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 151 | { |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 152 | bool cancelled = ctx.GetReceivedCloseOnServerCancelled(); |
| 153 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 154 | lock (myLock) |
| 155 | { |
| 156 | finished = true; |
| 157 | |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 158 | if (cancelled) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 159 | { |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 160 | // Once we cancel, we don't have to care that much |
| 161 | // about reads and writes. |
Jan Tattermusch | 0846b68 | 2015-07-23 17:02:12 -0700 | [diff] [blame] | 162 | |
| 163 | // TODO(jtattermusch): is this still necessary? |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 164 | Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 167 | ReleaseResourcesIfPossible(); |
| 168 | } |
Jan Tattermusch | 9b9a877 | 2015-05-06 14:43:51 -0700 | [diff] [blame] | 169 | // TODO(jtattermusch): handle error |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 170 | |
Jan Tattermusch | 0846b68 | 2015-07-23 17:02:12 -0700 | [diff] [blame] | 171 | if (cancelled) |
| 172 | { |
| 173 | cancellationTokenSource.Cancel(); |
| 174 | } |
| 175 | |
Jan Tattermusch | a29d0f3 | 2015-03-04 17:54:56 -0800 | [diff] [blame] | 176 | finishedServersideTcs.SetResult(null); |
| 177 | } |
| 178 | } |
| 179 | } |