Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [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.CompilerServices; |
| 36 | using System.Threading.Tasks; |
| 37 | |
| 38 | namespace Grpc.Core |
| 39 | { |
| 40 | /// <summary> |
| 41 | /// Return type for client streaming calls. |
| 42 | /// </summary> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 43 | public sealed class AsyncClientStreamingCall<TRequest, TResponse> : IDisposable |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 44 | { |
| 45 | readonly IClientStreamWriter<TRequest> requestStream; |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 46 | readonly Task<TResponse> responseAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 47 | readonly Func<Status> getStatusFunc; |
| 48 | readonly Func<Metadata> getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 49 | readonly Action disposeAction; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 50 | |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 51 | public AsyncClientStreamingCall(IClientStreamWriter<TRequest> requestStream, Task<TResponse> responseAsync, Func<Status> getStatusFunc, Func<Metadata> getTrailersFunc, Action disposeAction) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 52 | { |
| 53 | this.requestStream = requestStream; |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 54 | this.responseAsync = responseAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 55 | this.getStatusFunc = getStatusFunc; |
| 56 | this.getTrailersFunc = getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 57 | this.disposeAction = disposeAction; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 61 | /// Asynchronous call result. |
| 62 | /// </summary> |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 63 | public Task<TResponse> ResponseAsync |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 64 | { |
| 65 | get |
| 66 | { |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 67 | return this.responseAsync; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Async stream to send streaming requests. |
| 73 | /// </summary> |
| 74 | public IClientStreamWriter<TRequest> RequestStream |
| 75 | { |
| 76 | get |
| 77 | { |
| 78 | return requestStream; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
| 83 | /// Allows awaiting this object directly. |
| 84 | /// </summary> |
| 85 | /// <returns></returns> |
| 86 | public TaskAwaiter<TResponse> GetAwaiter() |
| 87 | { |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 88 | return responseAsync.GetAwaiter(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 89 | } |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 90 | |
| 91 | /// <summary> |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 92 | /// Provides means to cleanup after the call. |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 93 | /// If the call has already finished normally (request stream has been completed and call result has been received), doesn't do anything. |
| 94 | /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call. |
| 95 | /// As a result, all resources being used by the call should be released eventually. |
| 96 | /// </summary> |
| 97 | public void Dispose() |
| 98 | { |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 99 | disposeAction.Invoke(); |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 100 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 101 | } |
| 102 | } |