TestServiceGrpc.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using Google.GRPC.Core;
  7. namespace grpc.testing
  8. {
  9. /// <summary>
  10. /// TestService (this is handwritten version of code that will normally be generated).
  11. /// </summary>
  12. public class TestServiceGrpc
  13. {
  14. readonly static Marshaller<Empty> emptyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Empty.ParseFrom);
  15. readonly static Marshaller<SimpleRequest> simpleRequestMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), SimpleRequest.ParseFrom);
  16. readonly static Marshaller<SimpleResponse> simpleResponseMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), SimpleResponse.ParseFrom);
  17. readonly static Marshaller<StreamingOutputCallRequest> streamingOutputCallRequestMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), StreamingOutputCallRequest.ParseFrom);
  18. readonly static Marshaller<StreamingOutputCallResponse> streamingOutputCallResponseMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), StreamingOutputCallResponse.ParseFrom);
  19. readonly static Marshaller<StreamingInputCallRequest> streamingInputCallRequestMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), StreamingInputCallRequest.ParseFrom);
  20. readonly static Marshaller<StreamingInputCallResponse> streamingInputCallResponseMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), StreamingInputCallResponse.ParseFrom);
  21. readonly static Method<Empty, Empty> emptyCallMethod = new Method<Empty, Empty>(
  22. MethodType.Unary,
  23. "/grpc.testing.TestService/EmptyCall",
  24. emptyMarshaller,
  25. emptyMarshaller
  26. );
  27. readonly static Method<SimpleRequest, SimpleResponse> unaryCallMethod = new Method<SimpleRequest, SimpleResponse>(
  28. MethodType.Unary,
  29. "/grpc.testing.TestService/UnaryCall",
  30. simpleRequestMarshaller,
  31. simpleResponseMarshaller
  32. );
  33. readonly static Method<StreamingOutputCallRequest, StreamingOutputCallResponse> streamingOutputCallMethod = new Method<StreamingOutputCallRequest, StreamingOutputCallResponse>(
  34. MethodType.ServerStreaming,
  35. "/grpc.testing.TestService/StreamingOutputCall",
  36. streamingOutputCallRequestMarshaller,
  37. streamingOutputCallResponseMarshaller
  38. );
  39. readonly static Method<StreamingInputCallRequest, StreamingInputCallResponse> streamingInputCallMethod = new Method<StreamingInputCallRequest, StreamingInputCallResponse>(
  40. MethodType.ClientStreaming,
  41. "/grpc.testing.TestService/StreamingInputCall",
  42. streamingInputCallRequestMarshaller,
  43. streamingInputCallResponseMarshaller
  44. );
  45. readonly static Method<StreamingOutputCallRequest, StreamingOutputCallResponse> fullDuplexCallMethod = new Method<StreamingOutputCallRequest, StreamingOutputCallResponse>(
  46. MethodType.DuplexStreaming,
  47. "/grpc.testing.TestService/FullDuplexCall",
  48. streamingOutputCallRequestMarshaller,
  49. streamingOutputCallResponseMarshaller
  50. );
  51. readonly static Method<StreamingOutputCallRequest, StreamingOutputCallResponse> halfDuplexCallMethod = new Method<StreamingOutputCallRequest, StreamingOutputCallResponse>(
  52. MethodType.DuplexStreaming,
  53. "/grpc.testing.TestService/HalfDuplexCall",
  54. streamingOutputCallRequestMarshaller,
  55. streamingOutputCallResponseMarshaller
  56. );
  57. public interface ITestServiceClient
  58. {
  59. Empty EmptyCall(Empty request, CancellationToken token = default(CancellationToken));
  60. Task<Empty> EmptyCallAsync(Empty request, CancellationToken token = default(CancellationToken));
  61. SimpleResponse UnaryCall(SimpleRequest request, CancellationToken token = default(CancellationToken));
  62. Task<SimpleResponse> UnaryCallAsync(SimpleRequest request, CancellationToken token = default(CancellationToken));
  63. Task StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
  64. ClientStreamingAsyncResult<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken));
  65. IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
  66. IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
  67. }
  68. public class TestServiceClientStub : ITestServiceClient
  69. {
  70. readonly Channel channel;
  71. public TestServiceClientStub(Channel channel)
  72. {
  73. this.channel = channel;
  74. }
  75. public Empty EmptyCall(Empty request, CancellationToken token = default(CancellationToken))
  76. {
  77. var call = new Google.GRPC.Core.Call<Empty, Empty>(emptyCallMethod, channel);
  78. return Calls.BlockingUnaryCall(call, request, token);
  79. }
  80. public Task<Empty> EmptyCallAsync(Empty request, CancellationToken token = default(CancellationToken))
  81. {
  82. var call = new Google.GRPC.Core.Call<Empty, Empty>(emptyCallMethod, channel);
  83. return Calls.AsyncUnaryCall(call, request, token);
  84. }
  85. public SimpleResponse UnaryCall(SimpleRequest request, CancellationToken token = default(CancellationToken))
  86. {
  87. var call = new Google.GRPC.Core.Call<SimpleRequest, SimpleResponse>(unaryCallMethod, channel);
  88. return Calls.BlockingUnaryCall(call, request, token);
  89. }
  90. public Task<SimpleResponse> UnaryCallAsync(SimpleRequest request, CancellationToken token = default(CancellationToken))
  91. {
  92. var call = new Google.GRPC.Core.Call<SimpleRequest, SimpleResponse>(unaryCallMethod, channel);
  93. return Calls.AsyncUnaryCall(call, request, token);
  94. }
  95. public Task StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken)) {
  96. var call = new Google.GRPC.Core.Call<StreamingOutputCallRequest, StreamingOutputCallResponse>(streamingOutputCallMethod, channel);
  97. return Calls.AsyncServerStreamingCall(call, request, responseObserver, token);
  98. }
  99. public ClientStreamingAsyncResult<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken))
  100. {
  101. var call = new Google.GRPC.Core.Call<StreamingInputCallRequest, StreamingInputCallResponse>(streamingInputCallMethod, channel);
  102. return Calls.AsyncClientStreamingCall(call, token);
  103. }
  104. public IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken))
  105. {
  106. var call = new Google.GRPC.Core.Call<StreamingOutputCallRequest, StreamingOutputCallResponse>(fullDuplexCallMethod, channel);
  107. return Calls.DuplexStreamingCall(call, responseObserver, token);
  108. }
  109. public IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken))
  110. {
  111. var call = new Google.GRPC.Core.Call<StreamingOutputCallRequest, StreamingOutputCallResponse>(halfDuplexCallMethod, channel);
  112. return Calls.DuplexStreamingCall(call, responseObserver, token);
  113. }
  114. }
  115. // server-side interface
  116. public interface ITestService
  117. {
  118. void EmptyCall(Empty request, IObserver<Empty> responseObserver);
  119. void UnaryCall(SimpleRequest request, IObserver<SimpleResponse> responseObserver);
  120. void StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver);
  121. IObserver<StreamingInputCallRequest> StreamingInputCall(IObserver<StreamingInputCallResponse> responseObserver);
  122. IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver);
  123. IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver);
  124. }
  125. public static ServerServiceDefinition BindService(ITestService serviceImpl)
  126. {
  127. return ServerServiceDefinition.CreateBuilder("/grpc.testing.TestService/")
  128. .AddMethod(emptyCallMethod, serviceImpl.EmptyCall)
  129. .AddMethod(unaryCallMethod, serviceImpl.UnaryCall)
  130. .AddMethod(streamingOutputCallMethod, serviceImpl.StreamingOutputCall)
  131. .AddMethod(streamingInputCallMethod, serviceImpl.StreamingInputCall)
  132. .AddMethod(fullDuplexCallMethod, serviceImpl.FullDuplexCall)
  133. .AddMethod(halfDuplexCallMethod, serviceImpl.HalfDuplexCall)
  134. .Build();
  135. }
  136. public static ITestServiceClient NewStub(Channel channel)
  137. {
  138. return new TestServiceClientStub(channel);
  139. }
  140. }
  141. }