GRPC C++  1.13.0-dev
server_builder.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016 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_SERVER_BUILDER_H
20 #define GRPCPP_SERVER_BUILDER_H
21 
22 #include <climits>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
27 #include <grpc/compression.h>
28 #include <grpc/support/cpu.h>
33 #include <grpcpp/support/config.h>
34 
35 struct grpc_resource_quota;
36 
37 namespace grpc {
38 
39 class AsyncGenericService;
40 class ResourceQuota;
41 class CompletionQueue;
42 class Server;
43 class ServerCompletionQueue;
44 class ServerCredentials;
45 class Service;
46 
47 namespace testing {
48 class ServerBuilderPluginTest;
49 } // namespace testing
50 
53  public:
54  ServerBuilder();
55  virtual ~ServerBuilder();
56 
58  // Primary API's
59 
68  virtual std::unique_ptr<Server> BuildAndStart();
69 
74  ServerBuilder& RegisterService(Service* service);
75 
91  ServerBuilder& AddListeningPort(const grpc::string& addr_uri,
92  std::shared_ptr<ServerCredentials> creds,
93  int* selected_port = nullptr);
94 
125  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
126  bool is_frequently_polled = true);
127 
129  // Less commonly used RegisterService variants
130 
135  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
136 
141  ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
142 
144  // Fine control knobs
145 
147  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
148  max_receive_message_size_ = max_receive_message_size;
149  return *this;
150  }
151 
153  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
154  max_send_message_size_ = max_send_message_size;
155  return *this;
156  }
157 
159  ServerBuilder& SetMaxMessageSize(int max_message_size) {
160  return SetMaxReceiveMessageSize(max_message_size);
161  }
162 
168  ServerBuilder& SetCompressionAlgorithmSupportStatus(
169  grpc_compression_algorithm algorithm, bool enabled);
170 
173  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
174 
178  ServerBuilder& SetDefaultCompressionAlgorithm(
179  grpc_compression_algorithm algorithm);
180 
182  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
183 
184  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
185 
191  CQ_TIMEOUT_MSEC
192  };
193 
195  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
196 
199  template <class T>
200  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
201  return SetOption(MakeChannelArgumentOption(arg, value));
202  }
203 
205  static void InternalAddPluginFactory(
206  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
207 
211  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
212 
213  protected:
215  struct Port {
217  std::shared_ptr<ServerCredentials> creds;
219  };
220 
222  typedef std::unique_ptr<grpc::string> HostString;
223  struct NamedService {
224  explicit NamedService(Service* s) : service(s) {}
226  : host(new grpc::string(h)), service(s) {}
227  HostString host;
229  };
230 
232  std::vector<Port> ports() { return ports_; }
233 
235  std::vector<NamedService*> services() {
236  std::vector<NamedService*> service_refs;
237  for (auto& ptr : services_) {
238  service_refs.push_back(ptr.get());
239  }
240  return service_refs;
241  }
242 
244  std::vector<ServerBuilderOption*> options() {
245  std::vector<ServerBuilderOption*> option_refs;
246  for (auto& ptr : options_) {
247  option_refs.push_back(ptr.get());
248  }
249  return option_refs;
250  }
251 
252  private:
253  friend class ::grpc::testing::ServerBuilderPluginTest;
254 
255  struct SyncServerSettings {
256  SyncServerSettings()
257  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
258 
260  int num_cqs;
261 
264  int min_pollers;
265 
268  int max_pollers;
269 
271  int cq_timeout_msec;
272  };
273 
274  int max_receive_message_size_;
275  int max_send_message_size_;
276  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
277  std::vector<std::unique_ptr<NamedService>> services_;
278  std::vector<Port> ports_;
279 
280  SyncServerSettings sync_server_settings_;
281 
283  std::vector<ServerCompletionQueue*> cqs_;
284 
285  std::shared_ptr<ServerCredentials> creds_;
286  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
287  grpc_resource_quota* resource_quota_;
288  AsyncGenericService* generic_service_;
289  struct {
290  bool is_set;
292  } maybe_default_compression_level_;
293  struct {
294  bool is_set;
296  } maybe_default_compression_algorithm_;
297  uint32_t enabled_compression_algorithms_bitset_;
298 };
299 
300 } // namespace grpc
301 
302 #endif // GRPCPP_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:159
int * selected_port
Definition: server_builder.h:218
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:235
Maximum number of polling threads.
Definition: server_builder.h:190
std::string string
Definition: config.h:35
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:217
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:222
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
Experimental, to be deprecated.
Definition: server_builder.h:215
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
ResourceQuota represents a bound on memory usage by the gRPC library.
Definition: resource_quota.h:34
bool is_set
Definition: server_builder.h:290
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:70
Definition: async_generic_service.h:62
HostString host
Definition: server_builder.h:227
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:153
Number of completion queues.
Definition: server_builder.h:188
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:232
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
grpc_workaround_list
Definition: workaround_list.h:26
NamedService(Service *s)
Definition: server_builder.h:224
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:147
grpc_compression_level level
Definition: server_builder.h:291
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:187
grpc::string addr
Definition: server_builder.h:216
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:614
Definition: server_builder.h:223
NamedService(const grpc::string &h, Service *s)
Definition: server_builder.h:225
Minimum number of polling threads.
Definition: server_builder.h:189
Service * service
Definition: server_builder.h:228
ServerBuilder & AddChannelArgument(const grpc::string &arg, const T &value)
Add a channel argument (an escape hatch to tuning core library parameters directly) ...
Definition: server_builder.h:200
std::vector< ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:244
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:52
grpc_compression_algorithm algorithm
Definition: server_builder.h:295