GRPC C++  1.31.1
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 
28 
29 #include <grpc/compression.h>
30 #include <grpc/support/cpu.h>
36 #include <grpcpp/server.h>
37 #include <grpcpp/support/config.h>
38 
39 struct grpc_resource_quota;
40 
41 namespace grpc_impl {
42 
43 class CompletionQueue;
44 class Server;
46 class ServerCredentials;
47 } // namespace grpc_impl
48 
49 namespace grpc {
50 
51 class AsyncGenericService;
52 class Service;
53 namespace testing {
54 class ServerBuilderPluginTest;
55 } // namespace testing
56 
57 namespace internal {
58 class ExternalConnectionAcceptorImpl;
59 } // namespace internal
60 
61 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
62 namespace experimental {
63 #endif
64 class CallbackGenericService;
65 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
66 } // namespace experimental
67 #endif
68 
69 namespace experimental {
70 // EXPERIMENTAL API:
71 // Interface for a grpc server to build transports with connections created out
72 // of band.
73 // See ServerBuilder's AddExternalConnectionAcceptor API.
75  public:
77  int listener_fd = -1;
78  int fd = -1;
79  ByteBuffer read_buffer; // data intended for the grpc server
80  };
82  // If called before grpc::Server is started or after it is shut down, the new
83  // connection will be closed.
84  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
85 };
86 
87 } // namespace experimental
88 
91  public:
92  ServerBuilder();
93  virtual ~ServerBuilder();
94 
96  // Primary API's
97 
108  virtual std::unique_ptr<grpc::Server> BuildAndStart();
109 
115 
132  const std::string& addr_uri,
133  std::shared_ptr<grpc_impl::ServerCredentials> creds,
134  int* selected_port = nullptr);
135 
166  std::unique_ptr<grpc_impl::ServerCompletionQueue> AddCompletionQueue(
167  bool is_frequently_polled = true);
168 
170  // Less commonly used RegisterService variants
171 
176  ServerBuilder& RegisterService(const std::string& host,
177  grpc::Service* service);
178 
184  grpc::AsyncGenericService* service);
185 
187  // Fine control knobs
188 
191  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
192  max_receive_message_size_ = max_receive_message_size;
193  return *this;
194  }
195 
198  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
199  max_send_message_size_ = max_send_message_size;
200  return *this;
201  }
202 
204  ServerBuilder& SetMaxMessageSize(int max_message_size) {
205  return SetMaxReceiveMessageSize(max_message_size);
206  }
207 
214  grpc_compression_algorithm algorithm, bool enabled);
215 
219 
225 
227  ServerBuilder& SetResourceQuota(const grpc::ResourceQuota& resource_quota);
228 
229  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
230 
237  };
238 
241 
244  template <class T>
245  ServerBuilder& AddChannelArgument(const std::string& arg, const T& value) {
246  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
247  }
248 
250  static void InternalAddPluginFactory(
251  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
252 
257 
262  public:
263  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
264 
266  std::vector<std::unique_ptr<
268  interceptor_creators) {
269  builder_->interceptor_creators_ = std::move(interceptor_creators);
270  }
271 
272 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
279 #endif
280 
282  FROM_FD = 0 // in the form of a file descriptor
283  };
284 
289  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
291  std::shared_ptr<ServerCredentials> creds);
292 
293  private:
294  ServerBuilder* builder_;
295  };
296 
297 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
298  ServerBuilder& RegisterCallbackGenericService(
303  grpc::CallbackGenericService* service);
304 #endif
305 
310 
311  protected:
313  struct Port {
314  std::string addr;
315  std::shared_ptr<grpc_impl::ServerCredentials> creds;
317  };
318 
320  typedef std::unique_ptr<std::string> HostString;
321  struct NamedService {
322  explicit NamedService(grpc::Service* s) : service(s) {}
323  NamedService(const std::string& h, grpc::Service* s)
324  : host(new std::string(h)), service(s) {}
327  };
328 
330  std::vector<Port> ports() { return ports_; }
331 
333  std::vector<NamedService*> services() {
334  std::vector<NamedService*> service_refs;
335  for (auto& ptr : services_) {
336  service_refs.push_back(ptr.get());
337  }
338  return service_refs;
339  }
340 
342  std::vector<grpc::ServerBuilderOption*> options() {
343  std::vector<grpc::ServerBuilderOption*> option_refs;
344  for (auto& ptr : options_) {
345  option_refs.push_back(ptr.get());
346  }
347  return option_refs;
348  }
349 
350  private:
351  friend class ::grpc::testing::ServerBuilderPluginTest;
352 
353  struct SyncServerSettings {
354  SyncServerSettings()
355  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
356 
358  int num_cqs;
359 
362  int min_pollers;
363 
366  int max_pollers;
367 
369  int cq_timeout_msec;
370  };
371 
372  int max_receive_message_size_;
373  int max_send_message_size_;
374  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
375  std::vector<std::unique_ptr<NamedService>> services_;
376  std::vector<Port> ports_;
377 
378  SyncServerSettings sync_server_settings_;
379 
381  std::vector<grpc_impl::ServerCompletionQueue*> cqs_;
382 
383  std::shared_ptr<grpc_impl::ServerCredentials> creds_;
384  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
385  grpc_resource_quota* resource_quota_;
386  grpc::AsyncGenericService* generic_service_{nullptr};
387 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
388  grpc::CallbackGenericService* callback_generic_service_{nullptr};
389 #else
390  grpc::experimental::CallbackGenericService* callback_generic_service_{
391  nullptr};
392 #endif
393 
394  struct {
395  bool is_set;
397  } maybe_default_compression_level_;
398  struct {
399  bool is_set;
401  } maybe_default_compression_algorithm_;
402  uint32_t enabled_compression_algorithms_bitset_;
403  std::vector<
404  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
405  interceptor_creators_;
406  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
407  acceptors_;
408 };
409 
410 } // namespace grpc
411 
412 #endif // GRPCPP_SERVER_BUILDER_H
grpc::ServerBuilder::level
grpc_compression_level level
Definition: server_builder.h:396
grpc::ServerBuilder::NamedService
Definition: server_builder.h:321
compression.h
grpc::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Return a running server which is ready for processing calls.
grpc::ServerBuilder::algorithm
grpc_compression_algorithm algorithm
Definition: server_builder.h:400
grpc::MakeChannelArgumentOption
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const std::string &name, const std::string &value)
grpc::ServerBuilder::ServerBuilder
ServerBuilder()
grpc::ServerBuilder::SetDefaultCompressionAlgorithm
ServerBuilder & SetDefaultCompressionAlgorithm(grpc_compression_algorithm algorithm)
The default compression algorithm to use for all channel calls in the absence of a call-specific leve...
grpc::ServerBuilder::EnableWorkaround
ServerBuilder & EnableWorkaround(grpc_workaround_list id)
Enable a server workaround.
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:696
grpc::ServerBuilder::ports
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:330
grpc::ServerCredentials
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
grpc::ServerBuilder::NamedService::NamedService
NamedService(const std::string &h, grpc::Service *s)
Definition: server_builder.h:323
grpc::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
workaround_list.h
grpc_compression_algorithm
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:57
grpc::ServerBuilder::experimental_type::AddExternalConnectionAcceptor
std::unique_ptr< grpc::experimental::ExternalConnectionAcceptor > AddExternalConnectionAcceptor(ExternalConnectionType type, std::shared_ptr< ServerCredentials > creds)
Register an acceptor to handle the externally accepted connection in grpc server.
grpc::ServerBuilder::Port::addr
std::string addr
Definition: server_builder.h:314
server_interceptor.h
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
grpc::ServerBuilder::InternalAddPluginFactory
static void InternalAddPluginFactory(std::unique_ptr< grpc::ServerBuilderPlugin >(*CreatePlugin)())
For internal use only: Register a ServerBuilderPlugin factory function.
grpc::ServerBuilder::Port
Experimental, to be deprecated.
Definition: server_builder.h:313
grpc::Server
::grpc_impl::Server Server
Definition: server.h:26
grpc::ServerBuilder::Port::selected_port
int * selected_port
Definition: server_builder.h:316
grpc::ServerBuilder::NUM_CQS
@ NUM_CQS
Number of completion queues.
Definition: server_builder.h:233
server_builder_plugin.h
grpc::ServerBuilder::experimental_type::ExternalConnectionType
ExternalConnectionType
Definition: server_builder.h:281
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::read_buffer
ByteBuffer read_buffer
Definition: server_builder.h:79
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters
Definition: server_builder.h:76
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::fd
int fd
Definition: server_builder.h:78
grpc::ServerBuilder::MAX_POLLERS
@ MAX_POLLERS
Maximum number of polling threads.
Definition: server_builder.h:235
grpc::ServerBuilder::SetDefaultCompressionLevel
ServerBuilder & SetDefaultCompressionLevel(grpc_compression_level level)
The default compression level to use for all channel calls in the absence of a call-specific level.
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:67
grpc::ServerBuilder::experimental_type::experimental_type
experimental_type(ServerBuilder *builder)
Definition: server_builder.h:263
grpc::ServerBuilder::Port::creds
std::shared_ptr< grpc_impl::ServerCredentials > creds
Definition: server_builder.h:315
grpc::ResourceQuota
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota.h:34
grpc::ServerBuilder::HostString
std::unique_ptr< std::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:320
grpc::ServerBuilder::RegisterAsyncGenericService
ServerBuilder & RegisterAsyncGenericService(grpc::AsyncGenericService *service)
Register a generic service.
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:125
server_builder_option.h
grpc::ServerBuilder::experimental_type
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder.h:261
grpc::AsyncGenericService
Definition: async_generic_service.h:77
grpc::experimental::ExternalConnectionAcceptor
Definition: server_builder.h:74
cpu.h
grpc::ServerBuilder::CQ_TIMEOUT_MSEC
@ CQ_TIMEOUT_MSEC
Completion queue timeout in milliseconds.
Definition: server_builder.h:236
grpc::ServerBuilder::SetMaxMessageSize
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:204
grpc::ServerBuilder::SetSyncServerOption
ServerBuilder & SetSyncServerOption(SyncServerOption option, int value)
Only useful if this is a Synchronous server.
grpc::ServerBuilder::SetCompressionAlgorithmSupportStatus
ServerBuilder & SetCompressionAlgorithmSupportStatus(grpc_compression_algorithm algorithm, bool enabled)
Set the support status for compression algorithms.
grpc::ServerBuilder::SyncServerOption
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:232
grpc::ServerBuilder::AddListeningPort
ServerBuilder & AddListeningPort(const std::string &addr_uri, std::shared_ptr< grpc_impl::ServerCredentials > creds, int *selected_port=nullptr)
Enlists an endpoint addr (port with an optional IP address) to bind the grpc::Server object to be cre...
grpc::ServerBuilder::services
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:333
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:47
grpc::ServerBuilder::NamedService::service
grpc::Service * service
Definition: server_builder.h:326
grpc::ServerBuilder::experimental_type::RegisterCallbackGenericService
ServerBuilder & RegisterCallbackGenericService(grpc::experimental::CallbackGenericService *service)
Register a generic service that uses the callback API.
grpc::ServerBuilder::is_set
bool is_set
Definition: server_builder.h:395
grpc::ServerBuilder::experimental_type::ExternalConnectionType::FROM_FD
@ FROM_FD
config.h
grpc::ServerBuilder::NamedService::host
HostString host
Definition: server_builder.h:325
port_platform.h
grpc_workaround_list
grpc_workaround_list
Definition: workaround_list.h:26
grpc_compression_level
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:71
std
Definition: async_unary_call_impl.h:301
grpc::experimental::ExternalConnectionAcceptor::~ExternalConnectionAcceptor
virtual ~ExternalConnectionAcceptor()
Definition: server_builder.h:81
grpc::ServerBuilder::SetResourceQuota
ServerBuilder & SetResourceQuota(const grpc::ResourceQuota &resource_quota)
Set the attached buffer pool for this server.
grpc::ServerBuilder::experimental_type::SetInterceptorCreators
void SetInterceptorCreators(std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder.h:265
grpc::ServerBuilder::NamedService::NamedService
NamedService(grpc::Service *s)
Definition: server_builder.h:322
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
server.h
grpc::experimental::ExternalConnectionAcceptor::HandleNewConnection
virtual void HandleNewConnection(NewConnectionParameters *p)=0
grpc::ServerBuilder::AddCompletionQueue
std::unique_ptr< grpc_impl::ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::listener_fd
int listener_fd
Definition: server_builder.h:77
grpc::ServerBuilder::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:309
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:90
grpc::ServerCompletionQueue
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
grpc::ServerBuilder::SetOption
ServerBuilder & SetOption(std::unique_ptr< grpc::ServerBuilderOption > option)
grpc::ServerBuilder::SetMaxReceiveMessageSize
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:191
grpc::ServerBuilder::~ServerBuilder
virtual ~ServerBuilder()
grpc::ServerBuilder::options
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:342
grpc::ServerBuilder::AddChannelArgument
ServerBuilder & AddChannelArgument(const std::string &arg, const T &value)
Add a channel argument (an escape hatch to tuning core library parameters directly)
Definition: server_builder.h:245
channel_argument_option.h
grpc::ServerBuilder::MIN_POLLERS
@ MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder.h:234
grpc::ServerBuilder::SetMaxSendMessageSize
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:198
grpc::ServerBuilder::RegisterService
ServerBuilder & RegisterService(grpc::Service *service)
Register a service.