GRPC C++  1.19.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>
34 #include <grpcpp/support/config.h>
35 
36 struct grpc_resource_quota;
37 
38 namespace grpc {
39 
40 class AsyncGenericService;
41 class ResourceQuota;
42 class CompletionQueue;
43 class Server;
44 class ServerCompletionQueue;
45 class ServerCredentials;
46 class Service;
47 
48 namespace testing {
49 class ServerBuilderPluginTest;
50 } // namespace testing
51 
54  public:
55  ServerBuilder();
56  virtual ~ServerBuilder();
57 
59  // Primary API's
60 
69  virtual std::unique_ptr<Server> BuildAndStart();
70 
75  ServerBuilder& RegisterService(Service* service);
76 
92  ServerBuilder& AddListeningPort(const grpc::string& addr_uri,
93  std::shared_ptr<ServerCredentials> creds,
94  int* selected_port = nullptr);
95 
126  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
127  bool is_frequently_polled = true);
128 
130  // Less commonly used RegisterService variants
131 
136  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
137 
142  ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
143 
145  // Fine control knobs
146 
149  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
150  max_receive_message_size_ = max_receive_message_size;
151  return *this;
152  }
153 
156  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
157  max_send_message_size_ = max_send_message_size;
158  return *this;
159  }
160 
162  ServerBuilder& SetMaxMessageSize(int max_message_size) {
163  return SetMaxReceiveMessageSize(max_message_size);
164  }
165 
171  ServerBuilder& SetCompressionAlgorithmSupportStatus(
172  grpc_compression_algorithm algorithm, bool enabled);
173 
176  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
177 
181  ServerBuilder& SetDefaultCompressionAlgorithm(
182  grpc_compression_algorithm algorithm);
183 
185  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
186 
187  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
188 
194  CQ_TIMEOUT_MSEC
195  };
196 
198  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
199 
202  template <class T>
203  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
204  return SetOption(MakeChannelArgumentOption(arg, value));
205  }
206 
208  static void InternalAddPluginFactory(
209  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
210 
214  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
215 
220  public:
221  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
222 
224  std::vector<
225  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
226  interceptor_creators) {
227  builder_->interceptor_creators_ = std::move(interceptor_creators);
228  }
229 
230  private:
231  ServerBuilder* builder_;
232  };
233 
238 
239  protected:
241  struct Port {
243  std::shared_ptr<ServerCredentials> creds;
245  };
246 
248  typedef std::unique_ptr<grpc::string> HostString;
249  struct NamedService {
250  explicit NamedService(Service* s) : service(s) {}
252  : host(new grpc::string(h)), service(s) {}
253  HostString host;
255  };
256 
258  std::vector<Port> ports() { return ports_; }
259 
261  std::vector<NamedService*> services() {
262  std::vector<NamedService*> service_refs;
263  for (auto& ptr : services_) {
264  service_refs.push_back(ptr.get());
265  }
266  return service_refs;
267  }
268 
270  std::vector<ServerBuilderOption*> options() {
271  std::vector<ServerBuilderOption*> option_refs;
272  for (auto& ptr : options_) {
273  option_refs.push_back(ptr.get());
274  }
275  return option_refs;
276  }
277 
278  private:
279  friend class ::grpc::testing::ServerBuilderPluginTest;
280 
281  struct SyncServerSettings {
282  SyncServerSettings()
283  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
284 
286  int num_cqs;
287 
290  int min_pollers;
291 
294  int max_pollers;
295 
297  int cq_timeout_msec;
298  };
299 
300  int max_receive_message_size_;
301  int max_send_message_size_;
302  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
303  std::vector<std::unique_ptr<NamedService>> services_;
304  std::vector<Port> ports_;
305 
306  SyncServerSettings sync_server_settings_;
307 
309  std::vector<ServerCompletionQueue*> cqs_;
310 
311  std::shared_ptr<ServerCredentials> creds_;
312  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
313  grpc_resource_quota* resource_quota_;
314  AsyncGenericService* generic_service_;
315  struct {
316  bool is_set;
318  } maybe_default_compression_level_;
319  struct {
320  bool is_set;
322  } maybe_default_compression_algorithm_;
323  uint32_t enabled_compression_algorithms_bitset_;
324  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
325  interceptor_creators_;
326 };
327 
328 } // namespace grpc
329 
330 #endif // GRPCPP_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:162
int * selected_port
Definition: server_builder.h:244
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:261
Maximum number of polling threads.
Definition: server_builder.h:193
std::string string
Definition: config.h:35
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:243
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:237
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:248
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
Experimental, to be deprecated.
Definition: server_builder.h:241
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota.h:34
bool is_set
Definition: server_builder.h:316
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:71
Definition: async_generic_service.h:65
HostString host
Definition: server_builder.h:253
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder.h:219
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:156
Number of completion queues.
Definition: server_builder.h:191
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:258
void SetInterceptorCreators(std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder.h:223
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
experimental_type(ServerBuilder *builder)
Definition: server_builder.h:221
grpc_workaround_list
Definition: workaround_list.h:26
NamedService(Service *s)
Definition: server_builder.h:250
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:149
grpc_compression_level level
Definition: server_builder.h:317
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:190
grpc::string addr
Definition: server_builder.h:242
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:640
Definition: server_builder.h:249
NamedService(const grpc::string &h, Service *s)
Definition: server_builder.h:251
Minimum number of polling threads.
Definition: server_builder.h:192
Service * service
Definition: server_builder.h:254
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:203
std::vector< ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:270
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:53
grpc_compression_algorithm algorithm
Definition: server_builder.h:321