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