blob: f938a245439b9bb04222c1c86ded99ab0b820d11 [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 {
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 Tattermusch337a2dd2015-02-13 15:41:41 -080084 void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken));
Jan Tattermusch15111f52015-02-05 18:15:14 -080085
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 Tattermusch30868622015-02-19 09:22:33 -0800102 var call = new Grpc.Core.Call<DivArgs, DivReply>(divMethod, channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800103 return Calls.BlockingUnaryCall(call, request, token);
104 }
105
106 public Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken))
107 {
Jan Tattermusch30868622015-02-19 09:22:33 -0800108 var call = new Grpc.Core.Call<DivArgs, DivReply>(divMethod, channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800109 return Calls.AsyncUnaryCall(call, request, token);
110 }
111
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800112 public void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken))
Jan Tattermusch15111f52015-02-05 18:15:14 -0800113 {
Jan Tattermusch30868622015-02-19 09:22:33 -0800114 var call = new Grpc.Core.Call<FibArgs, Num>(fibMethod, channel);
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800115 Calls.AsyncServerStreamingCall(call, request, responseObserver, token);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800116 }
117
118 public ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken))
119 {
Jan Tattermusch30868622015-02-19 09:22:33 -0800120 var call = new Grpc.Core.Call<Num, Num>(sumMethod, channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800121 return Calls.AsyncClientStreamingCall(call, token);
122 }
123
124 public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken))
125 {
Jan Tattermusch30868622015-02-19 09:22:33 -0800126 var call = new Grpc.Core.Call<DivArgs, DivReply>(divManyMethod, channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800127 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 Tiller190d3602015-02-18 09:23:38 -0800157}