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++/" + 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 (auto a = other.args_.begin(); a != other.args_.end(); ++a) {
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 grpc_core::ExecCtx exec_ctx;
70 for (auto it = args_.begin(); it != args_.end(); ++it) {
71 if (it->type == GRPC_ARG_POINTER) {
72 it->value.pointer.vtable->destroy(it->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 it = args_.begin(); it != args_.end(); ++it) {
99 if (it->type == mutator_arg.type &&
100 grpc::string(it->key) == grpc::string(mutator_arg.key)) {
101 GPR_ASSERT(!replaced);
102 it->value.pointer.vtable->destroy(it->value.pointer.p);
103 it->value.pointer = mutator_arg.value.pointer;
109 strings_.push_back(grpc::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 grpc::string& user_agent_prefix) {
121 if (user_agent_prefix.empty()) {
124 bool replaced = false;
125 auto strings_it = strings_.begin();
126 for (auto it = args_.begin(); it != args_.end(); ++it) {
127 const grpc_arg& arg = *it;
129 if (arg.type == GRPC_ARG_STRING) {
130 if (grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
131 GPR_ASSERT(arg.value.string == strings_it->c_str());
132 *(strings_it) = user_agent_prefix + " " + arg.value.string;
133 it->value.string = const_cast<char*>(strings_it->c_str());
141 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
145 void ChannelArguments::SetResourceQuota(
146 const grpc::ResourceQuota& resource_quota) {
147 SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA,
148 resource_quota.c_resource_quota(),
149 grpc_resource_quota_arg_vtable());
152 void ChannelArguments::SetMaxReceiveMessageSize(int size) {
153 SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, size);
156 void ChannelArguments::SetMaxSendMessageSize(int size) {
157 SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, size);
160 void ChannelArguments::SetLoadBalancingPolicyName(
161 const grpc::string& lb_policy_name) {
162 SetString(GRPC_ARG_LB_POLICY_NAME, lb_policy_name);
165 void ChannelArguments::SetServiceConfigJSON(
166 const grpc::string& service_config_json) {
167 SetString(GRPC_ARG_SERVICE_CONFIG, service_config_json);
170 void ChannelArguments::SetInt(const grpc::string& key, int value) {
172 arg.type = GRPC_ARG_INTEGER;
173 strings_.push_back(key);
174 arg.key = const_cast<char*>(strings_.back().c_str());
175 arg.value.integer = value;
177 args_.push_back(arg);
180 void ChannelArguments::SetPointer(const grpc::string& key, void* value) {
181 static const grpc_arg_pointer_vtable vtable = {
182 &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
183 &PointerVtableMembers::Compare};
184 SetPointerWithVtable(key, value, &vtable);
187 void ChannelArguments::SetPointerWithVtable(
188 const grpc::string& key, void* value,
189 const grpc_arg_pointer_vtable* vtable) {
191 arg.type = GRPC_ARG_POINTER;
192 strings_.push_back(key);
193 arg.key = const_cast<char*>(strings_.back().c_str());
194 arg.value.pointer.p = vtable->copy(value);
195 arg.value.pointer.vtable = vtable;
196 args_.push_back(arg);
199 void ChannelArguments::SetString(const grpc::string& key,
200 const grpc::string& value) {
202 arg.type = GRPC_ARG_STRING;
203 strings_.push_back(key);
204 arg.key = const_cast<char*>(strings_.back().c_str());
205 strings_.push_back(value);
206 arg.value.string = const_cast<char*>(strings_.back().c_str());
208 args_.push_back(arg);
211 void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
212 channel_args->num_args = args_.size();
213 if (channel_args->num_args > 0) {
214 channel_args->args = const_cast<grpc_arg*>(&args_[0]);