GRPC C++  1.10.0
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 
27 #include <grpc/compression.h>
28 #include <grpc/support/cpu.h>
33 #include <grpcpp/support/config.h>
34 
35 struct grpc_resource_quota;
36 
37 namespace grpc {
38 
39 class AsyncGenericService;
40 class ResourceQuota;
41 class CompletionQueue;
42 class Server;
43 class ServerCompletionQueue;
44 class ServerCredentials;
45 class Service;
46 
47 namespace testing {
48 class ServerBuilderPluginTest;
49 } // namespace testing
50 
53  public:
54  ServerBuilder();
55  ~ServerBuilder();
56 
58  // Primary API's
59 
68  std::unique_ptr<Server> BuildAndStart();
69 
74  ServerBuilder& RegisterService(Service* service);
75 
91  ServerBuilder& AddListeningPort(const grpc::string& addr_uri,
92  std::shared_ptr<ServerCredentials> creds,
93  int* selected_port = nullptr);
94 
125  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
126  bool is_frequently_polled = true);
127 
129  // Less commonly used RegisterService variants
130 
135  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
136 
141  ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
142 
144  // Fine control knobs
145 
147  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
148  max_receive_message_size_ = max_receive_message_size;
149  return *this;
150  }
151 
153  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
154  max_send_message_size_ = max_send_message_size;
155  return *this;
156  }
157 
159  ServerBuilder& SetMaxMessageSize(int max_message_size) {
160  return SetMaxReceiveMessageSize(max_message_size);
161  }
162 
168  ServerBuilder& SetCompressionAlgorithmSupportStatus(
169  grpc_compression_algorithm algorithm, bool enabled);
170 
173  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
174 
178  ServerBuilder& SetDefaultCompressionAlgorithm(
179  grpc_compression_algorithm algorithm);
180 
182  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
183 
184  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
185 
191  CQ_TIMEOUT_MSEC
192  };
193 
195  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
196 
199  template <class T>
200  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
201  return SetOption(MakeChannelArgumentOption(arg, value));
202  }
203 
205  static void InternalAddPluginFactory(
206  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
207 
211  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
212 
213  private:
214  friend class ::grpc::testing::ServerBuilderPluginTest;
215 
216  struct Port {
217  grpc::string addr;
218  std::shared_ptr<ServerCredentials> creds;
219  int* selected_port;
220  };
221 
222  struct SyncServerSettings {
223  SyncServerSettings()
224  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
225 
227  int num_cqs;
228 
231  int min_pollers;
232 
235  int max_pollers;
236 
238  int cq_timeout_msec;
239  };
240 
241  typedef std::unique_ptr<grpc::string> HostString;
242  struct NamedService {
243  explicit NamedService(Service* s) : service(s) {}
244  NamedService(const grpc::string& h, Service* s)
245  : host(new grpc::string(h)), service(s) {}
246  HostString host;
247  Service* service;
248  };
249 
250  int max_receive_message_size_;
251  int max_send_message_size_;
252  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
253  std::vector<std::unique_ptr<NamedService>> services_;
254  std::vector<Port> ports_;
255 
256  SyncServerSettings sync_server_settings_;
257 
259  std::vector<ServerCompletionQueue*> cqs_;
260 
261  std::shared_ptr<ServerCredentials> creds_;
262  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
263  grpc_resource_quota* resource_quota_;
264  AsyncGenericService* generic_service_;
265  struct {
266  bool is_set;
268  } maybe_default_compression_level_;
269  struct {
270  bool is_set;
272  } maybe_default_compression_algorithm_;
273  uint32_t enabled_compression_algorithms_bitset_;
274 };
275 
276 } // namespace grpc
277 
278 #endif // GRPCPP_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:159
Maximum number of polling threads.
Definition: server_builder.h:190
std::string string
Definition: config.h:35
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
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:266
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:70
Definition: async_generic_service.h:62
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:153
Number of completion queues.
Definition: server_builder.h:188
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
grpc_workaround_list
Definition: workaround_list.h:26
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:147
grpc_compression_level level
Definition: server_builder.h:267
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:187
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:590
Minimum number of polling threads.
Definition: server_builder.h:189
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:200
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:52
grpc_compression_algorithm algorithm
Definition: server_builder.h:271