aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcApi/MathGrpc.cs
blob: 520fec437aba90bac3e6354caada181e45ae7fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Reactive.Linq;
using Google.GRPC.Core;

namespace math
{
    /// <summary>
    /// Math service definitions (this is handwritten version of code that will normally be generated).
    /// </summary>
    public class MathGrpc
    {
        readonly static Marshaller<DivArgs> divArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom);
        readonly static Marshaller<DivReply> divReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom);
        readonly static Marshaller<Num> numMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom);
        readonly static Marshaller<FibArgs> fibArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom);

        readonly static Method<DivArgs, DivReply> divMethod = new Method<DivArgs, DivReply>(
            MethodType.Unary,
            "/math.Math/Div",
            divArgsMarshaller,
            divReplyMarshaller
        );
        readonly static Method<FibArgs, Num> fibMethod = new Method<FibArgs, Num>(
            MethodType.ServerStreaming,
            "/math.Math/Fib",
            fibArgsMarshaller,
            numMarshaller
        );
        readonly static Method<Num, Num> sumMethod = new Method<Num, Num>(
            MethodType.ClientStreaming,
            "/math.Math/Sum",
            numMarshaller,
            numMarshaller
        );
        readonly static Method<DivArgs, DivReply> divManyMethod = new Method<DivArgs, DivReply>(
            MethodType.DuplexStreaming,
            "/math.Math/DivMany",
            divArgsMarshaller,
            divReplyMarshaller
        );

        public interface IMathServiceClient
        {
            DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken));

            Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken));

            Task Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken));

            ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken));

            IObserver<DivArgs> DivMany(IObserver<DivReply> 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<DivArgs, DivReply>(divMethod, channel);
                return Calls.BlockingUnaryCall(call, request, token);
            }

            public Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken))
            {
                var call = new Google.GRPC.Core.Call<DivArgs, DivReply>(divMethod, channel);
                return Calls.AsyncUnaryCall(call, request, token);
            }

            public Task Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken))
            {
                var call = new Google.GRPC.Core.Call<FibArgs, Num>(fibMethod, channel);
                return Calls.AsyncServerStreamingCall(call, request, responseObserver, token);
            }

            public ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken))
            {
                var call = new Google.GRPC.Core.Call<Num, Num>(sumMethod, channel);
                return Calls.AsyncClientStreamingCall(call, token);
            }

            public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken))
            {
                var call = new Google.GRPC.Core.Call<DivArgs, DivReply>(divManyMethod, channel);
                return Calls.DuplexStreamingCall(call, responseObserver, token);
            }
        }

        // server-side interface
        public interface IMathService
        {
            void Div(DivArgs request, IObserver<DivReply> responseObserver);

            void Fib(FibArgs request, IObserver<Num> responseObserver);

            IObserver<Num> Sum(IObserver<Num> responseObserver);

            IObserver<DivArgs> DivMany(IObserver<DivReply> 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);
        }
    }
}