Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015-2016 gRPC authors. |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Collections.Generic; |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 21 | using System.Linq; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 22 | using System.Threading; |
| 23 | using System.Threading.Tasks; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 24 | using Google.Protobuf; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 25 | using Grpc.Core; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 26 | using Grpc.Core.Utils; |
| 27 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 28 | namespace Grpc.Testing |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 29 | { |
| 30 | /// <summary> |
| 31 | /// Implementation of TestService server |
| 32 | /// </summary> |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 33 | public class TestServiceImpl : TestService.TestServiceBase |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 34 | { |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 35 | public override Task<Empty> EmptyCall(Empty request, ServerCallContext context) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 36 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 37 | return Task.FromResult(new Empty()); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 40 | public override async Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 41 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 42 | await EnsureEchoMetadataAsync(context); |
| 43 | EnsureEchoStatus(request.ResponseStatus, context); |
| 44 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 45 | var response = new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) }; |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 46 | return response; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 49 | public override async Task StreamingOutputCall(StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 50 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 51 | await EnsureEchoMetadataAsync(context); |
| 52 | EnsureEchoStatus(request.ResponseStatus, context); |
| 53 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 54 | foreach (var responseParam in request.ResponseParameters) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 55 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 56 | var response = new StreamingOutputCallResponse { Payload = CreateZerosPayload(responseParam.Size) }; |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 57 | await responseStream.WriteAsync(response); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 58 | } |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 61 | public override async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream, ServerCallContext context) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 62 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 63 | await EnsureEchoMetadataAsync(context); |
| 64 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 65 | int sum = 0; |
Jan Tattermusch | c152d66 | 2017-08-09 09:24:20 +0200 | [diff] [blame] | 66 | await requestStream.ForEachAsync(request => |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 67 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 68 | sum += request.Payload.Body.Length; |
Jan Tattermusch | c152d66 | 2017-08-09 09:24:20 +0200 | [diff] [blame] | 69 | return TaskUtils.CompletedTask; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 70 | }); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 71 | return new StreamingInputCallResponse { AggregatedPayloadSize = sum }; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 74 | public override async Task FullDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 75 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 76 | await EnsureEchoMetadataAsync(context); |
| 77 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 78 | await requestStream.ForEachAsync(async request => |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 79 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 80 | EnsureEchoStatus(request.ResponseStatus, context); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 81 | foreach (var responseParam in request.ResponseParameters) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 82 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 83 | var response = new StreamingOutputCallResponse { Payload = CreateZerosPayload(responseParam.Size) }; |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 84 | await responseStream.WriteAsync(response); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 85 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
Jan Tattermusch | c152d66 | 2017-08-09 09:24:20 +0200 | [diff] [blame] | 89 | public override Task HalfDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallContext context) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 90 | { |
| 91 | throw new NotImplementedException(); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 94 | private static Payload CreateZerosPayload(int size) |
| 95 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 96 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 97 | } |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 98 | |
| 99 | private static async Task EnsureEchoMetadataAsync(ServerCallContext context) |
| 100 | { |
| 101 | var echoInitialList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-initial").ToList(); |
| 102 | if (echoInitialList.Any()) { |
| 103 | var entry = echoInitialList.Single(); |
| 104 | await context.WriteResponseHeadersAsync(new Metadata { entry }); |
| 105 | } |
| 106 | |
| 107 | var echoTrailingList = context.RequestHeaders.Where((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ToList(); |
| 108 | if (echoTrailingList.Any()) { |
| 109 | context.ResponseTrailers.Add(echoTrailingList.Single()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private static void EnsureEchoStatus(EchoStatus responseStatus, ServerCallContext context) |
| 114 | { |
| 115 | if (responseStatus != null) |
| 116 | { |
| 117 | var statusCode = (StatusCode)responseStatus.Code; |
| 118 | context.Status = new Status(statusCode, responseStatus.Message); |
| 119 | } |
| 120 | } |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 121 | } |
| 122 | } |