GRPC C++  1.8.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 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 GRPCXX_SERVER_BUILDER_H
20 #define GRPCXX_SERVER_BUILDER_H
21 
22 #include <climits>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
30 #include <grpc++/support/config.h>
31 #include <grpc/compression.h>
32 #include <grpc/support/cpu.h>
33 #include <grpc/support/useful.h>
35 
36 struct grpc_resource_quota;
37 
38 namespace grpc {
39 
40 class AsyncGenericService;
41 class ResourceQuota;
42 class CompletionQueue;
43 class Server;
44 class ServerCompletionQueue;
45 class ServerCredentials;
46 class Service;
47 
48 namespace testing {
49 class ServerBuilderPluginTest;
50 } // namespace testing
51 
54  public:
55  ServerBuilder();
57 
59  // Primary API's
60 
69  std::unique_ptr<Server> BuildAndStart();
70 
76 
93  std::shared_ptr<ServerCredentials> creds,
94  int* selected_port = nullptr);
95 
126  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
127  bool is_frequently_polled = true);
128 
130  // Less commonly used RegisterService variants
131 
136  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
137 
143 
145  // Fine control knobs
146 
148  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
149  max_receive_message_size_ = max_receive_message_size;
150  return *this;
151  }
152 
154  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
155  max_send_message_size_ = max_send_message_size;
156  return *this;
157  }
158 
160  ServerBuilder& SetMaxMessageSize(int max_message_size) {
161  return SetMaxReceiveMessageSize(max_message_size);
162  }
163 
170  grpc_compression_algorithm algorithm, bool enabled);
171 
175 
181 
183  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
184 
185  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
186 
193  };
194 
197 
200  template <class T>
201  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
202  return SetOption(MakeChannelArgumentOption(arg, value));
203  }
204 
206  static void InternalAddPluginFactory(
207  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
208 
213 
214  private:
215  friend class ::grpc::testing::ServerBuilderPluginTest;
216 
217  struct Port {
218  grpc::string addr;
219  std::shared_ptr<ServerCredentials> creds;
220  int* selected_port;
221  };
222 
223  struct SyncServerSettings {
224  SyncServerSettings()
225  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
226 
228  int num_cqs;
229 
232  int min_pollers;
233 
236  int max_pollers;
237 
239  int cq_timeout_msec;
240  };
241 
242  typedef std::unique_ptr<grpc::string> HostString;
243  struct NamedService {
244  explicit NamedService(Service* s) : service(s) {}
245  NamedService(const grpc::string& h, Service* s)
246  : host(new grpc::string(h)), service(s) {}
247  HostString host;
248  Service* service;
249  };
250 
251  int max_receive_message_size_;
252  int max_send_message_size_;
253  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
254  std::vector<std::unique_ptr<NamedService>> services_;
255  std::vector<Port> ports_;
256 
257  SyncServerSettings sync_server_settings_;
258 
260  std::vector<ServerCompletionQueue*> cqs_;
261 
262  std::shared_ptr<ServerCredentials> creds_;
263  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
264  grpc_resource_quota* resource_quota_;
265  AsyncGenericService* generic_service_;
266  struct {
267  bool is_set;
269  } maybe_default_compression_level_;
270  struct {
271  bool is_set;
273  } maybe_default_compression_algorithm_;
274  uint32_t enabled_compression_algorithms_bitset_;
275 };
276 
277 } // namespace grpc
278 
279 #endif // GRPCXX_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:160
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.
Maximum number of polling threads.
Definition: server_builder.h:191
std::string string
Definition: config.h:35
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.
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
ResourceQuota represents a bound on memory usage by the gRPC library.
Definition: resource_quota.h:34
bool is_set
Definition: server_builder.h:267
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:97
Definition: async_generic_service.h:45
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:154
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.
Number of completion queues.
Definition: server_builder.h:189
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:78
ServerBuilder & SetOption(std::unique_ptr< ServerBuilderOption > option)
grpc_workaround_list
Definition: workaround_list.h:26
ServerBuilder & EnableWorkaround(grpc_workaround_list id)
Enable a server workaround.
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:148
ServerBuilder & RegisterService(Service *service)
Register a service.
grpc_compression_level level
Definition: server_builder.h:268
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:188
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:587
std::unique_ptr< ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
Minimum number of polling threads.
Definition: server_builder.h:190
Completion queue timeout in milliseconds.
Definition: server_builder.h:192
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:201
ServerBuilder & AddListeningPort(const grpc::string &addr_uri, std::shared_ptr< 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...
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:53
grpc_compression_algorithm algorithm
Definition: server_builder.h:272