You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

channel_arguments.h 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <list>
  21. #include <vector>
  22. #include <grpc/compression.h>
  23. #include <grpc/grpc.h>
  24. #include <grpcpp/resource_quota.h>
  25. #include <grpcpp/support/config.h>
  26. namespace grpc
  27. {
  28. class SecureChannelCredentials;
  29. namespace testing
  30. {
  31. class ChannelArgumentsTest;
  32. } // namespace testing
  33. /// Options for channel creation. The user can use generic setters to pass
  34. /// key value pairs down to C channel creation code. For gRPC related options,
  35. /// concrete setters are provided.
  36. class ChannelArguments
  37. {
  38. public:
  39. ChannelArguments();
  40. ~ChannelArguments();
  41. ChannelArguments(const ChannelArguments& other);
  42. ChannelArguments& operator=(ChannelArguments other)
  43. {
  44. Swap(other);
  45. return *this;
  46. }
  47. void Swap(ChannelArguments& other);
  48. /// Dump arguments in this instance to \a channel_args. Does not take
  49. /// ownership of \a channel_args.
  50. ///
  51. /// Note that the underlying arguments are shared. Changes made to either \a
  52. /// channel_args or this instance would be reflected on both.
  53. void SetChannelArgs(grpc_channel_args* channel_args) const;
  54. // gRPC specific channel argument setters
  55. /// Set target name override for SSL host name checking. This option should
  56. /// be used with caution in production.
  57. void SetSslTargetNameOverride(const std::string& name);
  58. // TODO(yangg) add flow control options
  59. /// Set the compression algorithm for the channel.
  60. void SetCompressionAlgorithm(grpc_compression_algorithm algorithm);
  61. /// Set the grpclb fallback timeout (in ms) for the channel. If this amount
  62. /// of time has passed but we have not gotten any non-empty \a serverlist from
  63. /// the balancer, we will fall back to use the backend address(es) returned by
  64. /// the resolver.
  65. void SetGrpclbFallbackTimeout(int fallback_timeout);
  66. /// Set a mutator for the underlying socket.
  67. void SetSocketMutator(grpc_socket_mutator* mutator);
  68. /// Set the string to prepend to the user agent.
  69. void SetUserAgentPrefix(const std::string& user_agent_prefix);
  70. /// Set the buffer pool to be attached to the constructed channel.
  71. void SetResourceQuota(const grpc::ResourceQuota& resource_quota);
  72. /// Set the max receive and send message sizes.
  73. void SetMaxReceiveMessageSize(int size);
  74. void SetMaxSendMessageSize(int size);
  75. /// Set LB policy name.
  76. /// Note that if the name resolver returns only balancer addresses, the
  77. /// grpclb LB policy will be used, regardless of what is specified here.
  78. void SetLoadBalancingPolicyName(const std::string& lb_policy_name);
  79. /// Set service config in JSON form.
  80. /// Primarily meant for use in unit tests.
  81. void SetServiceConfigJSON(const std::string& service_config_json);
  82. // Generic channel argument setter. Only for advanced use cases.
  83. /// Set an integer argument \a value under \a key.
  84. void SetInt(const std::string& key, int value);
  85. // Generic channel argument setter. Only for advanced use cases.
  86. /// Set a pointer argument \a value under \a key. Ownership is not
  87. /// transferred.
  88. void SetPointer(const std::string& key, void* value);
  89. /// Set a pointer argument \a value under \a key, transferring ownership of
  90. /// \a value to the \a ChannelArguments object. The \a vtable::Delete function
  91. /// is responsible for \a value cleanup/destruction when called.
  92. void SetPointerWithVtable(const std::string& key, void* value, const grpc_arg_pointer_vtable* vtable);
  93. /// Set a textual argument \a value under \a key.
  94. void SetString(const std::string& key, const std::string& 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. {
  99. grpc_channel_args out;
  100. out.num_args = args_.size();
  101. out.args = args_.empty() ? nullptr : const_cast<grpc_arg*>(&args_[0]);
  102. return out;
  103. }
  104. private:
  105. friend class grpc::SecureChannelCredentials;
  106. friend class grpc::testing::ChannelArgumentsTest;
  107. /// Default pointer argument operations.
  108. struct PointerVtableMembers
  109. {
  110. static void* Copy(void* in)
  111. {
  112. return in;
  113. }
  114. static void Destroy(void* /*in*/)
  115. {
  116. }
  117. static int Compare(void* a, void* b)
  118. {
  119. if (a < b)
  120. return -1;
  121. if (a > b)
  122. return 1;
  123. return 0;
  124. }
  125. };
  126. // Returns empty string when it is not set.
  127. std::string GetSslTargetNameOverride() const;
  128. std::vector<grpc_arg> args_;
  129. std::list<std::string> strings_;
  130. };
  131. } // namespace grpc
  132. #endif // GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H