GRPC C++  1.30.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 
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 ResourceQuota;
45 class Server;
47 class ServerCredentials;
48 } // namespace grpc_impl
49 
50 namespace grpc {
51 
52 class AsyncGenericService;
53 class Service;
54 namespace testing {
55 class ServerBuilderPluginTest;
56 } // namespace testing
57 
58 namespace internal {
59 class ExternalConnectionAcceptorImpl;
60 } // namespace internal
61 
62 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
63 namespace experimental {
64 #endif
65 class CallbackGenericService;
66 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
67 } // namespace experimental
68 #endif
69 
70 namespace experimental {
71 // EXPERIMENTAL API:
72 // Interface for a grpc server to build transports with connections created out
73 // of band.
74 // See ServerBuilder's AddExternalConnectionAcceptor API.
76  public:
78  int listener_fd = -1;
79  int fd = -1;
80  ByteBuffer read_buffer; // data intended for the grpc server
81  };
83  // If called before grpc::Server is started or after it is shut down, the new
84  // connection will be closed.
85  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
86 };
87 
88 } // namespace experimental
89 } // namespace grpc
90 
91 namespace grpc_impl {
92 
95  public:
96  ServerBuilder();
97  virtual ~ServerBuilder();
98 
100  // Primary API's
101 
112  virtual std::unique_ptr<grpc::Server> BuildAndStart();
113 
119 
136  const grpc::string& addr_uri,
137  std::shared_ptr<grpc_impl::ServerCredentials> creds,
138  int* selected_port = nullptr);
139 
170  std::unique_ptr<grpc_impl::ServerCompletionQueue> AddCompletionQueue(
171  bool is_frequently_polled = true);
172 
174  // Less commonly used RegisterService variants
175 
181  grpc::Service* service);
182 
188  grpc::AsyncGenericService* service);
189 
191  // Fine control knobs
192 
195  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
196  max_receive_message_size_ = max_receive_message_size;
197  return *this;
198  }
199 
202  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
203  max_send_message_size_ = max_send_message_size;
204  return *this;
205  }
206 
208  ServerBuilder& SetMaxMessageSize(int max_message_size) {
209  return SetMaxReceiveMessageSize(max_message_size);
210  }
211 
218  grpc_compression_algorithm algorithm, bool enabled);
219 
223 
229 
232  const grpc_impl::ResourceQuota& resource_quota);
233 
234  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
235 
242  };
243 
246 
249  template <class T>
250  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
251  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
252  }
253 
255  static void InternalAddPluginFactory(
256  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
257 
262 
267  public:
269  : builder_(builder) {}
270 
272  std::vector<std::unique_ptr<
274  interceptor_creators) {
275  builder_->interceptor_creators_ = std::move(interceptor_creators);
276  }
277 
278 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
285 #endif
286 
288  FROM_FD = 0 // in the form of a file descriptor
289  };
290 
295  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
297  std::shared_ptr<ServerCredentials> creds);
298 
299  private:
300  ServerBuilder* builder_;
301  };
302 
303 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
304  ServerBuilder& RegisterCallbackGenericService(
309  grpc::CallbackGenericService* service);
310 #endif
311 
316 
317  protected:
319  struct Port {
321  std::shared_ptr<grpc_impl::ServerCredentials> creds;
323  };
324 
326  typedef std::unique_ptr<grpc::string> HostString;
327  struct NamedService {
328  explicit NamedService(grpc::Service* s) : service(s) {}
330  : host(new grpc::string(h)), service(s) {}
333  };
334 
336  std::vector<Port> ports() { return ports_; }
337 
339  std::vector<NamedService*> services() {
340  std::vector<NamedService*> service_refs;
341  for (auto& ptr : services_) {
342  service_refs.push_back(ptr.get());
343  }
344  return service_refs;
345  }
346 
348  std::vector<grpc::ServerBuilderOption*> options() {
349  std::vector<grpc::ServerBuilderOption*> option_refs;
350  for (auto& ptr : options_) {
351  option_refs.push_back(ptr.get());
352  }
353  return option_refs;
354  }
355 
356  private:
357  friend class ::grpc::testing::ServerBuilderPluginTest;
358 
359  struct SyncServerSettings {
360  SyncServerSettings()
361  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
362 
364  int num_cqs;
365 
368  int min_pollers;
369 
372  int max_pollers;
373 
375  int cq_timeout_msec;
376  };
377 
378  int max_receive_message_size_;
379  int max_send_message_size_;
380  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
381  std::vector<std::unique_ptr<NamedService>> services_;
382  std::vector<Port> ports_;
383 
384  SyncServerSettings sync_server_settings_;
385 
387  std::vector<grpc_impl::ServerCompletionQueue*> cqs_;
388 
389  std::shared_ptr<grpc_impl::ServerCredentials> creds_;
390  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
391  grpc_resource_quota* resource_quota_;
392  grpc::AsyncGenericService* generic_service_{nullptr};
393 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
394  grpc::CallbackGenericService* callback_generic_service_{nullptr};
395 #else
396  grpc::experimental::CallbackGenericService* callback_generic_service_{
397  nullptr};
398 #endif
399 
400  struct {
401  bool is_set;
403  } maybe_default_compression_level_;
404  struct {
405  bool is_set;
407  } maybe_default_compression_algorithm_;
408  uint32_t enabled_compression_algorithms_bitset_;
409  std::vector<
410  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
411  interceptor_creators_;
412  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
413  acceptors_;
414 };
415 
416 } // namespace grpc_impl
417 
418 #endif // GRPCPP_SERVER_BUILDER_IMPL_H
grpc_impl::ServerBuilder::SetCompressionAlgorithmSupportStatus
ServerBuilder & SetCompressionAlgorithmSupportStatus(grpc_compression_algorithm algorithm, bool enabled)
Set the support status for compression algorithms.
compression.h
grpc_impl::ServerBuilder::NamedService::host
HostString host
Definition: server_builder_impl.h:331
grpc_impl::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder_impl.h:94
grpc_impl::ServerBuilder::InternalAddPluginFactory
static void InternalAddPluginFactory(std::unique_ptr< grpc::ServerBuilderPlugin >(*CreatePlugin)())
For internal use only: Register a ServerBuilderPlugin factory function.
grpc_impl::ServerBuilder::services
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder_impl.h:339
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:694
grpc::ServerCredentials
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
grpc_impl::ServerBuilder::AddChannelArgument
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:250
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_impl::ServerBuilder::ServerBuilder
ServerBuilder()
grpc_impl::ServerBuilder::RegisterAsyncGenericService
ServerBuilder & RegisterAsyncGenericService(grpc::AsyncGenericService *service)
Register a generic service.
grpc_impl::ServerBuilder::MAX_POLLERS
Maximum number of polling threads.
Definition: server_builder_impl.h:240
grpc_impl::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_impl::ServerBuilder::~ServerBuilder
virtual ~ServerBuilder()
grpc_impl::ServerBuilder::NamedService
Definition: server_builder_impl.h:327
server_interceptor.h
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
grpc_impl::ServerBuilder::NUM_CQS
Number of completion queues.
Definition: server_builder_impl.h:238
grpc_impl::ServerBuilder::AddListeningPort
ServerBuilder & AddListeningPort(const grpc::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::Server
::grpc_impl::Server Server
Definition: server.h:26
grpc_impl::ServerBuilder::experimental_type::SetInterceptorCreators
void SetInterceptorCreators(std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder_impl.h:271
grpc_impl::ServerBuilder::NamedService::NamedService
NamedService(const grpc::string &h, grpc::Service *s)
Definition: server_builder_impl.h:329
grpc_impl::ServerBuilder::SyncServerOption
SyncServerOption
Options for synchronous servers.
Definition: server_builder_impl.h:237
grpc_impl::ServerBuilder::Port::creds
std::shared_ptr< grpc_impl::ServerCredentials > creds
Definition: server_builder_impl.h:321
grpc_impl::ServerBuilder::experimental_type::ExternalConnectionType::FROM_FD
grpc_impl::ServerBuilder::experimental_type::RegisterCallbackGenericService
ServerBuilder & RegisterCallbackGenericService(grpc::experimental::CallbackGenericService *service)
Register a generic service that uses the callback API.
grpc_impl::ServerBuilder::options
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder_impl.h:348
server_builder_plugin.h
grpc_impl::ServerBuilder::MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder_impl.h:239
grpc_impl::ResourceQuota
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota_impl.h:34
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::read_buffer
ByteBuffer read_buffer
Definition: server_builder_impl.h:80
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters
Definition: server_builder_impl.h:77
grpc_impl::ServerBuilder::Port::selected_port
int * selected_port
Definition: server_builder_impl.h:322
grpc_impl::ServerBuilder::NamedService::service
grpc::Service * service
Definition: server_builder_impl.h:332
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::fd
int fd
Definition: server_builder_impl.h:79
grpc_impl::ServerBuilder::experimental_type
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder_impl.h:266
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:67
grpc_impl::ServerBuilder::Port
Experimental, to be deprecated.
Definition: server_builder_impl.h:319
grpc_impl::ServerBuilder::AddCompletionQueue
std::unique_ptr< grpc_impl::ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
grpc_impl::ServerBuilder::is_set
bool is_set
Definition: server_builder_impl.h:401
grpc_impl::ServerBuilder::SetMaxReceiveMessageSize
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder_impl.h:195
grpc_impl::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_impl::ServerBuilder::Port::addr
grpc::string addr
Definition: server_builder_impl.h:320
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_impl::ServerBuilder::level
grpc_compression_level level
Definition: server_builder_impl.h:402
grpc::AsyncGenericService
Definition: async_generic_service.h:77
grpc::experimental::ExternalConnectionAcceptor
Definition: server_builder_impl.h:75
cpu.h
grpc_impl::ServerBuilder::SetMaxMessageSize
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder_impl.h:208
grpc_impl::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::MakeChannelArgumentOption
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
grpc_impl::ServerBuilder::experimental_type::experimental_type
experimental_type(grpc_impl::ServerBuilder *builder)
Definition: server_builder_impl.h:268
grpc_impl::ServerBuilder::NamedService::NamedService
NamedService(grpc::Service *s)
Definition: server_builder_impl.h:328
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:47
grpc_impl::ServerBuilder::ports
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder_impl.h:336
grpc_impl::ServerBuilder::CQ_TIMEOUT_MSEC
Completion queue timeout in milliseconds.
Definition: server_builder_impl.h:241
grpc_impl::ServerBuilder::SetMaxSendMessageSize
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder_impl.h:202
grpc_impl::ServerBuilder::experimental_type::ExternalConnectionType
ExternalConnectionType
Definition: server_builder_impl.h:287
config.h
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
grpc::experimental::ExternalConnectionAcceptor::~ExternalConnectionAcceptor
virtual ~ExternalConnectionAcceptor()
Definition: server_builder_impl.h:82
grpc_impl::ServerBuilder::SetSyncServerOption
ServerBuilder & SetSyncServerOption(SyncServerOption option, int value)
Only useful if this is a Synchronous server.
grpc::string
std::string string
Definition: config.h:35
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_impl::ServerBuilder::RegisterService
ServerBuilder & RegisterService(grpc::Service *service)
Register a service.
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::listener_fd
int listener_fd
Definition: server_builder_impl.h:78
grpc_impl::ServerBuilder::SetOption
ServerBuilder & SetOption(std::unique_ptr< grpc::ServerBuilderOption > option)
grpc_impl::ServerBuilder::EnableWorkaround
ServerBuilder & EnableWorkaround(grpc_workaround_list id)
Enable a server workaround.
grpc::ServerCompletionQueue
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
grpc_impl::ServerBuilder::HostString
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder_impl.h:326
grpc_impl::ServerBuilder::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder_impl.h:315
grpc_impl::ServerBuilder::SetResourceQuota
ServerBuilder & SetResourceQuota(const grpc_impl::ResourceQuota &resource_quota)
Set the attached buffer pool for this server.
grpc_impl::ServerBuilder::algorithm
grpc_compression_algorithm algorithm
Definition: server_builder_impl.h:406
channel_argument_option.h
grpc_impl::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Return a running server which is ready for processing calls.
grpc::ResourceQuota
::grpc_impl::ResourceQuota ResourceQuota
Definition: resource_quota.h:26