blob: 03f5c31cb7acc77e7c846f93e08daa3c9cad1847 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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 Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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 Tattermusch15111f52015-02-05 18:15:14 -080034using System;
Jan Tattermusch15111f52015-02-05 18:15:14 -080035using System.Collections.Generic;
36using System.Reactive.Linq;
Jan Tattermusch30868622015-02-19 09:22:33 -080037using System.Threading;
38using System.Threading.Tasks;
39using Grpc.Core;
Jan Tattermusch15111f52015-02-05 18:15:14 -080040
41namespace 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 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -070048 static readonly string ServiceName = "/math.Math";
49
Jan Tattermusch3de9f492015-03-12 12:53:50 -070050 static readonly Marshaller<DivArgs> DivArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom);
51 static readonly Marshaller<DivReply> DivReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom);
52 static readonly Marshaller<Num> NumMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom);
53 static readonly Marshaller<FibArgs> FibArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom);
Jan Tattermusch15111f52015-02-05 18:15:14 -080054
Jan Tattermusch3de9f492015-03-12 12:53:50 -070055 static readonly Method<DivArgs, DivReply> DivMethod = new Method<DivArgs, DivReply>(
Jan Tattermusch15111f52015-02-05 18:15:14 -080056 MethodType.Unary,
Jan Tattermuschc0b37212015-03-13 08:35:41 -070057 "Div",
Jan Tattermusch3de9f492015-03-12 12:53:50 -070058 DivArgsMarshaller,
59 DivReplyMarshaller);
60
61 static readonly Method<FibArgs, Num> FibMethod = new Method<FibArgs, Num>(
Jan Tattermusch15111f52015-02-05 18:15:14 -080062 MethodType.ServerStreaming,
Jan Tattermuschc0b37212015-03-13 08:35:41 -070063 "Fib",
Jan Tattermusch3de9f492015-03-12 12:53:50 -070064 FibArgsMarshaller,
65 NumMarshaller);
66
67 static readonly Method<Num, Num> SumMethod = new Method<Num, Num>(
Jan Tattermusch15111f52015-02-05 18:15:14 -080068 MethodType.ClientStreaming,
Jan Tattermuschc0b37212015-03-13 08:35:41 -070069 "Sum",
Jan Tattermusch3de9f492015-03-12 12:53:50 -070070 NumMarshaller,
71 NumMarshaller);
72
73 static readonly Method<DivArgs, DivReply> DivManyMethod = new Method<DivArgs, DivReply>(
Jan Tattermusch15111f52015-02-05 18:15:14 -080074 MethodType.DuplexStreaming,
Jan Tattermuschc0b37212015-03-13 08:35:41 -070075 "DivMany",
Jan Tattermusch3de9f492015-03-12 12:53:50 -070076 DivArgsMarshaller,
77 DivReplyMarshaller);
Jan Tattermusch15111f52015-02-05 18:15:14 -080078
79 public interface IMathServiceClient
80 {
81 DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken));
82
83 Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken));
84
Jan Tattermuscha5272b62015-04-30 11:56:46 -070085 AsyncServerStreamingCall<Num> Fib(FibArgs request, CancellationToken token = default(CancellationToken));
Jan Tattermusch15111f52015-02-05 18:15:14 -080086
Jan Tattermuscha5272b62015-04-30 11:56:46 -070087 AsyncClientStreamingCall<Num, Num> Sum(CancellationToken token = default(CancellationToken));
Jan Tattermusch15111f52015-02-05 18:15:14 -080088
Jan Tattermuscha5272b62015-04-30 11:56:46 -070089 AsyncDuplexStreamingCall<DivArgs, DivReply> DivMany(CancellationToken token = default(CancellationToken));
Jan Tattermusch15111f52015-02-05 18:15:14 -080090 }
91
Jan Tattermuschc0b37212015-03-13 08:35:41 -070092 public class MathServiceClientStub : AbstractStub<MathServiceClientStub, StubConfiguration>, IMathServiceClient
Jan Tattermusch15111f52015-02-05 18:15:14 -080093 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -070094 public MathServiceClientStub(Channel channel) : this(channel, StubConfiguration.Default)
Jan Tattermusch15111f52015-02-05 18:15:14 -080095 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -070096 }
97
98 public MathServiceClientStub(Channel channel, StubConfiguration config) : base(channel, config)
99 {
Jan Tattermusch15111f52015-02-05 18:15:14 -0800100 }
101
102 public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken))
103 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700104 var call = CreateCall(ServiceName, DivMethod);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800105 return Calls.BlockingUnaryCall(call, request, token);
106 }
107
108 public Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken))
109 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700110 var call = CreateCall(ServiceName, DivMethod);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800111 return Calls.AsyncUnaryCall(call, request, token);
112 }
113
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700114 public AsyncServerStreamingCall<Num> Fib(FibArgs request, CancellationToken token = default(CancellationToken))
Jan Tattermusch15111f52015-02-05 18:15:14 -0800115 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700116 var call = CreateCall(ServiceName, FibMethod);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700117 return Calls.AsyncServerStreamingCall(call, request, token);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800118 }
119
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700120 public AsyncClientStreamingCall<Num, Num> Sum(CancellationToken token = default(CancellationToken))
Jan Tattermusch15111f52015-02-05 18:15:14 -0800121 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700122 var call = CreateCall(ServiceName, SumMethod);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800123 return Calls.AsyncClientStreamingCall(call, token);
124 }
125
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700126 public AsyncDuplexStreamingCall<DivArgs, DivReply> DivMany(CancellationToken token = default(CancellationToken))
Jan Tattermusch15111f52015-02-05 18:15:14 -0800127 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700128 var call = CreateCall(ServiceName, DivManyMethod);
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700129 return Calls.AsyncDuplexStreamingCall(call, token);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800130 }
131 }
132
133 // server-side interface
134 public interface IMathService
135 {
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -0700136 Task<DivReply> Div(ServerCallContext context, DivArgs request);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800137
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -0700138 Task Fib(ServerCallContext context, FibArgs request, IServerStreamWriter<Num> responseStream);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800139
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -0700140 Task<Num> Sum(ServerCallContext context, IAsyncStreamReader<Num> requestStream);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800141
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -0700142 Task DivMany(ServerCallContext context, IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800143 }
144
145 public static ServerServiceDefinition BindService(IMathService serviceImpl)
146 {
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700147 return ServerServiceDefinition.CreateBuilder(ServiceName)
Jan Tattermusch3de9f492015-03-12 12:53:50 -0700148 .AddMethod(DivMethod, serviceImpl.Div)
149 .AddMethod(FibMethod, serviceImpl.Fib)
150 .AddMethod(SumMethod, serviceImpl.Sum)
151 .AddMethod(DivManyMethod, serviceImpl.DivMany).Build();
Jan Tattermusch15111f52015-02-05 18:15:14 -0800152 }
153
154 public static IMathServiceClient NewStub(Channel channel)
155 {
156 return new MathServiceClientStub(channel);
157 }
Jan Tattermuschc0b37212015-03-13 08:35:41 -0700158
159 public static IMathServiceClient NewStub(Channel channel, StubConfiguration config)
160 {
161 return new MathServiceClientStub(channel, config);
162 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800163 }
Craig Tiller190d3602015-02-18 09:23:38 -0800164}