AutoChannelCallInvoker.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel.Channels;
  4. using System.Text;
  5. using Grpc.Core;
  6. namespace Grpc.Extension.Client.Internal
  7. {
  8. /// <summary>
  9. /// 自动负载Channel的CallInvoker
  10. /// </summary>
  11. internal class AutoChannelCallInvoker : CallInvoker
  12. {
  13. private ChannelPool _channelManager;
  14. /// <summary>
  15. /// 自动负载Channel的CallInvoker
  16. /// </summary>
  17. public AutoChannelCallInvoker(ChannelPool channelManager)
  18. {
  19. this._channelManager = channelManager;
  20. }
  21. /// <summary>
  22. /// Invokes a simple remote call in a blocking fashion.
  23. /// </summary>
  24. public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
  25. {
  26. var call = CreateCall(method, host, options);
  27. return Calls.BlockingUnaryCall(call, request);
  28. }
  29. /// <summary>
  30. /// Invokes a simple remote call asynchronously.
  31. /// </summary>
  32. public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
  33. {
  34. var call = CreateCall(method, host, options);
  35. return Calls.AsyncUnaryCall(call, request);
  36. }
  37. /// <summary>
  38. /// Invokes a server streaming call asynchronously.
  39. /// In server streaming scenario, client sends on request and server responds with a stream of responses.
  40. /// </summary>
  41. public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
  42. {
  43. var call = CreateCall(method, host, options);
  44. return Calls.AsyncServerStreamingCall(call, request);
  45. }
  46. /// <summary>
  47. /// Invokes a client streaming call asynchronously.
  48. /// In client streaming scenario, client sends a stream of requests and server responds with a single response.
  49. /// </summary>
  50. public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options)
  51. {
  52. var call = CreateCall(method, host, options);
  53. return Calls.AsyncClientStreamingCall(call);
  54. }
  55. /// <summary>
  56. /// Invokes a duplex streaming call asynchronously.
  57. /// In duplex streaming scenario, client sends a stream of requests and server responds with a stream of responses.
  58. /// The response stream is completely independent and both side can be sending messages at the same time.
  59. /// </summary>
  60. public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options)
  61. {
  62. var call = CreateCall(method, host, options);
  63. return Calls.AsyncDuplexStreamingCall(call);
  64. }
  65. /// <summary>Creates call invocation details for given method.</summary>
  66. protected virtual CallInvocationDetails<TRequest, TResponse> CreateCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options)
  67. where TRequest : class
  68. where TResponse : class
  69. {
  70. var channel = _channelManager.GetChannel(method.ServiceName);
  71. return new CallInvocationDetails<TRequest, TResponse>(channel, method, host, options);
  72. }
  73. }
  74. }