OLD | NEW |
(Empty) | |
| 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.Linq; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Google.Protobuf; |
| 40 using Grpc.Core; |
| 41 using Grpc.Core.Utils; |
| 42 |
| 43 namespace Grpc.Testing |
| 44 { |
| 45 /// <summary> |
| 46 /// Implementation of TestService server |
| 47 /// </summary> |
| 48 public class TestServiceImpl : TestService.ITestService |
| 49 { |
| 50 public Task<Empty> EmptyCall(Empty request, ServerCallContext context) |
| 51 { |
| 52 return Task.FromResult(new Empty()); |
| 53 } |
| 54 |
| 55 public async Task<SimpleResponse> UnaryCall(SimpleRequest request, Serve
rCallContext context) |
| 56 { |
| 57 await EnsureEchoMetadataAsync(context); |
| 58 EnsureEchoStatus(request.ResponseStatus, context); |
| 59 |
| 60 var response = new SimpleResponse { Payload = CreateZerosPayload(req
uest.ResponseSize) }; |
| 61 return response; |
| 62 } |
| 63 |
| 64 public async Task StreamingOutputCall(StreamingOutputCallRequest request
, IServerStreamWriter<StreamingOutputCallResponse> responseStream, ServerCallCon
text context) |
| 65 { |
| 66 await EnsureEchoMetadataAsync(context); |
| 67 EnsureEchoStatus(request.ResponseStatus, context); |
| 68 |
| 69 foreach (var responseParam in request.ResponseParameters) |
| 70 { |
| 71 var response = new StreamingOutputCallResponse { Payload = Creat
eZerosPayload(responseParam.Size) }; |
| 72 await responseStream.WriteAsync(response); |
| 73 } |
| 74 } |
| 75 |
| 76 public async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncS
treamReader<StreamingInputCallRequest> requestStream, ServerCallContext context) |
| 77 { |
| 78 await EnsureEchoMetadataAsync(context); |
| 79 |
| 80 int sum = 0; |
| 81 await requestStream.ForEachAsync(async request => |
| 82 { |
| 83 sum += request.Payload.Body.Length; |
| 84 }); |
| 85 return new StreamingInputCallResponse { AggregatedPayloadSize = sum
}; |
| 86 } |
| 87 |
| 88 public async Task FullDuplexCall(IAsyncStreamReader<StreamingOutputCallR
equest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> response
Stream, ServerCallContext context) |
| 89 { |
| 90 await EnsureEchoMetadataAsync(context); |
| 91 |
| 92 await requestStream.ForEachAsync(async request => |
| 93 { |
| 94 EnsureEchoStatus(request.ResponseStatus, context); |
| 95 foreach (var responseParam in request.ResponseParameters) |
| 96 { |
| 97 var response = new StreamingOutputCallResponse { Payload = C
reateZerosPayload(responseParam.Size) }; |
| 98 await responseStream.WriteAsync(response); |
| 99 } |
| 100 }); |
| 101 } |
| 102 |
| 103 public async Task HalfDuplexCall(IAsyncStreamReader<StreamingOutputCallR
equest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> response
Stream, ServerCallContext context) |
| 104 { |
| 105 throw new NotImplementedException(); |
| 106 } |
| 107 |
| 108 private static Payload CreateZerosPayload(int size) |
| 109 { |
| 110 return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
| 111 } |
| 112 |
| 113 private static async Task EnsureEchoMetadataAsync(ServerCallContext cont
ext) |
| 114 { |
| 115 var echoInitialList = context.RequestHeaders.Where((entry) => entry.
Key == "x-grpc-test-echo-initial").ToList(); |
| 116 if (echoInitialList.Any()) { |
| 117 var entry = echoInitialList.Single(); |
| 118 await context.WriteResponseHeadersAsync(new Metadata { entry }); |
| 119 } |
| 120 |
| 121 var echoTrailingList = context.RequestHeaders.Where((entry) => entry
.Key == "x-grpc-test-echo-trailing-bin").ToList(); |
| 122 if (echoTrailingList.Any()) { |
| 123 context.ResponseTrailers.Add(echoTrailingList.Single()); |
| 124 } |
| 125 } |
| 126 |
| 127 private static void EnsureEchoStatus(EchoStatus responseStatus, ServerCa
llContext context) |
| 128 { |
| 129 if (responseStatus != null) |
| 130 { |
| 131 var statusCode = (StatusCode)responseStatus.Code; |
| 132 context.Status = new Status(statusCode, responseStatus.Message); |
| 133 } |
| 134 } |
| 135 } |
| 136 } |
OLD | NEW |