Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
| 36 | using System.Reactive.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using System.Threading; |
| 38 | using System.Threading.Tasks; |
| 39 | using Grpc.Core; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 40 | |
| 41 | namespace math |
| 42 | { |
| 43 | /// <summary> |
| 44 | /// Math service definitions (this is handwritten version of code that will normally be generated). |
| 45 | /// </summary> |
| 46 | public class MathGrpc |
| 47 | { |
| 48 | readonly static Marshaller<DivArgs> divArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom); |
| 49 | readonly static Marshaller<DivReply> divReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom); |
| 50 | readonly static Marshaller<Num> numMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom); |
| 51 | readonly static Marshaller<FibArgs> fibArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom); |
| 52 | |
| 53 | readonly static Method<DivArgs, DivReply> divMethod = new Method<DivArgs, DivReply>( |
| 54 | MethodType.Unary, |
| 55 | "/math.Math/Div", |
| 56 | divArgsMarshaller, |
| 57 | divReplyMarshaller |
| 58 | ); |
| 59 | readonly static Method<FibArgs, Num> fibMethod = new Method<FibArgs, Num>( |
| 60 | MethodType.ServerStreaming, |
| 61 | "/math.Math/Fib", |
| 62 | fibArgsMarshaller, |
| 63 | numMarshaller |
| 64 | ); |
| 65 | readonly static Method<Num, Num> sumMethod = new Method<Num, Num>( |
| 66 | MethodType.ClientStreaming, |
| 67 | "/math.Math/Sum", |
| 68 | numMarshaller, |
| 69 | numMarshaller |
| 70 | ); |
| 71 | readonly static Method<DivArgs, DivReply> divManyMethod = new Method<DivArgs, DivReply>( |
| 72 | MethodType.DuplexStreaming, |
| 73 | "/math.Math/DivMany", |
| 74 | divArgsMarshaller, |
| 75 | divReplyMarshaller |
| 76 | ); |
| 77 | |
| 78 | public interface IMathServiceClient |
| 79 | { |
| 80 | DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)); |
| 81 | |
| 82 | Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)); |
| 83 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 84 | void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 85 | |
| 86 | ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken)); |
| 87 | |
| 88 | IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken)); |
| 89 | } |
| 90 | |
| 91 | public class MathServiceClientStub : IMathServiceClient |
| 92 | { |
| 93 | readonly Channel channel; |
| 94 | |
| 95 | public MathServiceClientStub(Channel channel) |
| 96 | { |
| 97 | this.channel = channel; |
| 98 | } |
| 99 | |
| 100 | public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)) |
| 101 | { |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 102 | var call = new Grpc.Core.Call<DivArgs, DivReply>(divMethod, channel); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 103 | return Calls.BlockingUnaryCall(call, request, token); |
| 104 | } |
| 105 | |
| 106 | public Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)) |
| 107 | { |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 108 | var call = new Grpc.Core.Call<DivArgs, DivReply>(divMethod, channel); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 109 | return Calls.AsyncUnaryCall(call, request, token); |
| 110 | } |
| 111 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 112 | public void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken)) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 113 | { |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 114 | var call = new Grpc.Core.Call<FibArgs, Num>(fibMethod, channel); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 115 | Calls.AsyncServerStreamingCall(call, request, responseObserver, token); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken)) |
| 119 | { |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 120 | var call = new Grpc.Core.Call<Num, Num>(sumMethod, channel); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 121 | return Calls.AsyncClientStreamingCall(call, token); |
| 122 | } |
| 123 | |
| 124 | public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken)) |
| 125 | { |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 126 | var call = new Grpc.Core.Call<DivArgs, DivReply>(divManyMethod, channel); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 127 | return Calls.DuplexStreamingCall(call, responseObserver, token); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // server-side interface |
| 132 | public interface IMathService |
| 133 | { |
| 134 | void Div(DivArgs request, IObserver<DivReply> responseObserver); |
| 135 | |
| 136 | void Fib(FibArgs request, IObserver<Num> responseObserver); |
| 137 | |
| 138 | IObserver<Num> Sum(IObserver<Num> responseObserver); |
| 139 | |
| 140 | IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver); |
| 141 | } |
| 142 | |
| 143 | public static ServerServiceDefinition BindService(IMathService serviceImpl) |
| 144 | { |
| 145 | return ServerServiceDefinition.CreateBuilder("/math.Math/") |
| 146 | .AddMethod(divMethod, serviceImpl.Div) |
| 147 | .AddMethod(fibMethod, serviceImpl.Fib) |
| 148 | .AddMethod(sumMethod, serviceImpl.Sum) |
| 149 | .AddMethod(divManyMethod, serviceImpl.DivMany).Build(); |
| 150 | } |
| 151 | |
| 152 | public static IMathServiceClient NewStub(Channel channel) |
| 153 | { |
| 154 | return new MathServiceClientStub(channel); |
| 155 | } |
| 156 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 157 | } |