• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 #endregion
16 
17 using System;
18 using System.Threading.Tasks;
19 using Grpc.Core.Internal;
20 
21 namespace Grpc.Core.Internal
22 {
23     /// <summary>
24     /// Writes responses asynchronously to an underlying AsyncCallServer object.
25     /// </summary>
26     internal class ServerResponseStream<TRequest, TResponse> : IServerStreamWriter<TResponse>, IHasWriteOptions
27         where TRequest : class
28         where TResponse : class
29     {
30         readonly AsyncCallServer<TRequest, TResponse> call;
31         WriteOptions writeOptions;
32 
ServerResponseStream(AsyncCallServer<TRequest, TResponse> call)33         public ServerResponseStream(AsyncCallServer<TRequest, TResponse> call)
34         {
35             this.call = call;
36         }
37 
WriteAsync(TResponse message)38         public Task WriteAsync(TResponse message)
39         {
40             return call.SendMessageAsync(message, GetWriteFlags());
41         }
42 
WriteResponseHeadersAsync(Metadata responseHeaders)43         public Task WriteResponseHeadersAsync(Metadata responseHeaders)
44         {
45             return call.SendInitialMetadataAsync(responseHeaders);
46         }
47 
48         public WriteOptions WriteOptions
49         {
50             get
51             {
52                 return writeOptions;
53             }
54 
55             set
56             {
57                 writeOptions = value;
58             }
59         }
60 
GetWriteFlags()61         private WriteFlags GetWriteFlags()
62         {
63             var options = writeOptions;
64             return options != null ? options.Flags : default(WriteFlags);
65         }
66     }
67 }
68