blob: bf020cd627454035db3a2f71e57dc8756b4a03ee [file] [log] [blame]
Jan Tattermuscha5272b62015-04-30 11:56:46 -07001#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
34using System;
35using System.Runtime.CompilerServices;
36using System.Threading.Tasks;
37
38namespace Grpc.Core
39{
40 /// <summary>
41 /// Return type for client streaming calls.
42 /// </summary>
Jan Tattermuschbdf2e622015-05-18 15:59:02 -070043 public sealed class AsyncClientStreamingCall<TRequest, TResponse> : IDisposable
Jan Tattermuscha5272b62015-04-30 11:56:46 -070044 {
45 readonly IClientStreamWriter<TRequest> requestStream;
Jan Tattermuscha236ff22015-07-21 12:33:31 -070046 readonly Task<TResponse> responseAsync;
Jan Tattermusched4b7a72015-07-21 11:46:43 -070047 readonly Func<Status> getStatusFunc;
48 readonly Func<Metadata> getTrailersFunc;
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070049 readonly Action disposeAction;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070050
Jan Tattermuscha236ff22015-07-21 12:33:31 -070051 public AsyncClientStreamingCall(IClientStreamWriter<TRequest> requestStream, Task<TResponse> responseAsync, Func<Status> getStatusFunc, Func<Metadata> getTrailersFunc, Action disposeAction)
Jan Tattermuscha5272b62015-04-30 11:56:46 -070052 {
53 this.requestStream = requestStream;
Jan Tattermuscha236ff22015-07-21 12:33:31 -070054 this.responseAsync = responseAsync;
Jan Tattermusched4b7a72015-07-21 11:46:43 -070055 this.getStatusFunc = getStatusFunc;
56 this.getTrailersFunc = getTrailersFunc;
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070057 this.disposeAction = disposeAction;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070058 }
59
60 /// <summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070061 /// Asynchronous call result.
62 /// </summary>
Jan Tattermuscha236ff22015-07-21 12:33:31 -070063 public Task<TResponse> ResponseAsync
Jan Tattermuscha5272b62015-04-30 11:56:46 -070064 {
65 get
66 {
Jan Tattermuscha236ff22015-07-21 12:33:31 -070067 return this.responseAsync;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070068 }
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 Tattermuscha236ff22015-07-21 12:33:31 -070088 return responseAsync.GetAwaiter();
Jan Tattermuscha5272b62015-04-30 11:56:46 -070089 }
Jan Tattermuschbdf2e622015-05-18 15:59:02 -070090
91 /// <summary>
Jan Tattermusched4b7a72015-07-21 11:46:43 -070092 /// Provides means to cleanup after the call.
Jan Tattermuschbdf2e622015-05-18 15:59:02 -070093 /// 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 Tattermusch2d2652d2015-05-18 16:23:04 -070099 disposeAction.Invoke();
Jan Tattermuschbdf2e622015-05-18 15:59:02 -0700100 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700101 }
102}