TestServiceImpl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #region Copyright notice and license
  2. // Copyright 2015-2016 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.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Google.Protobuf;
  22. using Grpc.Core;
  23. using Grpc.Core.Utils;
  24. namespace Grpc.Testing
  25. {
  26. /// <summary>
  27. /// Implementation of TestService server
  28. /// </summary>
  29. public class TestServiceImpl : TestService.TestServiceBase
  30. {
  31. public override Task<Empty> EmptyCall(Empty request, ServerCallContext context)
  32. {
  33. return Task.FromResult(new Empty());
  34. }
  35. public override async Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context)
  36. {
  37. await EnsureEchoMetadataAsync(context);
  38. EnsureEchoStatus(request.ResponseStatus, context);
  39. var response = new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) };
  40. return response;
  41. }
  42. public override async Task StreamingOutputCall(StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context)
  43. {
  44. await EnsureEchoMetadataAsync(context);
  45. EnsureEchoStatus(request.ResponseStatus, context);
  46. foreach (var responseParam in request.ResponseParameters)
  47. {
  48. var response = new StreamingOutputCallResponse { Payload = CreateZerosPayload(responseParam.Size) };
  49. await responseStream.WriteAsync(response);
  50. }
  51. }
  52. public override async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream, ServerCallContext context)
  53. {
  54. await EnsureEchoMetadataAsync(context);
  55. int sum = 0;
  56. await requestStream.ForEachAsync(request =>
  57. {
  58. sum += request.Payload.Body.Length;
  59. return TaskUtils.CompletedTask;
  60. });
  61. return new StreamingInputCallResponse { AggregatedPayloadSize = sum };
  62. }
  63. public override async Task FullDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context)
  64. {
  65. await EnsureEchoMetadataAsync(context);
  66. await requestStream.ForEachAsync(async request =>
  67. {
  68. EnsureEchoStatus(request.ResponseStatus, context);
  69. foreach (var responseParam in request.ResponseParameters)
  70. {
  71. var response = new StreamingOutputCallResponse { Payload = CreateZerosPayload(responseParam.Size) };
  72. await responseStream.WriteAsync(response);
  73. }
  74. });
  75. }
  76. public override Task HalfDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. private static Payload CreateZerosPayload(int size)
  81. {
  82. return new Payload { Body = ByteString.CopyFrom(new byte[size]) };
  83. }
  84. private static async Task EnsureEchoMetadataAsync(ServerCallContext context)
  85. {
  86. var echoInitialList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-initial").ToList();
  87. if (echoInitialList.Any()) {
  88. var entry = echoInitialList.Single();
  89. await context.WriteResponseHeadersAsync(new Metadata { entry });
  90. }
  91. var echoTrailingList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ToList();
  92. if (echoTrailingList.Any()) {
  93. context.ResponseTrailers.Add(echoTrailingList.Single());
  94. }
  95. }
  96. private static void EnsureEchoStatus(EchoStatus responseStatus, ServerCallContext context)
  97. {
  98. if (responseStatus != null)
  99. {
  100. var statusCode = (StatusCode)responseStatus.Code;
  101. context.Status = new Status(statusCode, responseStatus.Message);
  102. }
  103. }
  104. }
  105. }