Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
| 3 | // Copyright 2015, Google Inc. |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | #endregion |
| 33 | |
| 34 | using System; |
| 35 | using System.Collections.Generic; |
| 36 | using System.Threading; |
| 37 | using System.Threading.Tasks; |
| 38 | using Google.ProtocolBuffers; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 39 | using Grpc.Core; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 40 | using Grpc.Core.Utils; |
| 41 | |
| 42 | namespace grpc.testing |
| 43 | { |
| 44 | /// <summary> |
| 45 | /// Implementation of TestService server |
| 46 | /// </summary> |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 47 | public class TestServiceImpl : TestService.ITestService |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 48 | { |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 49 | public Task<Empty> EmptyCall(ServerCallContext context, Empty request) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 50 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 51 | return Task.FromResult(Empty.DefaultInstance); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 54 | public Task<SimpleResponse> UnaryCall(ServerCallContext context, SimpleRequest request) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 55 | { |
| 56 | var response = SimpleResponse.CreateBuilder() |
| 57 | .SetPayload(CreateZerosPayload(request.ResponseSize)).Build(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 58 | return Task.FromResult(response); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 61 | public async Task StreamingOutputCall(ServerCallContext context, StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 62 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 63 | foreach (var responseParam in request.ResponseParametersList) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 64 | { |
| 65 | var response = StreamingOutputCallResponse.CreateBuilder() |
| 66 | .SetPayload(CreateZerosPayload(responseParam.Size)).Build(); |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 67 | await responseStream.WriteAsync(response); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 68 | } |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 71 | public async Task<StreamingInputCallResponse> StreamingInputCall(ServerCallContext context, IAsyncStreamReader<StreamingInputCallRequest> requestStream) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 72 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 73 | int sum = 0; |
| 74 | await requestStream.ForEach(async request => |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 75 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 76 | sum += request.Payload.Body.Length; |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 77 | }); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 78 | return StreamingInputCallResponse.CreateBuilder().SetAggregatedPayloadSize(sum).Build(); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 81 | public async Task FullDuplexCall(ServerCallContext context, IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream) |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 82 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 83 | await requestStream.ForEach(async request => |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 84 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 85 | foreach (var responseParam in request.ResponseParametersList) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 86 | { |
| 87 | var response = StreamingOutputCallResponse.CreateBuilder() |
| 88 | .SetPayload(CreateZerosPayload(responseParam.Size)).Build(); |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 89 | await responseStream.WriteAsync(response); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 90 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 91 | }); |
| 92 | } |
| 93 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 94 | public async Task HalfDuplexCall(ServerCallContext context, IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 95 | { |
| 96 | throw new NotImplementedException(); |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 99 | private static Payload CreateZerosPayload(int size) |
| 100 | { |
Jan Tattermusch | 2083138 | 2015-02-24 14:16:04 -0800 | [diff] [blame] | 101 | return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build(); |
| 102 | } |
| 103 | } |
| 104 | } |