ServerRequestStream.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.Collections.Generic;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. namespace Grpc.Core.Internal
  21. {
  22. internal class ServerRequestStream<TRequest, TResponse> : IAsyncStreamReader<TRequest>
  23. where TRequest : class
  24. where TResponse : class
  25. {
  26. readonly AsyncCallServer<TRequest, TResponse> call;
  27. TRequest current;
  28. public ServerRequestStream(AsyncCallServer<TRequest, TResponse> call)
  29. {
  30. this.call = call;
  31. }
  32. public TRequest Current
  33. {
  34. get
  35. {
  36. if (current == null)
  37. {
  38. throw new InvalidOperationException("No current element is available.");
  39. }
  40. return current;
  41. }
  42. }
  43. public async Task<bool> MoveNext(CancellationToken token)
  44. {
  45. using (call.RegisterCancellationCallbackForToken(token))
  46. {
  47. var result = await call.ReadMessageAsync().ConfigureAwait(false);
  48. this.current = result;
  49. return result != null;
  50. }
  51. }
  52. public void Dispose()
  53. {
  54. // TODO(jtattermusch): implement the semantics of stream disposal.
  55. }
  56. }
  57. }