GRPC C++  1.3.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
server_builder.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_SERVER_BUILDER_H
35 #define GRPCXX_SERVER_BUILDER_H
36 
37 #include <climits>
38 #include <map>
39 #include <memory>
40 #include <vector>
41 
45 #include <grpc++/support/config.h>
46 #include <grpc/compression.h>
47 #include <grpc/support/cpu.h>
48 #include <grpc/support/useful.h>
49 
50 struct grpc_resource_quota;
51 
52 namespace grpc {
53 
54 class AsyncGenericService;
55 class ResourceQuota;
56 class CompletionQueue;
57 class RpcService;
58 class Server;
59 class ServerCompletionQueue;
60 class ServerCredentials;
61 class Service;
62 
63 namespace testing {
64 class ServerBuilderPluginTest;
65 } // namespace testing
66 
69  public:
70  ServerBuilder();
72 
74 
80 
84 
89  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
90 
92  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
93  max_receive_message_size_ = max_receive_message_size;
94  return *this;
95  }
96 
98  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
99  max_send_message_size_ = max_send_message_size;
100  return *this;
101  }
102 
104  ServerBuilder& SetMaxMessageSize(int max_message_size) {
105  return SetMaxReceiveMessageSize(max_message_size);
106  }
107 
114  grpc_compression_algorithm algorithm, bool enabled);
115 
119 
125 
127  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
128 
129  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
130 
133 
136  template <class T>
137  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
138  return SetOption(MakeChannelArgumentOption(arg, value));
139  }
140 
151  // TODO(dgq): the "port" part seems to be a misnomer.
153  std::shared_ptr<ServerCredentials> creds,
154  int* selected_port = nullptr);
155 
177  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
178  bool is_frequently_polled = true);
179 
181  std::unique_ptr<Server> BuildAndStart();
182 
184  static void InternalAddPluginFactory(
185  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
186 
187  private:
188  friend class ::grpc::testing::ServerBuilderPluginTest;
189 
190  struct Port {
191  grpc::string addr;
192  std::shared_ptr<ServerCredentials> creds;
193  int* selected_port;
194  };
195 
196  struct SyncServerSettings {
197  SyncServerSettings()
198  : num_cqs(1),
199  min_pollers(1),
200  max_pollers(INT_MAX),
201  cq_timeout_msec(1000) {}
202 
203  // Number of server completion queues to create to listen to incoming RPCs.
204  int num_cqs;
205 
206  // Minimum number of threads per completion queue that should be listening
207  // to incoming RPCs.
208  int min_pollers;
209 
210  // Maximum number of threads per completion queue that can be listening to
211  // incoming RPCs.
212  int max_pollers;
213 
214  // The timeout for server completion queue's AsyncNext call.
215  int cq_timeout_msec;
216  };
217 
218  typedef std::unique_ptr<grpc::string> HostString;
219  struct NamedService {
220  explicit NamedService(Service* s) : service(s) {}
221  NamedService(const grpc::string& h, Service* s)
222  : host(new grpc::string(h)), service(s) {}
223  HostString host;
224  Service* service;
225  };
226 
227  int max_receive_message_size_;
228  int max_send_message_size_;
229  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
230  std::vector<std::unique_ptr<NamedService>> services_;
231  std::vector<Port> ports_;
232 
233  SyncServerSettings sync_server_settings_;
234 
235  // List of completion queues added via AddCompletionQueue() method
236  std::vector<ServerCompletionQueue*> cqs_;
237 
238  std::shared_ptr<ServerCredentials> creds_;
239  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
240  grpc_resource_quota* resource_quota_;
241  AsyncGenericService* generic_service_;
242  struct {
243  bool is_set;
245  } maybe_default_compression_level_;
246  struct {
247  bool is_set;
249  } maybe_default_compression_algorithm_;
250  uint32_t enabled_compression_algorithms_bitset_;
251 };
252 
253 } // namespace grpc
254 
255 #endif // GRPCXX_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:104
ServerBuilder & SetResourceQuota(const ResourceQuota &resource_quota)
Set the attached buffer pool for this server.
ServerBuilder & RegisterAsyncGenericService(AsyncGenericService *service)
Register a generic service.
static void InternalAddPluginFactory(std::unique_ptr< ServerBuilderPlugin >(*CreatePlugin)())
For internal use only: Register a ServerBuilderPlugin factory function.
Definition: server_builder.h:73
std::string string
Definition: config.h:50
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
ServerBuilder & SetSyncServerOption(SyncServerOption option, int value)
Only useful if this is a Synchronous server.
Definition: service_type.h:64
ServerBuilder & AddListeningPort(const grpc::string &addr, std::shared_ptr< ServerCredentials > creds, int *selected_port=nullptr)
Tries to bind server to the given addr.
ResourceQuota represents a bound on memory usage by the gRPC library.
Definition: resource_quota.h:48
bool is_set
Definition: server_builder.h:243
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:84
Definition: async_generic_service.h:60
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:98
ServerBuilder & SetDefaultCompressionLevel(grpc_compression_level level)
The default compression level to use for all channel calls in the absence of a call-specific level...
ServerBuilder & SetCompressionAlgorithmSupportStatus(grpc_compression_algorithm algorithm, bool enabled)
Set the support status for compression algorithms.
std::unique_ptr< Server > BuildAndStart()
Return a running server which is ready for processing calls.
Definition: server_builder.h:73
grpc_compression_algorithm
Definition: compression_types.h:72
ServerBuilder & SetOption(std::unique_ptr< ServerBuilderOption > option)
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:92
ServerBuilder & RegisterService(Service *service)
Register a service.
grpc_compression_level level
Definition: server_builder.h:244
SyncServerOption
Definition: server_builder.h:73
ServerBuilder & SetDefaultCompressionAlgorithm(grpc_compression_algorithm algorithm)
The default compression algorithm to use for all channel calls in the absence of a call-specific leve...
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:544
std::unique_ptr< ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
Definition: server_builder.h:73
Definition: server_builder.h:73
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:137
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:68
grpc_compression_algorithm algorithm
Definition: server_builder.h:248