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 using System;
17 using System.Threading.Tasks;
18 using Grpc.Core.Internal;
19 
20 namespace Grpc.Core.Internal
21 {
22     /// <summary>
23     /// Writes requests asynchronously to an underlying AsyncCall object.
24     /// </summary>
25     internal class ClientRequestStream<TRequest, TResponse> : IClientStreamWriter<TRequest>
26     {
27         readonly AsyncCall<TRequest, TResponse> call;
28         WriteOptions writeOptions;
29 
ClientRequestStream(AsyncCall<TRequest, TResponse> call)30         public ClientRequestStream(AsyncCall<TRequest, TResponse> call)
31         {
32             this.call = call;
33             this.writeOptions = call.Details.Options.WriteOptions;
34         }
35 
WriteAsync(TRequest message)36         public Task WriteAsync(TRequest message)
37         {
38             return call.SendMessageAsync(message, GetWriteFlags());
39         }
40 
CompleteAsync()41         public Task CompleteAsync()
42         {
43             return call.SendCloseFromClientAsync();
44         }
45 
46         public WriteOptions WriteOptions
47         {
48             get
49             {
50                 return this.writeOptions;
51             }
52 
53             set
54             {
55                 writeOptions = value;
56             }
57         }
58 
GetWriteFlags()59         private WriteFlags GetWriteFlags()
60         {
61             var options = writeOptions;
62             return options != null ? options.Flags : default(WriteFlags);
63         }
64     }
65 }
66