using System; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using System.Reactive.Linq; using Google.GRPC.Core; namespace math { /// /// Math service definitions (this is handwritten version of code that will normally be generated). /// public class MathGrpc { readonly static Marshaller divArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom); readonly static Marshaller divReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom); readonly static Marshaller numMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom); readonly static Marshaller fibArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom); readonly static Method divMethod = new Method( MethodType.Unary, "/math.Math/Div", divArgsMarshaller, divReplyMarshaller ); readonly static Method fibMethod = new Method( MethodType.ServerStreaming, "/math.Math/Fib", fibArgsMarshaller, numMarshaller ); readonly static Method sumMethod = new Method( MethodType.ClientStreaming, "/math.Math/Sum", numMarshaller, numMarshaller ); readonly static Method divManyMethod = new Method( MethodType.DuplexStreaming, "/math.Math/DivMany", divArgsMarshaller, divReplyMarshaller ); public interface IMathServiceClient { DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)); Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)); Task Fib(FibArgs request, IObserver responseObserver, CancellationToken token = default(CancellationToken)); ClientStreamingAsyncResult Sum(CancellationToken token = default(CancellationToken)); IObserver DivMany(IObserver responseObserver, CancellationToken token = default(CancellationToken)); } public class MathServiceClientStub : IMathServiceClient { readonly Channel channel; public MathServiceClientStub(Channel channel) { this.channel = channel; } public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)) { var call = new Google.GRPC.Core.Call(divMethod, channel); return Calls.BlockingUnaryCall(call, request, token); } public Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)) { var call = new Google.GRPC.Core.Call(divMethod, channel); return Calls.AsyncUnaryCall(call, request, token); } public Task Fib(FibArgs request, IObserver responseObserver, CancellationToken token = default(CancellationToken)) { var call = new Google.GRPC.Core.Call(fibMethod, channel); return Calls.AsyncServerStreamingCall(call, request, responseObserver, token); } public ClientStreamingAsyncResult Sum(CancellationToken token = default(CancellationToken)) { var call = new Google.GRPC.Core.Call(sumMethod, channel); return Calls.AsyncClientStreamingCall(call, token); } public IObserver DivMany(IObserver responseObserver, CancellationToken token = default(CancellationToken)) { var call = new Google.GRPC.Core.Call(divManyMethod, channel); return Calls.DuplexStreamingCall(call, responseObserver, token); } } // server-side interface public interface IMathService { void Div(DivArgs request, IObserver responseObserver); void Fib(FibArgs request, IObserver responseObserver); IObserver Sum(IObserver responseObserver); IObserver DivMany(IObserver responseObserver); } public static ServerServiceDefinition BindService(IMathService serviceImpl) { return ServerServiceDefinition.CreateBuilder("/math.Math/") .AddMethod(divMethod, serviceImpl.Div) .AddMethod(fibMethod, serviceImpl.Fib) .AddMethod(sumMethod, serviceImpl.Sum) .AddMethod(divManyMethod, serviceImpl.DivMany).Build(); } public static IMathServiceClient NewStub(Channel channel) { return new MathServiceClientStub(channel); } } }