channel_arguments.cc
Go to the documentation of this file.
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 #include <algorithm>
19 #include <list>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
26 #include <grpc/support/log.h>
27 #include <grpcpp/grpcpp.h>
28 #include <grpcpp/resource_quota.h>
30 #include <grpcpp/support/config.h>
31 
34 
35 namespace grpc {
36 
38  // This will be ignored if used on the server side.
40 }
41 
43  : strings_(other.strings_) {
44  args_.reserve(other.args_.size());
45  auto list_it_dst = strings_.begin();
46  auto list_it_src = other.strings_.begin();
47  for (const auto& a : other.args_) {
48  grpc_arg ap;
49  ap.type = a.type;
50  GPR_ASSERT(list_it_src->c_str() == a.key);
51  ap.key = const_cast<char*>(list_it_dst->c_str());
52  ++list_it_src;
53  ++list_it_dst;
54  switch (a.type) {
55  case GRPC_ARG_INTEGER:
56  ap.value.integer = a.value.integer;
57  break;
58  case GRPC_ARG_STRING:
59  GPR_ASSERT(list_it_src->c_str() == a.value.string);
60  ap.value.string = const_cast<char*>(list_it_dst->c_str());
61  ++list_it_src;
62  ++list_it_dst;
63  break;
64  case GRPC_ARG_POINTER:
65  ap.value.pointer = a.value.pointer;
66  ap.value.pointer.p = a.value.pointer.vtable->copy(ap.value.pointer.p);
67  break;
68  }
69  args_.push_back(ap);
70  }
71 }
72 
74  for (auto& arg : args_) {
75  if (arg.type == GRPC_ARG_POINTER) {
77  arg.value.pointer.vtable->destroy(arg.value.pointer.p);
78  }
79  }
80 }
81 
83  args_.swap(other.args_);
84  strings_.swap(other.strings_);
85 }
86 
88  grpc_compression_algorithm algorithm) {
90 }
91 
92 void ChannelArguments::SetGrpclbFallbackTimeout(int fallback_timeout) {
94 }
95 
97  if (!mutator) {
98  return;
99  }
100  grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
101  bool replaced = false;
103  for (auto& arg : args_) {
104  if (arg.type == mutator_arg.type &&
105  std::string(arg.key) == std::string(mutator_arg.key)) {
106  GPR_ASSERT(!replaced);
107  arg.value.pointer.vtable->destroy(arg.value.pointer.p);
108  arg.value.pointer = mutator_arg.value.pointer;
109  replaced = true;
110  }
111  }
112 
113  if (!replaced) {
114  strings_.push_back(std::string(mutator_arg.key));
115  args_.push_back(mutator_arg);
116  args_.back().key = const_cast<char*>(strings_.back().c_str());
117  }
118 }
119 
120 // Note: a second call to this will add in front the result of the first call.
121 // An example is calling this on a copy of ChannelArguments which already has a
122 // prefix. The user can build up a prefix string by calling this multiple times,
123 // each with more significant identifier.
125  const std::string& user_agent_prefix) {
126  if (user_agent_prefix.empty()) {
127  return;
128  }
129  bool replaced = false;
130  auto strings_it = strings_.begin();
131  for (auto& arg : args_) {
132  ++strings_it;
133  if (arg.type == GRPC_ARG_STRING) {
135  GPR_ASSERT(arg.value.string == strings_it->c_str());
136  *(strings_it) = user_agent_prefix + " " + arg.value.string;
137  arg.value.string = const_cast<char*>(strings_it->c_str());
138  replaced = true;
139  break;
140  }
141  ++strings_it;
142  }
143  }
144  if (!replaced) {
145  SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
146  }
147 }
148 
152  resource_quota.c_resource_quota(),
154 }
155 
158 }
159 
162 }
163 
165  const std::string& lb_policy_name) {
166  SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
167 }
168 
170  const std::string& service_config_json) {
171  SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
172 }
173 
175  grpc_arg arg;
177  strings_.push_back(key);
178  arg.key = const_cast<char*>(strings_.back().c_str());
179  arg.value.integer = value;
180 
181  args_.push_back(arg);
182 }
183 
185  static const grpc_arg_pointer_vtable vtable = {
189 }
190 
192  const std::string& key, void* value,
194  grpc_arg arg;
196  strings_.push_back(key);
197  arg.key = const_cast<char*>(strings_.back().c_str());
198  arg.value.pointer.p = vtable->copy(value);
199  arg.value.pointer.vtable = vtable;
200  args_.push_back(arg);
201 }
202 
204  const std::string& value) {
205  grpc_arg arg;
207  strings_.push_back(key);
208  arg.key = const_cast<char*>(strings_.back().c_str());
209  strings_.push_back(value);
210  arg.value.string = const_cast<char*>(strings_.back().c_str());
211 
212  args_.push_back(arg);
213 }
214 
216  channel_args->num_args = args_.size();
217  if (channel_args->num_args > 0) {
218  channel_args->args = const_cast<grpc_arg*>(&args_[0]);
219  }
220 }
221 
222 } // namespace grpc
grpc_arg
Definition: grpc_types.h:103
GRPC_ARG_PRIMARY_USER_AGENT_STRING
#define GRPC_ARG_PRIMARY_USER_AGENT_STRING
Definition: grpc_types.h:254
vtable
static const grpc_transport_vtable vtable
Definition: binder_transport.cc:680
log.h
grpc::ChannelArguments::SetGrpclbFallbackTimeout
void SetGrpclbFallbackTimeout(int fallback_timeout)
Definition: channel_arguments.cc:92
grpc::ChannelArguments::Swap
void Swap(ChannelArguments &other)
Definition: channel_arguments.cc:82
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_arg::value
union grpc_arg::grpc_arg_value value
grpc_arg::grpc_arg_value::pointer
struct grpc_arg::grpc_arg_value::grpc_arg_pointer pointer
grpc
Definition: grpcpp/alarm.h:33
GRPC_ARG_STRING
@ GRPC_ARG_STRING
Definition: grpc_types.h:80
GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH
#define GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH
Definition: grpc_types.h:153
GRPC_ARG_RESOURCE_QUOTA
#define GRPC_ARG_RESOURCE_QUOTA
Definition: grpc_types.h:299
grpc::ChannelArguments::SetChannelArgs
void SetChannelArgs(grpc_channel_args *channel_args) const
Definition: channel_arguments.cc:215
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
grpc::ChannelArguments::SetString
void SetString(const std::string &key, const std::string &value)
Set a textual argument value under key.
Definition: channel_arguments.cc:203
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
arg::value
void * value
Definition: cmdline.cc:44
grpc::ResourceQuota
Definition: include/grpcpp/resource_quota.h:34
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc::Version
std::string Version()
Return gRPC library version.
Definition: version_cc.cc:28
grpc::ChannelArguments::SetSocketMutator
void SetSocketMutator(grpc_socket_mutator *mutator)
Set a mutator for the underlying socket.
Definition: channel_arguments.cc:96
grpc::ChannelArguments::SetCompressionAlgorithm
void SetCompressionAlgorithm(grpc_compression_algorithm algorithm)
Set the compression algorithm for the channel.
Definition: channel_arguments.cc:87
GRPC_ARG_MAX_SEND_MESSAGE_LENGTH
#define GRPC_ARG_MAX_SEND_MESSAGE_LENGTH
Definition: grpc_types.h:159
grpc::ChannelArguments::SetUserAgentPrefix
void SetUserAgentPrefix(const std::string &user_agent_prefix)
Set the string to prepend to the user agent.
Definition: channel_arguments.cc:124
grpc::ChannelArguments::PointerVtableMembers::Destroy
static void Destroy(void *)
Definition: grpcpp/support/channel_arguments.h:129
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
resource_quota
ResourceQuotaRefPtr resource_quota
Definition: filter_fuzzer.cc:145
grpc::ChannelArguments::SetMaxReceiveMessageSize
void SetMaxReceiveMessageSize(int size)
Set the max receive and send message sizes.
Definition: channel_arguments.cc:156
grpc_types.h
grpc::ChannelArguments::args_
std::vector< grpc_arg > args_
Definition: grpcpp/support/channel_arguments.h:140
grpc::ChannelArguments::SetMaxSendMessageSize
void SetMaxSendMessageSize(int size)
Definition: channel_arguments.cc:160
grpc::ChannelArguments::~ChannelArguments
~ChannelArguments()
Definition: channel_arguments.cc:73
grpc::ChannelArguments::SetResourceQuota
void SetResourceQuota(const grpc::ResourceQuota &resource_quota)
Set the buffer pool to be attached to the constructed channel.
Definition: channel_arguments.cc:149
arg::type
argtype type
Definition: cmdline.cc:43
grpc_arg::grpc_arg_value::string
char * string
Definition: grpc_types.h:107
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
channel_arguments.h
grpc_resource_quota_arg_vtable
const GRPCAPI grpc_arg_pointer_vtable * grpc_resource_quota_arg_vtable(void)
Definition: api.cc:62
grpc::ChannelArguments::PointerVtableMembers::Copy
static void * Copy(void *in)
Definition: grpcpp/support/channel_arguments.h:128
GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS
#define GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS
Definition: grpc_types.h:370
GRPC_ARG_LB_POLICY_NAME
#define GRPC_ARG_LB_POLICY_NAME
Definition: grpc_types.h:309
grpc_channel_args::num_args
size_t num_args
Definition: grpc_types.h:133
grpcpp.h
arg
Definition: cmdline.cc:40
grpc_arg::grpc_arg_value::grpc_arg_pointer::p
void * p
Definition: grpc_types.h:110
grpc::ChannelArguments::PointerVtableMembers::Compare
static int Compare(void *a, void *b)
Definition: grpcpp/support/channel_arguments.h:130
grpc_core::ExecCtx
Definition: exec_ctx.h:97
config.h
grpc_socket_mutator
Definition: socket_mutator.h:62
compression_types.h
value
const char * value
Definition: hpack_parser_table.cc:165
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
grpc::ChannelArguments::SetPointerWithVtable
void SetPointerWithVtable(const std::string &key, void *value, const grpc_arg_pointer_vtable *vtable)
Definition: channel_arguments.cc:191
key
const char * key
Definition: hpack_parser_table.cc:164
grpc::ChannelArguments::strings_
std::list< std::string > strings_
Definition: grpcpp/support/channel_arguments.h:141
GRPC_ARG_SERVICE_CONFIG
#define GRPC_ARG_SERVICE_CONFIG
Definition: grpc_types.h:304
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc::ChannelArguments::ChannelArguments
ChannelArguments()
Definition: channel_arguments.cc:37
grpc_arg::key
char * key
Definition: grpc_types.h:105
grpc::ChannelArguments::SetLoadBalancingPolicyName
void SetLoadBalancingPolicyName(const std::string &lb_policy_name)
Definition: channel_arguments.cc:164
arg
struct arg arg
exec_ctx.h
grpc::ChannelArguments::SetInt
void SetInt(const std::string &key, int value)
Set an integer argument value under key.
Definition: channel_arguments.cc:174
grpc_socket_mutator_to_arg
grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator)
Definition: socket_mutator.cc:93
resource_quota.h
grpc::ChannelArguments::SetServiceConfigJSON
void SetServiceConfigJSON(const std::string &service_config_json)
Definition: channel_arguments.cc:169
GRPC_ARG_POINTER
@ GRPC_ARG_POINTER
Definition: grpc_types.h:82
grpc_arg::grpc_arg_value::integer
int integer
Definition: grpc_types.h:108
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
#define GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
Definition: compression_types.h:42
grpc_arg::type
grpc_arg_type type
Definition: grpc_types.h:104
socket_mutator.h
grpc_channel_args::args
grpc_arg * args
Definition: grpc_types.h:134
grpc::ChannelArguments::SetPointer
void SetPointer(const std::string &key, void *value)
Definition: channel_arguments.cc:184


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:43