channel_arguments.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc++/support/channel_arguments.h>
  34. #include <sstream>
  35. #include <grpc++/grpc++.h>
  36. #include <grpc++/resource_quota.h>
  37. #include <grpc/impl/codegen/grpc_types.h>
  38. #include <grpc/support/log.h>
  39. #include "src/core/lib/channel/channel_args.h"
  40. #include "src/core/lib/iomgr/socket_mutator.h"
  41. namespace grpc {
  42. ChannelArguments::ChannelArguments() {
  43. // This will be ignored if used on the server side.
  44. SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, "grpc-c++/" + Version());
  45. }
  46. ChannelArguments::ChannelArguments(const ChannelArguments& other)
  47. : strings_(other.strings_) {
  48. args_.reserve(other.args_.size());
  49. auto list_it_dst = strings_.begin();
  50. auto list_it_src = other.strings_.begin();
  51. for (auto a = other.args_.begin(); a != other.args_.end(); ++a) {
  52. grpc_arg ap;
  53. ap.type = a->type;
  54. GPR_ASSERT(list_it_src->c_str() == a->key);
  55. ap.key = const_cast<char*>(list_it_dst->c_str());
  56. ++list_it_src;
  57. ++list_it_dst;
  58. switch (a->type) {
  59. case GRPC_ARG_INTEGER:
  60. ap.value.integer = a->value.integer;
  61. break;
  62. case GRPC_ARG_STRING:
  63. GPR_ASSERT(list_it_src->c_str() == a->value.string);
  64. ap.value.string = const_cast<char*>(list_it_dst->c_str());
  65. ++list_it_src;
  66. ++list_it_dst;
  67. break;
  68. case GRPC_ARG_POINTER:
  69. ap.value.pointer = a->value.pointer;
  70. ap.value.pointer.p = a->value.pointer.vtable->copy(ap.value.pointer.p);
  71. break;
  72. }
  73. args_.push_back(ap);
  74. }
  75. }
  76. void ChannelArguments::Swap(ChannelArguments& other) {
  77. args_.swap(other.args_);
  78. strings_.swap(other.strings_);
  79. }
  80. void ChannelArguments::SetCompressionAlgorithm(
  81. grpc_compression_algorithm algorithm) {
  82. SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm);
  83. }
  84. void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) {
  85. if (!mutator) {
  86. return;
  87. }
  88. grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
  89. bool replaced = false;
  90. for (auto it = args_.begin(); it != args_.end(); ++it) {
  91. if (it->type == mutator_arg.type &&
  92. grpc::string(it->key) == grpc::string(mutator_arg.key)) {
  93. it->value.pointer.vtable->destroy(it->value.pointer.p);
  94. it->value.pointer = mutator_arg.value.pointer;
  95. }
  96. }
  97. if (!replaced) {
  98. args_.push_back(mutator_arg);
  99. }
  100. }
  101. // Note: a second call to this will add in front the result of the first call.
  102. // An example is calling this on a copy of ChannelArguments which already has a
  103. // prefix. The user can build up a prefix string by calling this multiple times,
  104. // each with more significant identifier.
  105. void ChannelArguments::SetUserAgentPrefix(
  106. const grpc::string& user_agent_prefix) {
  107. if (user_agent_prefix.empty()) {
  108. return;
  109. }
  110. bool replaced = false;
  111. for (auto it = args_.begin(); it != args_.end(); ++it) {
  112. const grpc_arg& arg = *it;
  113. if (arg.type == GRPC_ARG_STRING &&
  114. grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
  115. strings_.push_back(user_agent_prefix + " " + arg.value.string);
  116. it->value.string = const_cast<char*>(strings_.back().c_str());
  117. replaced = true;
  118. break;
  119. }
  120. }
  121. if (!replaced) {
  122. SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
  123. }
  124. }
  125. void ChannelArguments::SetResourceQuota(
  126. const grpc::ResourceQuota& resource_quota) {
  127. SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA,
  128. resource_quota.c_resource_quota(),
  129. grpc_resource_quota_arg_vtable());
  130. }
  131. void ChannelArguments::SetLoadBalancingPolicyName(
  132. const grpc::string& lb_policy_name) {
  133. SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
  134. }
  135. void ChannelArguments::SetServiceConfigJSON(
  136. const grpc::string& service_config_json) {
  137. SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
  138. }
  139. void ChannelArguments::SetInt(const grpc::string& key, int value) {
  140. grpc_arg arg;
  141. arg.type = GRPC_ARG_INTEGER;
  142. strings_.push_back(key);
  143. arg.key = const_cast<char*>(strings_.back().c_str());
  144. arg.value.integer = value;
  145. args_.push_back(arg);
  146. }
  147. void ChannelArguments::SetPointer(const grpc::string& key, void* value) {
  148. static const grpc_arg_pointer_vtable vtable = {
  149. &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
  150. &PointerVtableMembers::Compare};
  151. SetPointerWithVtable(key, value, &vtable);
  152. }
  153. void ChannelArguments::SetPointerWithVtable(
  154. const grpc::string& key, void* value,
  155. const grpc_arg_pointer_vtable* vtable) {
  156. grpc_arg arg;
  157. arg.type = GRPC_ARG_POINTER;
  158. strings_.push_back(key);
  159. arg.key = const_cast<char*>(strings_.back().c_str());
  160. arg.value.pointer.p = value;
  161. arg.value.pointer.vtable = vtable;
  162. args_.push_back(arg);
  163. }
  164. void ChannelArguments::SetString(const grpc::string& key,
  165. const grpc::string& value) {
  166. grpc_arg arg;
  167. arg.type = GRPC_ARG_STRING;
  168. strings_.push_back(key);
  169. arg.key = const_cast<char*>(strings_.back().c_str());
  170. strings_.push_back(value);
  171. arg.value.string = const_cast<char*>(strings_.back().c_str());
  172. args_.push_back(arg);
  173. }
  174. void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
  175. channel_args->num_args = args_.size();
  176. if (channel_args->num_args > 0) {
  177. channel_args->args = const_cast<grpc_arg*>(&args_[0]);
  178. }
  179. }
  180. } // namespace grpc