3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <grpcpp/support/channel_arguments.h>
22 #include <grpc/impl/codegen/grpc_types.h>
23 #include <grpc/support/log.h>
24 #include <grpcpp/grpcpp.h>
25 #include <grpcpp/resource_quota.h>
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "src/core/lib/iomgr/socket_mutator.h"
32 ChannelArguments::ChannelArguments() {
33 // This will be ignored if used on the server side.
34 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, "grpc-c++/" + grpc::Version());
37 ChannelArguments::ChannelArguments(const ChannelArguments& other)
38 : strings_(other.strings_) {
39 args_.reserve(other.args_.size());
40 auto list_it_dst = strings_.begin();
41 auto list_it_src = other.strings_.begin();
42 for (const auto& a : other.args_) {
45 GPR_ASSERT(list_it_src->c_str() == a.key);
46 ap.key = const_cast<char*>(list_it_dst->c_str());
50 case GRPC_ARG_INTEGER:
51 ap.value.integer = a.value.integer;
54 GPR_ASSERT(list_it_src->c_str() == a.value.string);
55 ap.value.string = const_cast<char*>(list_it_dst->c_str());
59 case GRPC_ARG_POINTER:
60 ap.value.pointer = a.value.pointer;
61 ap.value.pointer.p = a.value.pointer.vtable->copy(ap.value.pointer.p);
68 ChannelArguments::~ChannelArguments() {
69 for (auto& arg : args_) {
70 if (arg.type == GRPC_ARG_POINTER) {
71 grpc_core::ExecCtx exec_ctx;
72 arg.value.pointer.vtable->destroy(arg.value.pointer.p);
77 void ChannelArguments::Swap(ChannelArguments& other) {
78 args_.swap(other.args_);
79 strings_.swap(other.strings_);
82 void ChannelArguments::SetCompressionAlgorithm(
83 grpc_compression_algorithm algorithm) {
84 SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm);
87 void ChannelArguments::SetGrpclbFallbackTimeout(int fallback_timeout) {
88 SetInt(GRPC_ARG_GRPCLB_FALLBACK_TIMEOUT_MS, fallback_timeout);
91 void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) {
95 grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
96 bool replaced = false;
97 grpc_core::ExecCtx exec_ctx;
98 for (auto& arg : args_) {
99 if (arg.type == mutator_arg.type &&
100 std::string(arg.key) == std::string(mutator_arg.key)) {
101 GPR_ASSERT(!replaced);
102 arg.value.pointer.vtable->destroy(arg.value.pointer.p);
103 arg.value.pointer = mutator_arg.value.pointer;
109 strings_.push_back(std::string(mutator_arg.key));
110 args_.push_back(mutator_arg);
111 args_.back().key = const_cast<char*>(strings_.back().c_str());
115 // Note: a second call to this will add in front the result of the first call.
116 // An example is calling this on a copy of ChannelArguments which already has a
117 // prefix. The user can build up a prefix string by calling this multiple times,
118 // each with more significant identifier.
119 void ChannelArguments::SetUserAgentPrefix(
120 const std::string& user_agent_prefix) {
121 if (user_agent_prefix.empty()) {
124 bool replaced = false;
125 auto strings_it = strings_.begin();
126 for (auto& arg : args_) {
128 if (arg.type == GRPC_ARG_STRING) {
129 if (std::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
130 GPR_ASSERT(arg.value.string == strings_it->c_str());
131 *(strings_it) = user_agent_prefix + " " + arg.value.string;
132 arg.value.string = const_cast<char*>(strings_it->c_str());
140 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
144 void ChannelArguments::SetResourceQuota(
145 const grpc::ResourceQuota& resource_quota) {
146 SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA,
147 resource_quota.c_resource_quota(),
148 grpc_resource_quota_arg_vtable());
151 void ChannelArguments::SetMaxReceiveMessageSize(int size) {
152 SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, size);
155 void ChannelArguments::SetMaxSendMessageSize(int size) {
156 SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, size);
159 void ChannelArguments::SetLoadBalancingPolicyName(
160 const std::string& lb_policy_name) {
161 SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
164 void ChannelArguments::SetServiceConfigJSON(
165 const std::string& service_config_json) {
166 SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
169 void ChannelArguments::SetInt(const std::string& key, int value) {
171 arg.type = GRPC_ARG_INTEGER;
172 strings_.push_back(key);
173 arg.key = const_cast<char*>(strings_.back().c_str());
174 arg.value.integer = value;
176 args_.push_back(arg);
179 void ChannelArguments::SetPointer(const std::string& key, void* value) {
180 static const grpc_arg_pointer_vtable vtable = {
181 &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
182 &PointerVtableMembers::Compare};
183 SetPointerWithVtable(key, value, &vtable);
186 void ChannelArguments::SetPointerWithVtable(
187 const std::string& key, void* value,
188 const grpc_arg_pointer_vtable* vtable) {
190 arg.type = GRPC_ARG_POINTER;
191 strings_.push_back(key);
192 arg.key = const_cast<char*>(strings_.back().c_str());
193 arg.value.pointer.p = vtable->copy(value);
194 arg.value.pointer.vtable = vtable;
195 args_.push_back(arg);
198 void ChannelArguments::SetString(const std::string& key,
199 const std::string& value) {
201 arg.type = GRPC_ARG_STRING;
202 strings_.push_back(key);
203 arg.key = const_cast<char*>(strings_.back().c_str());
204 strings_.push_back(value);
205 arg.value.string = const_cast<char*>(strings_.back().c_str());
207 args_.push_back(arg);
210 void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
211 channel_args->num_args = args_.size();
212 if (channel_args->num_args > 0) {
213 channel_args->args = const_cast<grpc_arg*>(&args_[0]);