channel_arguments.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. //
  3. // Copyright 2015 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H
  19. #define GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H
  20. #if defined(__GNUC__)
  21. #pragma GCC system_header
  22. #endif
  23. #include <list>
  24. #include <vector>
  25. #include <grpc/compression.h>
  26. #include <grpc/grpc.h>
  27. #include <grpcpp/resource_quota.h>
  28. #include <grpcpp/support/config.h>
  29. namespace grpc {
  30. class SecureChannelCredentials;
  31. namespace testing {
  32. class ChannelArgumentsTest;
  33. } // namespace testing
  34. /// Options for channel creation. The user can use generic setters to pass
  35. /// key value pairs down to C channel creation code. For gRPC related options,
  36. /// concrete setters are provided.
  37. class ChannelArguments {
  38. public:
  39. ChannelArguments();
  40. ~ChannelArguments();
  41. ChannelArguments(const ChannelArguments& other);
  42. ChannelArguments& operator=(ChannelArguments other) {
  43. Swap(other);
  44. return *this;
  45. }
  46. void Swap(ChannelArguments& other);
  47. /// Dump arguments in this instance to \a channel_args. Does not take
  48. /// ownership of \a channel_args.
  49. ///
  50. /// Note that the underlying arguments are shared. Changes made to either \a
  51. /// channel_args or this instance would be reflected on both.
  52. void SetChannelArgs(grpc_channel_args* channel_args) const;
  53. // gRPC specific channel argument setters
  54. /// Set target name override for SSL host name checking. This option should
  55. /// be used with caution in production.
  56. void SetSslTargetNameOverride(const TString& name);
  57. // TODO(yangg) add flow control options
  58. /// Set the compression algorithm for the channel.
  59. void SetCompressionAlgorithm(grpc_compression_algorithm algorithm);
  60. /// Set the grpclb fallback timeout (in ms) for the channel. If this amount
  61. /// of time has passed but we have not gotten any non-empty \a serverlist from
  62. /// the balancer, we will fall back to use the backend address(es) returned by
  63. /// the resolver.
  64. void SetGrpclbFallbackTimeout(int fallback_timeout);
  65. /// Set a mutator for the underlying socket.
  66. void SetSocketMutator(grpc_socket_mutator* mutator);
  67. /// Set the string to prepend to the user agent.
  68. void SetUserAgentPrefix(const TString& user_agent_prefix);
  69. /// Set the buffer pool to be attached to the constructed channel.
  70. void SetResourceQuota(const grpc::ResourceQuota& resource_quota);
  71. /// Set the max receive and send message sizes.
  72. void SetMaxReceiveMessageSize(int size);
  73. void SetMaxSendMessageSize(int size);
  74. /// Set LB policy name.
  75. /// Note that if the name resolver returns only balancer addresses, the
  76. /// grpclb LB policy will be used, regardless of what is specified here.
  77. void SetLoadBalancingPolicyName(const TString& lb_policy_name);
  78. /// Set service config in JSON form.
  79. /// Primarily meant for use in unit tests.
  80. void SetServiceConfigJSON(const TString& service_config_json);
  81. // Generic channel argument setter. Only for advanced use cases.
  82. /// Set an integer argument \a value under \a key.
  83. void SetInt(const TString& key, int value);
  84. // Generic channel argument setter. Only for advanced use cases.
  85. /// Set a pointer argument \a value under \a key. Ownership is not
  86. /// transferred.
  87. void SetPointer(const TString& key, void* value);
  88. /// Set a pointer argument \a value under \a key, transferring ownership of
  89. /// \a value to the \a ChannelArguments object. The \a vtable::Delete function
  90. /// is responsible for \a value cleanup/destruction when called.
  91. void SetPointerWithVtable(const TString& key, void* value,
  92. const grpc_arg_pointer_vtable* vtable);
  93. /// Set a textual argument \a value under \a key.
  94. void SetString(const TString& key, const TString& value);
  95. /// Return (by value) a C \a grpc_channel_args structure which points to
  96. /// arguments owned by this \a ChannelArguments instance
  97. grpc_channel_args c_channel_args() const {
  98. grpc_channel_args out;
  99. out.num_args = args_.size();
  100. out.args = args_.empty() ? nullptr : const_cast<grpc_arg*>(&args_[0]);
  101. return out;
  102. }
  103. private:
  104. friend class grpc::SecureChannelCredentials;
  105. friend class grpc::testing::ChannelArgumentsTest;
  106. /// Default pointer argument operations.
  107. struct PointerVtableMembers {
  108. static void* Copy(void* in) { return in; }
  109. static void Destroy(void* /*in*/) {}
  110. static int Compare(void* a, void* b) {
  111. if (a < b) return -1;
  112. if (a > b) return 1;
  113. return 0;
  114. }
  115. };
  116. // Returns empty string when it is not set.
  117. TString GetSslTargetNameOverride() const;
  118. std::vector<grpc_arg> args_;
  119. std::list<TString> strings_;
  120. };
  121. } // namespace grpc
  122. #endif // GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H