GRPC C++  1.26.0
server_builder_impl.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_IMPL_H
20 #define GRPCPP_SERVER_BUILDER_IMPL_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/server.h>
35 #include <grpcpp/support/config.h>
36 
37 struct grpc_resource_quota;
38 
39 namespace grpc_impl {
40 
41 class CompletionQueue;
42 class ResourceQuota;
43 class Server;
45 class ServerCredentials;
46 } // namespace grpc_impl
47 
48 namespace grpc {
49 
50 class AsyncGenericService;
51 class Service;
52 namespace testing {
53 class ServerBuilderPluginTest;
54 } // namespace testing
55 
56 namespace internal {
57 class ExternalConnectionAcceptorImpl;
58 } // namespace internal
59 
60 namespace experimental {
61 class CallbackGenericService;
62 
63 // EXPERIMENTAL API:
64 // Interface for a grpc server to build transports with connections created out
65 // of band.
66 // See ServerBuilder's AddExternalConnectionAcceptor API.
68  public:
70  int listener_fd = -1;
71  int fd = -1;
72  ByteBuffer read_buffer; // data intended for the grpc server
73  };
75  // If called before grpc::Server is started or after it is shut down, the new
76  // connection will be closed.
77  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
78 };
79 
80 } // namespace experimental
81 } // namespace grpc
82 
83 namespace grpc_impl {
84 
87  public:
88  ServerBuilder();
89  virtual ~ServerBuilder();
90 
92  // Primary API's
93 
102  virtual std::unique_ptr<grpc::Server> BuildAndStart();
103 
108  ServerBuilder& RegisterService(grpc::Service* service);
109 
125  ServerBuilder& AddListeningPort(
126  const grpc::string& addr_uri,
127  std::shared_ptr<grpc_impl::ServerCredentials> creds,
128  int* selected_port = nullptr);
129 
160  std::unique_ptr<grpc_impl::ServerCompletionQueue> AddCompletionQueue(
161  bool is_frequently_polled = true);
162 
164  // Less commonly used RegisterService variants
165 
170  ServerBuilder& RegisterService(const grpc::string& host,
171  grpc::Service* service);
172 
177  ServerBuilder& RegisterAsyncGenericService(
178  grpc::AsyncGenericService* service);
179 
181  // Fine control knobs
182 
185  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
186  max_receive_message_size_ = max_receive_message_size;
187  return *this;
188  }
189 
192  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
193  max_send_message_size_ = max_send_message_size;
194  return *this;
195  }
196 
198  ServerBuilder& SetMaxMessageSize(int max_message_size) {
199  return SetMaxReceiveMessageSize(max_message_size);
200  }
201 
207  ServerBuilder& SetCompressionAlgorithmSupportStatus(
208  grpc_compression_algorithm algorithm, bool enabled);
209 
212  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
213 
217  ServerBuilder& SetDefaultCompressionAlgorithm(
218  grpc_compression_algorithm algorithm);
219 
221  ServerBuilder& SetResourceQuota(
222  const grpc_impl::ResourceQuota& resource_quota);
223 
224  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
225 
231  CQ_TIMEOUT_MSEC
232  };
233 
235  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
236 
239  template <class T>
240  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
241  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
242  }
243 
245  static void InternalAddPluginFactory(
246  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
247 
251  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
252 
257  public:
259  : builder_(builder) {}
260 
262  std::vector<std::unique_ptr<
264  interceptor_creators) {
265  builder_->interceptor_creators_ = std::move(interceptor_creators);
266  }
267 
272  ServerBuilder& RegisterCallbackGenericService(
274 
276  FROM_FD = 0 // in the form of a file descriptor
277  };
278 
283  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
284  AddExternalConnectionAcceptor(ExternalConnectionType type,
285  std::shared_ptr<ServerCredentials> creds);
286 
287  private:
288  ServerBuilder* builder_;
289  };
290 
295 
296  protected:
298  struct Port {
300  std::shared_ptr<grpc_impl::ServerCredentials> creds;
302  };
303 
305  typedef std::unique_ptr<grpc::string> HostString;
306  struct NamedService {
307  explicit NamedService(grpc::Service* s) : service(s) {}
309  : host(new grpc::string(h)), service(s) {}
310  HostString host;
312  };
313 
315  std::vector<Port> ports() { return ports_; }
316 
318  std::vector<NamedService*> services() {
319  std::vector<NamedService*> service_refs;
320  for (auto& ptr : services_) {
321  service_refs.push_back(ptr.get());
322  }
323  return service_refs;
324  }
325 
327  std::vector<grpc::ServerBuilderOption*> options() {
328  std::vector<grpc::ServerBuilderOption*> option_refs;
329  for (auto& ptr : options_) {
330  option_refs.push_back(ptr.get());
331  }
332  return option_refs;
333  }
334 
335  private:
336  friend class ::grpc::testing::ServerBuilderPluginTest;
337 
338  struct SyncServerSettings {
339  SyncServerSettings()
340  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
341 
343  int num_cqs;
344 
347  int min_pollers;
348 
351  int max_pollers;
352 
354  int cq_timeout_msec;
355  };
356 
357  int max_receive_message_size_;
358  int max_send_message_size_;
359  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
360  std::vector<std::unique_ptr<NamedService>> services_;
361  std::vector<Port> ports_;
362 
363  SyncServerSettings sync_server_settings_;
364 
366  std::vector<grpc_impl::ServerCompletionQueue*> cqs_;
367 
368  std::shared_ptr<grpc_impl::ServerCredentials> creds_;
369  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
370  grpc_resource_quota* resource_quota_;
371  grpc::AsyncGenericService* generic_service_{nullptr};
372  grpc::experimental::CallbackGenericService* callback_generic_service_{
373  nullptr};
374  struct {
375  bool is_set;
377  } maybe_default_compression_level_;
378  struct {
379  bool is_set;
381  } maybe_default_compression_algorithm_;
382  uint32_t enabled_compression_algorithms_bitset_;
383  std::vector<
384  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
385  interceptor_creators_;
386  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
387  acceptors_;
388 };
389 
390 } // namespace grpc_impl
391 
392 #endif // GRPCPP_SERVER_BUILDER_IMPL_H
ExternalConnectionType
Definition: server_builder_impl.h:275
grpc_compression_algorithm algorithm
Definition: server_builder_impl.h:380
int * selected_port
Definition: server_builder_impl.h:301
std::string string
Definition: config.h:35
Maximum number of polling threads.
Definition: server_builder_impl.h:230
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
::grpc_impl::Server Server
Definition: server.h:26
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
::grpc_impl::ResourceQuota ResourceQuota
Definition: resource_quota.h:26
Experimental, to be deprecated.
Definition: server_builder_impl.h:298
Definition: server_builder_impl.h:67
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
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder_impl.h:327
Definition: async_generic_service.h:75
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
SyncServerOption
Options for synchronous servers.
Definition: server_builder_impl.h:227
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
Number of completion queues.
Definition: server_builder_impl.h:228
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder_impl.h:256
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
NamedService(grpc::Service *s)
Definition: server_builder_impl.h:307
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder_impl.h:185
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc_workaround_list
Definition: workaround_list.h:26
virtual ~ExternalConnectionAcceptor()
Definition: server_builder_impl.h:74
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder_impl.h:315
ByteBuffer read_buffer
Definition: server_builder_impl.h:72
HostString host
Definition: server_builder_impl.h:310
grpc_compression_level level
Definition: server_builder_impl.h:376
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder_impl.h:198
bool is_set
Definition: server_builder_impl.h:375
grpc::string addr
Definition: server_builder_impl.h:299
grpc::Service * service
Definition: server_builder_impl.h:311
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:121
Minimum number of polling threads.
Definition: server_builder_impl.h:229
::grpc_impl::ServerBuilder ServerBuilder
Definition: server_builder.h:26
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder_impl.h:294
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder_impl.h:305
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:677
Definition: server_builder_impl.h:306
std::shared_ptr< grpc_impl::ServerCredentials > creds
Definition: server_builder_impl.h:300
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder_impl.h:192
NamedService(const grpc::string &h, grpc::Service *s)
Definition: server_builder_impl.h:308
A sequence of bytes.
Definition: byte_buffer.h:67
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder_impl.h:86
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder_impl.h:318
experimental_type(grpc_impl::ServerBuilder *builder)
Definition: server_builder_impl.h:258
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota_impl.h:34
void SetInterceptorCreators(std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder_impl.h:261
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_impl.h:240