blob: 4303b0b1b02dc10013efaa28c029167d1f8204e6 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermuscha7608b02015-02-03 17:54:38 -080019using System;
Jan Tattermusch4c25efa2015-08-21 16:07:57 -070020using System.Threading.Tasks;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080021
Jan Tattermusch30868622015-02-19 09:22:33 -080022namespace Grpc.Core
Jan Tattermuscha7608b02015-02-03 17:54:38 -080023{
24 /// <summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070025 /// Return type for server streaming calls.
Jan Tattermuscha7608b02015-02-03 17:54:38 -080026 /// </summary>
Jan Tattermusch12855fc2015-08-24 16:43:23 -070027 /// <typeparam name="TResponse">Response message type for this call.</typeparam>
Jan Tattermuschbdf2e622015-05-18 15:59:02 -070028 public sealed class AsyncServerStreamingCall<TResponse> : IDisposable
Jan Tattermuscha7608b02015-02-03 17:54:38 -080029 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070030 readonly IAsyncStreamReader<TResponse> responseStream;
Jan Tattermusch4c25efa2015-08-21 16:07:57 -070031 readonly Task<Metadata> responseHeadersAsync;
Jan Tattermusched4b7a72015-07-21 11:46:43 -070032 readonly Func<Status> getStatusFunc;
33 readonly Func<Metadata> getTrailersFunc;
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070034 readonly Action disposeAction;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080035
Mehrdad Afshari4d7f4082017-10-06 14:51:11 -070036 /// <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 Tattermuscha7608b02015-02-03 17:54:38 -080049 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070050 this.responseStream = responseStream;
Jan Tattermusch4c25efa2015-08-21 16:07:57 -070051 this.responseHeadersAsync = responseHeadersAsync;
Jan Tattermusched4b7a72015-07-21 11:46:43 -070052 this.getStatusFunc = getStatusFunc;
53 this.getTrailersFunc = getTrailersFunc;
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070054 this.disposeAction = disposeAction;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080055 }
56
Jan Tattermuscha5272b62015-04-30 11:56:46 -070057 /// <summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070058 /// Async stream to read streaming responses.
59 /// </summary>
60 public IAsyncStreamReader<TResponse> ResponseStream
Jan Tattermuscha7608b02015-02-03 17:54:38 -080061 {
62 get
63 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070064 return responseStream;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080065 }
66 }
Jan Tattermuschbdf2e622015-05-18 15:59:02 -070067
68 /// <summary>
Jan Tattermusch4c25efa2015-08-21 16:07:57 -070069 /// 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 Tattermusched4b7a72015-07-21 11:46:43 -070080 /// 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 Tattermuschbdf2e622015-05-18 15:59:02 -070098 /// 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 Tattermusch6acc07d2016-06-03 17:03:30 -0700103 /// <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 Tattermuschbdf2e622015-05-18 15:59:02 -0700107 public void Dispose()
108 {
Jan Tattermusch2d2652d2015-05-18 16:23:04 -0700109 disposeAction.Invoke();
Jan Tattermuschbdf2e622015-05-18 15:59:02 -0700110 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800111 }
112}