Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Runtime.CompilerServices; |
| 21 | using System.Threading.Tasks; |
| 22 | |
| 23 | namespace Grpc.Core |
| 24 | { |
| 25 | /// <summary> |
| 26 | /// Return type for client streaming calls. |
| 27 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 28 | /// <typeparam name="TRequest">Request message type for this call.</typeparam> |
| 29 | /// <typeparam name="TResponse">Response message type for this call.</typeparam> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 30 | public sealed class AsyncClientStreamingCall<TRequest, TResponse> : IDisposable |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 31 | { |
| 32 | readonly IClientStreamWriter<TRequest> requestStream; |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 33 | readonly Task<TResponse> responseAsync; |
Jan Tattermusch | fb34a99 | 2015-08-21 10:45:39 -0700 | [diff] [blame] | 34 | readonly Task<Metadata> responseHeadersAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 35 | readonly Func<Status> getStatusFunc; |
| 36 | readonly Func<Metadata> getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 37 | readonly Action disposeAction; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 38 | |
Mehrdad Afshari | 4d7f408 | 2017-10-06 14:51:11 -0700 | [diff] [blame] | 39 | /// <summary> |
| 40 | /// Creates a new AsyncClientStreamingCall object with the specified properties. |
| 41 | /// </summary> |
| 42 | /// <param name="requestStream">Stream of request values.</param> |
| 43 | /// <param name="responseAsync">The response of the asynchronous call.</param> |
| 44 | /// <param name="responseHeadersAsync">Response headers of the asynchronous call.</param> |
| 45 | /// <param name="getStatusFunc">Delegate returning the status of the call.</param> |
| 46 | /// <param name="getTrailersFunc">Delegate returning the trailing metadata of the call.</param> |
| 47 | /// <param name="disposeAction">Delegate to invoke when Dispose is called on the call object.</param> |
| 48 | public AsyncClientStreamingCall(IClientStreamWriter<TRequest> requestStream, |
| 49 | Task<TResponse> responseAsync, |
| 50 | Task<Metadata> responseHeadersAsync, |
| 51 | Func<Status> getStatusFunc, |
| 52 | Func<Metadata> getTrailersFunc, |
| 53 | Action disposeAction) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 54 | { |
| 55 | this.requestStream = requestStream; |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 56 | this.responseAsync = responseAsync; |
Jan Tattermusch | fb34a99 | 2015-08-21 10:45:39 -0700 | [diff] [blame] | 57 | this.responseHeadersAsync = responseHeadersAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 58 | this.getStatusFunc = getStatusFunc; |
| 59 | this.getTrailersFunc = getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 60 | this.disposeAction = disposeAction; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 64 | /// Asynchronous call result. |
| 65 | /// </summary> |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 66 | public Task<TResponse> ResponseAsync |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 67 | { |
| 68 | get |
| 69 | { |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 70 | return this.responseAsync; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /// <summary> |
Jan Tattermusch | fb34a99 | 2015-08-21 10:45:39 -0700 | [diff] [blame] | 75 | /// Asynchronous access to response headers. |
| 76 | /// </summary> |
| 77 | public Task<Metadata> ResponseHeadersAsync |
| 78 | { |
| 79 | get |
| 80 | { |
| 81 | return this.responseHeadersAsync; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 86 | /// Async stream to send streaming requests. |
| 87 | /// </summary> |
| 88 | public IClientStreamWriter<TRequest> RequestStream |
| 89 | { |
| 90 | get |
| 91 | { |
| 92 | return requestStream; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// <summary> |
| 97 | /// Allows awaiting this object directly. |
| 98 | /// </summary> |
| 99 | /// <returns></returns> |
| 100 | public TaskAwaiter<TResponse> GetAwaiter() |
| 101 | { |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 102 | return responseAsync.GetAwaiter(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 103 | } |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 104 | |
| 105 | /// <summary> |
Jan Tattermusch | 2250454 | 2015-08-17 13:43:12 -0700 | [diff] [blame] | 106 | /// Gets the call status if the call has already finished. |
| 107 | /// Throws InvalidOperationException otherwise. |
| 108 | /// </summary> |
| 109 | public Status GetStatus() |
| 110 | { |
| 111 | return getStatusFunc(); |
| 112 | } |
| 113 | |
| 114 | /// <summary> |
| 115 | /// Gets the call trailing metadata if the call has already finished. |
| 116 | /// Throws InvalidOperationException otherwise. |
| 117 | /// </summary> |
| 118 | public Metadata GetTrailers() |
| 119 | { |
| 120 | return getTrailersFunc(); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 124 | /// Provides means to cleanup after the call. |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 125 | /// If the call has already finished normally (request stream has been completed and call result has been received), doesn't do anything. |
| 126 | /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call. |
| 127 | /// As a result, all resources being used by the call should be released eventually. |
| 128 | /// </summary> |
Jan Tattermusch | 781720f | 2016-06-03 17:40:28 -0700 | [diff] [blame] | 129 | /// <remarks> |
| 130 | /// Normally, there is no need for you to dispose the call unless you want to utilize the |
| 131 | /// "Cancel" semantics of invoking <c>Dispose</c>. |
| 132 | /// </remarks> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 133 | public void Dispose() |
| 134 | { |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 135 | disposeAction.Invoke(); |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 136 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 137 | } |
| 138 | } |