GRPC C++  1.20.0
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 
52 namespace experimental {
53 class CallbackGenericService;
54 } // namespace experimental
55 
58  public:
59  ServerBuilder();
60  virtual ~ServerBuilder();
61 
63  // Primary API's
64 
73  virtual std::unique_ptr<Server> BuildAndStart();
74 
79  ServerBuilder& RegisterService(Service* service);
80 
96  ServerBuilder& AddListeningPort(const grpc::string& addr_uri,
97  std::shared_ptr<ServerCredentials> creds,
98  int* selected_port = nullptr);
99 
130  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
131  bool is_frequently_polled = true);
132 
134  // Less commonly used RegisterService variants
135 
140  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
141 
146  ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
147 
149  // Fine control knobs
150 
153  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
154  max_receive_message_size_ = max_receive_message_size;
155  return *this;
156  }
157 
160  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
161  max_send_message_size_ = max_send_message_size;
162  return *this;
163  }
164 
166  ServerBuilder& SetMaxMessageSize(int max_message_size) {
167  return SetMaxReceiveMessageSize(max_message_size);
168  }
169 
175  ServerBuilder& SetCompressionAlgorithmSupportStatus(
176  grpc_compression_algorithm algorithm, bool enabled);
177 
180  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
181 
185  ServerBuilder& SetDefaultCompressionAlgorithm(
186  grpc_compression_algorithm algorithm);
187 
189  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
190 
191  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
192 
198  CQ_TIMEOUT_MSEC
199  };
200 
202  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
203 
206  template <class T>
207  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
208  return SetOption(MakeChannelArgumentOption(arg, value));
209  }
210 
212  static void InternalAddPluginFactory(
213  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
214 
218  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
219 
224  public:
225  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
226 
228  std::vector<
229  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
230  interceptor_creators) {
231  builder_->interceptor_creators_ = std::move(interceptor_creators);
232  }
233 
238  ServerBuilder& RegisterCallbackGenericService(
240 
241  private:
242  ServerBuilder* builder_;
243  };
244 
249 
250  protected:
252  struct Port {
254  std::shared_ptr<ServerCredentials> creds;
256  };
257 
259  typedef std::unique_ptr<grpc::string> HostString;
260  struct NamedService {
261  explicit NamedService(Service* s) : service(s) {}
263  : host(new grpc::string(h)), service(s) {}
264  HostString host;
266  };
267 
269  std::vector<Port> ports() { return ports_; }
270 
272  std::vector<NamedService*> services() {
273  std::vector<NamedService*> service_refs;
274  for (auto& ptr : services_) {
275  service_refs.push_back(ptr.get());
276  }
277  return service_refs;
278  }
279 
281  std::vector<ServerBuilderOption*> options() {
282  std::vector<ServerBuilderOption*> option_refs;
283  for (auto& ptr : options_) {
284  option_refs.push_back(ptr.get());
285  }
286  return option_refs;
287  }
288 
289  private:
290  friend class ::grpc::testing::ServerBuilderPluginTest;
291 
292  struct SyncServerSettings {
293  SyncServerSettings()
294  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
295 
297  int num_cqs;
298 
301  int min_pollers;
302 
305  int max_pollers;
306 
308  int cq_timeout_msec;
309  };
310 
311  int max_receive_message_size_;
312  int max_send_message_size_;
313  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
314  std::vector<std::unique_ptr<NamedService>> services_;
315  std::vector<Port> ports_;
316 
317  SyncServerSettings sync_server_settings_;
318 
320  std::vector<ServerCompletionQueue*> cqs_;
321 
322  std::shared_ptr<ServerCredentials> creds_;
323  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
324  grpc_resource_quota* resource_quota_;
325  AsyncGenericService* generic_service_{nullptr};
326  experimental::CallbackGenericService* callback_generic_service_{nullptr};
327  struct {
328  bool is_set;
330  } maybe_default_compression_level_;
331  struct {
332  bool is_set;
334  } maybe_default_compression_algorithm_;
335  uint32_t enabled_compression_algorithms_bitset_;
336  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
337  interceptor_creators_;
338 };
339 
340 } // namespace grpc
341 
342 #endif // GRPCPP_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:166
int * selected_port
Definition: server_builder.h:255
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:272
Maximum number of polling threads.
Definition: server_builder.h:197
std::string string
Definition: config.h:35
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:254
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:248
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:259
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
Experimental, to be deprecated.
Definition: server_builder.h:252
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:328
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:72
HostString host
Definition: server_builder.h:264
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder.h:223
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:160
Number of completion queues.
Definition: server_builder.h:195
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:269
void SetInterceptorCreators(std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder.h:227
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:225
grpc_workaround_list
Definition: workaround_list.h:26
NamedService(Service *s)
Definition: server_builder.h:261
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:153
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:125
grpc_compression_level level
Definition: server_builder.h:329
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:194
grpc::string addr
Definition: server_builder.h:253
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:649
Definition: server_builder.h:260
NamedService(const grpc::string &h, Service *s)
Definition: server_builder.h:262
Minimum number of polling threads.
Definition: server_builder.h:196
Service * service
Definition: server_builder.h:265
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:207
std::vector< ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:281
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:57
grpc_compression_algorithm algorithm
Definition: server_builder.h:333