• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #ifndef GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H
20 #define GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H
21 
22 #include <list>
23 #include <vector>
24 
25 #include <grpc/compression.h>
26 #include <grpc/grpc.h>
27 #include <grpcpp/support/config.h>
28 
29 namespace grpc {
30 namespace testing {
31 class ChannelArgumentsTest;
32 }  // namespace testing
33 
34 class ResourceQuota;
35 
36 /// Options for channel creation. The user can use generic setters to pass
37 /// key value pairs down to C channel creation code. For gRPC related options,
38 /// concrete setters are provided.
39 class ChannelArguments {
40  public:
41   ChannelArguments();
42   ~ChannelArguments();
43 
44   ChannelArguments(const ChannelArguments& other);
45   ChannelArguments& operator=(ChannelArguments other) {
46     Swap(other);
47     return *this;
48   }
49 
50   void Swap(ChannelArguments& other);
51 
52   /// Dump arguments in this instance to \a channel_args. Does not take
53   /// ownership of \a channel_args.
54   ///
55   /// Note that the underlying arguments are shared. Changes made to either \a
56   /// channel_args or this instance would be reflected on both.
57   void SetChannelArgs(grpc_channel_args* channel_args) const;
58 
59   // gRPC specific channel argument setters
60   /// Set target name override for SSL host name checking. This option is for
61   /// testing only and should never be used in production.
62   void SetSslTargetNameOverride(const grpc::string& name);
63   // TODO(yangg) add flow control options
64   /// Set the compression algorithm for the channel.
65   void SetCompressionAlgorithm(grpc_compression_algorithm algorithm);
66 
67   /// Set the grpclb fallback timeout (in ms) for the channel. If this amount
68   /// of time has passed but we have not gotten any non-empty \a serverlist from
69   /// the balancer, we will fall back to use the backend address(es) returned by
70   /// the resolver.
71   void SetGrpclbFallbackTimeout(int fallback_timeout);
72 
73   /// For client channel's, the socket mutator operates on
74   /// "channel" sockets. For server's, the socket mutator operates
75   /// only on "listen" sockets.
76   /// TODO(apolcyn): allow socket mutators to also operate
77   /// on server "channel" sockets, and adjust the socket mutator
78   /// object to be more speficic about which type of socket
79   /// it should operate on.
80   void SetSocketMutator(grpc_socket_mutator* mutator);
81 
82   /// Set the string to prepend to the user agent.
83   void SetUserAgentPrefix(const grpc::string& user_agent_prefix);
84 
85   /// Set the buffer pool to be attached to the constructed channel.
86   void SetResourceQuota(const ResourceQuota& resource_quota);
87 
88   /// Set the max receive and send message sizes.
89   void SetMaxReceiveMessageSize(int size);
90   void SetMaxSendMessageSize(int size);
91 
92   /// Set LB policy name.
93   /// Note that if the name resolver returns only balancer addresses, the
94   /// grpclb LB policy will be used, regardless of what is specified here.
95   void SetLoadBalancingPolicyName(const grpc::string& lb_policy_name);
96 
97   /// Set service config in JSON form.
98   /// Primarily meant for use in unit tests.
99   void SetServiceConfigJSON(const grpc::string& service_config_json);
100 
101   // Generic channel argument setters. Only for advanced use cases.
102   /// Set an integer argument \a value under \a key.
103   void SetInt(const grpc::string& key, int value);
104 
105   // Generic channel argument setter. Only for advanced use cases.
106   /// Set a pointer argument \a value under \a key. Owership is not transferred.
107   void SetPointer(const grpc::string& key, void* value);
108 
109   void SetPointerWithVtable(const grpc::string& key, void* value,
110                             const grpc_arg_pointer_vtable* vtable);
111 
112   /// Set a textual argument \a value under \a key.
113   void SetString(const grpc::string& key, const grpc::string& value);
114 
115   /// Return (by value) a C \a grpc_channel_args structure which points to
116   /// arguments owned by this \a ChannelArguments instance
c_channel_args()117   grpc_channel_args c_channel_args() const {
118     grpc_channel_args out;
119     out.num_args = args_.size();
120     out.args = args_.empty() ? NULL : const_cast<grpc_arg*>(&args_[0]);
121     return out;
122   }
123 
124  private:
125   friend class SecureChannelCredentials;
126   friend class testing::ChannelArgumentsTest;
127 
128   /// Default pointer argument operations.
129   struct PointerVtableMembers {
CopyPointerVtableMembers130     static void* Copy(void* in) { return in; }
DestroyPointerVtableMembers131     static void Destroy(void* in) {}
ComparePointerVtableMembers132     static int Compare(void* a, void* b) {
133       if (a < b) return -1;
134       if (a > b) return 1;
135       return 0;
136     }
137   };
138 
139   // Returns empty string when it is not set.
140   grpc::string GetSslTargetNameOverride() const;
141 
142   std::vector<grpc_arg> args_;
143   std::list<grpc::string> strings_;
144 };
145 
146 }  // namespace grpc
147 
148 #endif  // GRPCPP_SUPPORT_CHANNEL_ARGUMENTS_H
149