GRPC C++  1.22.0-dev
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 fd = -1;
71  ByteBuffer read_buffer; // data intended for the grpc server
72  };
74  // If called before grpc::Server is started or after it is shut down, the new
75  // connection will be closed.
76  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
77 };
78 
79 } // namespace experimental
80 } // namespace grpc
81 
82 namespace grpc_impl {
83 
86  public:
87  ServerBuilder();
88  virtual ~ServerBuilder();
89 
91  // Primary API's
92 
101  virtual std::unique_ptr<grpc::Server> BuildAndStart();
102 
107  ServerBuilder& RegisterService(grpc::Service* service);
108 
124  ServerBuilder& AddListeningPort(
125  const grpc::string& addr_uri,
126  std::shared_ptr<grpc_impl::ServerCredentials> creds,
127  int* selected_port = nullptr);
128 
159  std::unique_ptr<grpc_impl::ServerCompletionQueue> AddCompletionQueue(
160  bool is_frequently_polled = true);
161 
163  // Less commonly used RegisterService variants
164 
169  ServerBuilder& RegisterService(const grpc::string& host,
170  grpc::Service* service);
171 
176  ServerBuilder& RegisterAsyncGenericService(
177  grpc::AsyncGenericService* service);
178 
180  // Fine control knobs
181 
184  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
185  max_receive_message_size_ = max_receive_message_size;
186  return *this;
187  }
188 
191  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
192  max_send_message_size_ = max_send_message_size;
193  return *this;
194  }
195 
197  ServerBuilder& SetMaxMessageSize(int max_message_size) {
198  return SetMaxReceiveMessageSize(max_message_size);
199  }
200 
206  ServerBuilder& SetCompressionAlgorithmSupportStatus(
207  grpc_compression_algorithm algorithm, bool enabled);
208 
211  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
212 
216  ServerBuilder& SetDefaultCompressionAlgorithm(
217  grpc_compression_algorithm algorithm);
218 
220  ServerBuilder& SetResourceQuota(
221  const grpc_impl::ResourceQuota& resource_quota);
222 
223  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
224 
230  CQ_TIMEOUT_MSEC
231  };
232 
234  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
235 
238  template <class T>
239  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
240  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
241  }
242 
244  static void InternalAddPluginFactory(
245  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
246 
250  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
251 
256  public:
258  : builder_(builder) {}
259 
261  std::vector<std::unique_ptr<
263  interceptor_creators) {
264  builder_->interceptor_creators_ = std::move(interceptor_creators);
265  }
266 
271  ServerBuilder& RegisterCallbackGenericService(
273 
275  FROM_FD = 0 // in the form of a file descriptor
276  };
277 
282  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
283  AddExternalConnectionAcceptor(ExternalConnectionType type,
284  std::shared_ptr<ServerCredentials> creds);
285 
286  private:
287  ServerBuilder* builder_;
288  };
289 
294 
295  protected:
297  struct Port {
299  std::shared_ptr<grpc_impl::ServerCredentials> creds;
301  };
302 
304  typedef std::unique_ptr<grpc::string> HostString;
305  struct NamedService {
306  explicit NamedService(grpc::Service* s) : service(s) {}
308  : host(new grpc::string(h)), service(s) {}
309  HostString host;
311  };
312 
314  std::vector<Port> ports() { return ports_; }
315 
317  std::vector<NamedService*> services() {
318  std::vector<NamedService*> service_refs;
319  for (auto& ptr : services_) {
320  service_refs.push_back(ptr.get());
321  }
322  return service_refs;
323  }
324 
326  std::vector<grpc::ServerBuilderOption*> options() {
327  std::vector<grpc::ServerBuilderOption*> option_refs;
328  for (auto& ptr : options_) {
329  option_refs.push_back(ptr.get());
330  }
331  return option_refs;
332  }
333 
334  private:
335  friend class ::grpc::testing::ServerBuilderPluginTest;
336 
337  struct SyncServerSettings {
338  SyncServerSettings()
339  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
340 
342  int num_cqs;
343 
346  int min_pollers;
347 
350  int max_pollers;
351 
353  int cq_timeout_msec;
354  };
355 
356  int max_receive_message_size_;
357  int max_send_message_size_;
358  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
359  std::vector<std::unique_ptr<NamedService>> services_;
360  std::vector<Port> ports_;
361 
362  SyncServerSettings sync_server_settings_;
363 
365  std::vector<grpc_impl::ServerCompletionQueue*> cqs_;
366 
367  std::shared_ptr<grpc_impl::ServerCredentials> creds_;
368  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
369  grpc_resource_quota* resource_quota_;
370  grpc::AsyncGenericService* generic_service_{nullptr};
371  grpc::experimental::CallbackGenericService* callback_generic_service_{
372  nullptr};
373  struct {
374  bool is_set;
376  } maybe_default_compression_level_;
377  struct {
378  bool is_set;
380  } maybe_default_compression_algorithm_;
381  uint32_t enabled_compression_algorithms_bitset_;
382  std::vector<
383  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
384  interceptor_creators_;
385  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
386  acceptors_;
387 };
388 
389 } // namespace grpc_impl
390 
391 #endif // GRPCPP_SERVER_BUILDER_IMPL_H
ExternalConnectionType
Definition: server_builder_impl.h:274
grpc_compression_algorithm algorithm
Definition: server_builder_impl.h:379
int * selected_port
Definition: server_builder_impl.h:300
std::string string
Definition: config.h:35
Maximum number of polling threads.
Definition: server_builder_impl.h:229
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:297
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:326
Definition: async_generic_service.h:72
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
SyncServerOption
Options for synchronous servers.
Definition: server_builder_impl.h:226
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
Number of completion queues.
Definition: server_builder_impl.h:227
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder_impl.h:255
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:306
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder_impl.h:184
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:73
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder_impl.h:314
ByteBuffer read_buffer
Definition: server_builder_impl.h:71
HostString host
Definition: server_builder_impl.h:309
grpc_compression_level level
Definition: server_builder_impl.h:375
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder_impl.h:197
bool is_set
Definition: server_builder_impl.h:374
grpc::string addr
Definition: server_builder_impl.h:298
grpc::Service * service
Definition: server_builder_impl.h:310
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:125
Minimum number of polling threads.
Definition: server_builder_impl.h:228
::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:293
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder_impl.h:304
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:652
Definition: server_builder_impl.h:305
std::shared_ptr< grpc_impl::ServerCredentials > creds
Definition: server_builder_impl.h:299
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder_impl.h:191
NamedService(const grpc::string &h, grpc::Service *s)
Definition: server_builder_impl.h:307
A sequence of bytes.
Definition: byte_buffer.h:65
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder_impl.h:85
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder_impl.h:317
experimental_type(grpc_impl::ServerBuilder *builder)
Definition: server_builder_impl.h:257
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:260
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:239