GRPC C++  1.14.0-dev
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  virtual ~ServerBuilder();
56 
58  // Primary API's
59 
68  virtual 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 
148  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
149  max_receive_message_size_ = max_receive_message_size;
150  return *this;
151  }
152 
155  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
156  max_send_message_size_ = max_send_message_size;
157  return *this;
158  }
159 
161  ServerBuilder& SetMaxMessageSize(int max_message_size) {
162  return SetMaxReceiveMessageSize(max_message_size);
163  }
164 
170  ServerBuilder& SetCompressionAlgorithmSupportStatus(
171  grpc_compression_algorithm algorithm, bool enabled);
172 
175  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
176 
180  ServerBuilder& SetDefaultCompressionAlgorithm(
181  grpc_compression_algorithm algorithm);
182 
184  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
185 
186  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
187 
193  CQ_TIMEOUT_MSEC
194  };
195 
197  ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
198 
201  template <class T>
202  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
203  return SetOption(MakeChannelArgumentOption(arg, value));
204  }
205 
207  static void InternalAddPluginFactory(
208  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
209 
213  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
214 
215  protected:
217  struct Port {
219  std::shared_ptr<ServerCredentials> creds;
221  };
222 
224  typedef std::unique_ptr<grpc::string> HostString;
225  struct NamedService {
226  explicit NamedService(Service* s) : service(s) {}
228  : host(new grpc::string(h)), service(s) {}
229  HostString host;
231  };
232 
234  std::vector<Port> ports() { return ports_; }
235 
237  std::vector<NamedService*> services() {
238  std::vector<NamedService*> service_refs;
239  for (auto& ptr : services_) {
240  service_refs.push_back(ptr.get());
241  }
242  return service_refs;
243  }
244 
246  std::vector<ServerBuilderOption*> options() {
247  std::vector<ServerBuilderOption*> option_refs;
248  for (auto& ptr : options_) {
249  option_refs.push_back(ptr.get());
250  }
251  return option_refs;
252  }
253 
254  private:
255  friend class ::grpc::testing::ServerBuilderPluginTest;
256 
257  struct SyncServerSettings {
258  SyncServerSettings()
259  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
260 
262  int num_cqs;
263 
266  int min_pollers;
267 
270  int max_pollers;
271 
273  int cq_timeout_msec;
274  };
275 
276  int max_receive_message_size_;
277  int max_send_message_size_;
278  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
279  std::vector<std::unique_ptr<NamedService>> services_;
280  std::vector<Port> ports_;
281 
282  SyncServerSettings sync_server_settings_;
283 
285  std::vector<ServerCompletionQueue*> cqs_;
286 
287  std::shared_ptr<ServerCredentials> creds_;
288  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
289  grpc_resource_quota* resource_quota_;
290  AsyncGenericService* generic_service_;
291  struct {
292  bool is_set;
294  } maybe_default_compression_level_;
295  struct {
296  bool is_set;
298  } maybe_default_compression_algorithm_;
299  uint32_t enabled_compression_algorithms_bitset_;
300 };
301 
302 } // namespace grpc
303 
304 #endif // GRPCPP_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:161
int * selected_port
Definition: server_builder.h:220
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:237
Maximum number of polling threads.
Definition: server_builder.h:192
std::string string
Definition: config.h:35
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:219
std::unique_ptr< grpc::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:224
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const grpc::string &name, const grpc::string &value)
Experimental, to be deprecated.
Definition: server_builder.h:217
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:292
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
HostString host
Definition: server_builder.h:229
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:155
Number of completion queues.
Definition: server_builder.h:190
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:234
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
NamedService(Service *s)
Definition: server_builder.h:226
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:148
grpc_compression_level level
Definition: server_builder.h:293
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:189
grpc::string addr
Definition: server_builder.h:218
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:620
Definition: server_builder.h:225
NamedService(const grpc::string &h, Service *s)
Definition: server_builder.h:227
Minimum number of polling threads.
Definition: server_builder.h:191
Service * service
Definition: server_builder.h:230
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:202
std::vector< ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:246
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:297