1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // 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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // 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.
16 
17 #endregion
18 
19 using System;
20 using System.Collections.Generic;
21 using System.Threading;
22 using System.Threading.Tasks;
23 
24 namespace Grpc.Core.Internal
25 {
26     internal class ServerRequestStream<TRequest, TResponse> : IAsyncStreamReader<TRequest>
27         where TRequest : class
28         where TResponse : class
29     {
30         readonly AsyncCallServer<TRequest, TResponse> call;
31         TRequest current;
32 
ServerRequestStream(AsyncCallServer<TRequest, TResponse> call)33         public ServerRequestStream(AsyncCallServer<TRequest, TResponse> call)
34         {
35             this.call = call;
36         }
37 
38         public TRequest Current
39         {
40             get
41             {
42                 if (current == null)
43                 {
44                     throw new InvalidOperationException("No current element is available.");
45                 }
46                 return current;
47             }
48         }
49 
MoveNext(CancellationToken token)50         public async Task<bool> MoveNext(CancellationToken token)
51         {
52 
53             var cancellationTokenRegistration = token.CanBeCanceled ? token.Register(() => call.Cancel()) : (IDisposable) null;
54             using (cancellationTokenRegistration)
55             {
56                 var result = await call.ReadMessageAsync().ConfigureAwait(false);
57                 this.current = result;
58                 return result != null;
59             }
60         }
61 
Dispose()62         public void Dispose()
63         {
64             // TODO(jtattermusch): implement the semantics of stream disposal.
65         }
66     }
67 }
68