-
Notifications
You must be signed in to change notification settings - Fork 1
/
GreeterClient.cs
85 lines (75 loc) · 2.93 KB
/
GreeterClient.cs
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.Collections.Generic;
using System.Linq;
using System.Text;
using Grpc.Core;
using pb = global::Google.Protobuf;
namespace Example
{
/*
* This is class useful to serialize from and to proto.
*/
public class ProtoMarshaller<T> : Marshaller<T> where T : Google.Protobuf.IMessage<T>
{
public ProtoMarshaller(pb::MessageParser<T> parser) :
base(ProtoMarshaller<T>.Serializer, ProtoMarshaller<T>.DeserializerFn(parser))
{
}
static new byte[] Serializer(T obj)
{
byte[] outBytes = new byte[obj.CalculateSize()];
pb::CodedOutputStream outStream = new pb::CodedOutputStream(outBytes);
obj.WriteTo(outStream);
return outBytes;
}
static Func<byte[], T> DeserializerFn(pb::MessageParser<T> parser)
{
return (serialized) =>
{
pb::CodedInputStream inStream = new pb::CodedInputStream(serialized);
return parser.ParseFrom(inStream);
};
}
}
/*
* This is manually written Client for Greeter. Normally it would be generated by grpc plugins,
* but we cannot use it because of break in .Net 3.5 support required by Unity.
*/
class Client
{
private string host;
private Channel ch;
private DefaultCallInvoker invoker;
public Client(string host, int port)
{
this.host = host;
this.ch = new Channel(host, port, ChannelCredentials.Insecure);
this.invoker = new DefaultCallInvoker(ch);
}
/*
* Missing in gRPC library, but very useful (not used in this example, though)
*/
public virtual TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
where TRequest : class
where TResponse : class
{
var call = new CallInvocationDetails<TRequest, TResponse>(this.ch, method, host, options);
return Calls.BlockingUnaryCall(call, request);
}
public AsyncServerStreamingCall<Example.HelloReply> SayHello(GrpcCancellationTokenSource ctx, Example.HelloRequest req)
{
return this.invoker.AsyncServerStreamingCall<Example.HelloRequest, Example.HelloReply>(
new Method<Example.HelloRequest, Example.HelloReply>(
MethodType.ServerStreaming,
"example.MultiGreeter",
"sayHello",
new ProtoMarshaller<Example.HelloRequest>(Example.HelloRequest.Parser),
new ProtoMarshaller<Example.HelloReply>(Example.HelloReply.Parser)
),
this.host,
new CallOptions(cancellationToken: new GrpcCancellationToken(ctx)),
req
);
}
}
}