blob: a2d77dd5b7b625d9f78eda36aae0327fff941b09 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08002// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08003// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080018//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080030#endregion
31
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032using System;
33using System.Threading.Tasks;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070034using Grpc.Core.Internal;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080035
Jan Tattermuscha5272b62015-04-30 11:56:46 -070036namespace Grpc.Core.Internal
Jan Tattermuscha7608b02015-02-03 17:54:38 -080037{
38 /// <summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070039 /// Writes responses asynchronously to an underlying AsyncCallServer object.
Jan Tattermuscha7608b02015-02-03 17:54:38 -080040 /// </summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070041 internal class ServerResponseStream<TRequest, TResponse> : IServerStreamWriter<TResponse>
Jan Tattermuschbdb1b482015-05-06 14:46:25 -070042 where TRequest : class
43 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -080044 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070045 readonly AsyncCallServer<TRequest, TResponse> call;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080046
Jan Tattermuscha5272b62015-04-30 11:56:46 -070047 public ServerResponseStream(AsyncCallServer<TRequest, TResponse> call)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080048 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070049 this.call = call;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080050 }
51
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070052 public Task WriteAsync(TResponse message)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080053 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070054 var taskSource = new AsyncCompletionTaskSource<object>();
55 call.StartSendMessage(message, taskSource.CompletionDelegate);
56 return taskSource.Task;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080057 }
58
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070059 public Task WriteStatusAsync(Status status)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080060 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070061 var taskSource = new AsyncCompletionTaskSource<object>();
62 call.StartSendStatusFromServer(status, taskSource.CompletionDelegate);
63 return taskSource.Task;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080064 }
65 }
66}