Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [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 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [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 | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 19 | using System; |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame] | 20 | using System.Threading.Tasks; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 21 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 22 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 23 | { |
| 24 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 25 | /// Return type for server streaming calls. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 26 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 27 | /// <typeparam name="TResponse">Response message type for this call.</typeparam> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 28 | public sealed class AsyncServerStreamingCall<TResponse> : IDisposable |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 29 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 30 | readonly IAsyncStreamReader<TResponse> responseStream; |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame] | 31 | readonly Task<Metadata> responseHeadersAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 32 | readonly Func<Status> getStatusFunc; |
| 33 | readonly Func<Metadata> getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 34 | readonly Action disposeAction; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | |
Mehrdad Afshari | 4d7f408 | 2017-10-06 14:51:11 -0700 | [diff] [blame] | 36 | /// <summary> |
| 37 | /// Creates a new AsyncDuplexStreamingCall object with the specified properties. |
| 38 | /// </summary> |
| 39 | /// <param name="responseStream">Stream of response values.</param> |
| 40 | /// <param name="responseHeadersAsync">Response headers of the asynchronous call.</param> |
| 41 | /// <param name="getStatusFunc">Delegate returning the status of the call.</param> |
| 42 | /// <param name="getTrailersFunc">Delegate returning the trailing metadata of the call.</param> |
| 43 | /// <param name="disposeAction">Delegate to invoke when Dispose is called on the call object.</param> |
| 44 | public AsyncServerStreamingCall(IAsyncStreamReader<TResponse> responseStream, |
| 45 | Task<Metadata> responseHeadersAsync, |
| 46 | Func<Status> getStatusFunc, |
| 47 | Func<Metadata> getTrailersFunc, |
| 48 | Action disposeAction) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 50 | this.responseStream = responseStream; |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame] | 51 | this.responseHeadersAsync = responseHeadersAsync; |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 52 | this.getStatusFunc = getStatusFunc; |
| 53 | this.getTrailersFunc = getTrailersFunc; |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 54 | this.disposeAction = disposeAction; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 57 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 58 | /// Async stream to read streaming responses. |
| 59 | /// </summary> |
| 60 | public IAsyncStreamReader<TResponse> ResponseStream |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | { |
| 62 | get |
| 63 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 64 | return responseStream; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 67 | |
| 68 | /// <summary> |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame] | 69 | /// Asynchronous access to response headers. |
| 70 | /// </summary> |
| 71 | public Task<Metadata> ResponseHeadersAsync |
| 72 | { |
| 73 | get |
| 74 | { |
| 75 | return this.responseHeadersAsync; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// <summary> |
Jan Tattermusch | ed4b7a7 | 2015-07-21 11:46:43 -0700 | [diff] [blame] | 80 | /// Gets the call status if the call has already finished. |
| 81 | /// Throws InvalidOperationException otherwise. |
| 82 | /// </summary> |
| 83 | public Status GetStatus() |
| 84 | { |
| 85 | return getStatusFunc(); |
| 86 | } |
| 87 | |
| 88 | /// <summary> |
| 89 | /// Gets the call trailing metadata if the call has already finished. |
| 90 | /// Throws InvalidOperationException otherwise. |
| 91 | /// </summary> |
| 92 | public Metadata GetTrailers() |
| 93 | { |
| 94 | return getTrailersFunc(); |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 98 | /// Provides means to cleanup after the call. |
| 99 | /// If the call has already finished normally (response stream has been fully read), doesn't do anything. |
| 100 | /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call. |
| 101 | /// As a result, all resources being used by the call should be released eventually. |
| 102 | /// </summary> |
Jan Tattermusch | 6acc07d | 2016-06-03 17:03:30 -0700 | [diff] [blame] | 103 | /// <remarks> |
| 104 | /// Normally, there is no need for you to dispose the call unless you want to utilize the |
| 105 | /// "Cancel" semantics of invoking <c>Dispose</c>. |
| 106 | /// </remarks> |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 107 | public void Dispose() |
| 108 | { |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 109 | disposeAction.Invoke(); |
Jan Tattermusch | bdf2e62 | 2015-05-18 15:59:02 -0700 | [diff] [blame] | 110 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 111 | } |
| 112 | } |