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 ClientResponseStream<TRequest, TResponse> : IAsyncStreamReader<TResponse> 27 where TRequest : class 28 where TResponse : class 29 { 30 readonly AsyncCall<TRequest, TResponse> call; 31 TResponse current; 32 ClientResponseStream(AsyncCall<TRequest, TResponse> call)33 public ClientResponseStream(AsyncCall<TRequest, TResponse> call) 34 { 35 this.call = call; 36 } 37 38 public TResponse 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 var cancellationTokenRegistration = token.CanBeCanceled ? token.Register(() => call.Cancel()) : (IDisposable) null; 53 using (cancellationTokenRegistration) 54 { 55 var result = await call.ReadMessageAsync().ConfigureAwait(false); 56 this.current = result; 57 58 if (result == null) 59 { 60 await call.StreamingResponseCallFinishedTask.ConfigureAwait(false); 61 return false; 62 } 63 return true; 64 } 65 } 66 Dispose()67 public void Dispose() 68 { 69 // TODO(jtattermusch): implement the semantics of stream disposal. 70 } 71 } 72 } 73