Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: third_party/grpc/src/cpp/common/channel_arguments.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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
35 #include <sstream>
36
37 #include <grpc/impl/codegen/grpc_types.h>
38 #include <grpc/support/log.h>
39 #include "src/core/channel/channel_args.h"
40
41 namespace grpc {
42
43 ChannelArguments::ChannelArguments() {
44 std::ostringstream user_agent_prefix;
45 user_agent_prefix << "grpc-c++/" << grpc_version_string();
46 // This will be ignored if used on the server side.
47 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix.str());
48 }
49
50 ChannelArguments::ChannelArguments(const ChannelArguments& other)
51 : strings_(other.strings_) {
52 args_.reserve(other.args_.size());
53 auto list_it_dst = strings_.begin();
54 auto list_it_src = other.strings_.begin();
55 for (auto a = other.args_.begin(); a != other.args_.end(); ++a) {
56 grpc_arg ap;
57 ap.type = a->type;
58 GPR_ASSERT(list_it_src->c_str() == a->key);
59 ap.key = const_cast<char*>(list_it_dst->c_str());
60 ++list_it_src;
61 ++list_it_dst;
62 switch (a->type) {
63 case GRPC_ARG_INTEGER:
64 ap.value.integer = a->value.integer;
65 break;
66 case GRPC_ARG_STRING:
67 GPR_ASSERT(list_it_src->c_str() == a->value.string);
68 ap.value.string = const_cast<char*>(list_it_dst->c_str());
69 ++list_it_src;
70 ++list_it_dst;
71 break;
72 case GRPC_ARG_POINTER:
73 ap.value.pointer = a->value.pointer;
74 ap.value.pointer.p = a->value.pointer.vtable->copy(ap.value.pointer.p);
75 break;
76 }
77 args_.push_back(ap);
78 }
79 }
80
81 void ChannelArguments::Swap(ChannelArguments& other) {
82 args_.swap(other.args_);
83 strings_.swap(other.strings_);
84 }
85
86 void ChannelArguments::SetCompressionAlgorithm(
87 grpc_compression_algorithm algorithm) {
88 SetInt(GRPC_COMPRESSION_ALGORITHM_ARG, algorithm);
89 }
90
91 // Note: a second call to this will add in front the result of the first call.
92 // An example is calling this on a copy of ChannelArguments which already has a
93 // prefix. The user can build up a prefix string by calling this multiple times,
94 // each with more significant identifier.
95 void ChannelArguments::SetUserAgentPrefix(
96 const grpc::string& user_agent_prefix) {
97 if (user_agent_prefix.empty()) {
98 return;
99 }
100 bool replaced = false;
101 for (auto it = args_.begin(); it != args_.end(); ++it) {
102 const grpc_arg& arg = *it;
103 if (arg.type == GRPC_ARG_STRING &&
104 grpc::string(arg.key) == GRPC_ARG_PRIMARY_USER_AGENT_STRING) {
105 strings_.push_back(user_agent_prefix + " " + arg.value.string);
106 it->value.string = const_cast<char*>(strings_.back().c_str());
107 replaced = true;
108 break;
109 }
110 }
111 if (!replaced) {
112 SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING, user_agent_prefix);
113 }
114 }
115
116 void ChannelArguments::SetInt(const grpc::string& key, int value) {
117 grpc_arg arg;
118 arg.type = GRPC_ARG_INTEGER;
119 strings_.push_back(key);
120 arg.key = const_cast<char*>(strings_.back().c_str());
121 arg.value.integer = value;
122
123 args_.push_back(arg);
124 }
125
126 void ChannelArguments::SetPointer(const grpc::string& key, void* value) {
127 static const grpc_arg_pointer_vtable vtable = {
128 &PointerVtableMembers::Copy, &PointerVtableMembers::Destroy,
129 &PointerVtableMembers::Compare};
130 grpc_arg arg;
131 arg.type = GRPC_ARG_POINTER;
132 strings_.push_back(key);
133 arg.key = const_cast<char*>(strings_.back().c_str());
134 arg.value.pointer.p = value;
135 arg.value.pointer.vtable = &vtable;
136 args_.push_back(arg);
137 }
138
139 void ChannelArguments::SetString(const grpc::string& key,
140 const grpc::string& value) {
141 grpc_arg arg;
142 arg.type = GRPC_ARG_STRING;
143 strings_.push_back(key);
144 arg.key = const_cast<char*>(strings_.back().c_str());
145 strings_.push_back(value);
146 arg.value.string = const_cast<char*>(strings_.back().c_str());
147
148 args_.push_back(arg);
149 }
150
151 void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
152 channel_args->num_args = args_.size();
153 if (channel_args->num_args > 0) {
154 channel_args->args = const_cast<grpc_arg*>(&args_[0]);
155 }
156 }
157
158 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/src/cpp/common/call.cc ('k') | third_party/grpc/src/cpp/common/completion_queue.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698