aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcCore/Calls.cs
blob: c3e51cb47811eaf3a9753bcca4ec8165cf705238 (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
using System;
using System.Threading;
using System.Threading.Tasks;
using Google.GRPC.Core.Internal;

namespace Google.GRPC.Core
{
    // NOTE: this class is work-in-progress

    /// <summary>
    /// Helper methods for generated stubs to make RPC calls.
    /// </summary>
    public static class Calls
    {
        public static TResponse BlockingUnaryCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
        {
            //TODO: implement this in real synchronous style once new GRPC C core API is available.
            return AsyncUnaryCall(call, req, token).Result;
        }

        public static async Task<TResponse> AsyncUnaryCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
        {
            var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestSerializer, call.ResponseDeserializer);
            asyncCall.Initialize(call.Channel, call.MethodName);
            asyncCall.Start(false, GetCompletionQueue());

            await asyncCall.WriteAsync(req);
            await asyncCall.WritesCompletedAsync();

            TResponse response = await asyncCall.ReadAsync();

            Status status = await asyncCall.Finished;

            if (status.StatusCode != StatusCode.GRPC_STATUS_OK)
            {
                throw new RpcException(status);
            }
            return response;
        }

        public static async Task AsyncServerStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, IObserver<TResponse> outputs, CancellationToken token)
        {
            var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestSerializer, call.ResponseDeserializer);
            asyncCall.Initialize(call.Channel, call.MethodName);
            asyncCall.Start(false, GetCompletionQueue());

            asyncCall.StartReadingToStream(outputs);

            await asyncCall.WriteAsync(req);
            await asyncCall.WritesCompletedAsync();
        }

        public static ClientStreamingAsyncResult<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
        {
            var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestSerializer, call.ResponseDeserializer);
            asyncCall.Initialize(call.Channel, call.MethodName);
            asyncCall.Start(false, GetCompletionQueue());

            var task = asyncCall.ReadAsync();
            var inputs = new StreamingInputObserver<TRequest, TResponse>(asyncCall);
            return new ClientStreamingAsyncResult<TRequest, TResponse>(task, inputs);
        }

        public static TResponse BlockingClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, IObservable<TRequest> inputs, CancellationToken token)
        {
            throw new NotImplementedException();
        }

        public static IObserver<TRequest> DuplexStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, IObserver<TResponse> outputs, CancellationToken token)
        {
            var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestSerializer, call.ResponseDeserializer);
            asyncCall.Initialize(call.Channel, call.MethodName);
            asyncCall.Start(false, GetCompletionQueue());

            asyncCall.StartReadingToStream(outputs);
            var inputs = new StreamingInputObserver<TRequest, TResponse>(asyncCall);
            return inputs;
        }

        private static CompletionQueueSafeHandle GetCompletionQueue() {
            return GrpcEnvironment.ThreadPool.CompletionQueue;
        }
    }
}