GRPC C++  1.4.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 #include <grpc/support/workaround_list.h>
50 
51 struct grpc_resource_quota;
52 
53 namespace grpc {
54 
55 class AsyncGenericService;
56 class ResourceQuota;
57 class CompletionQueue;
58 class RpcService;
59 class Server;
60 class ServerCompletionQueue;
61 class ServerCredentials;
62 class Service;
63 
64 namespace testing {
65 class ServerBuilderPluginTest;
66 } // namespace testing
67 
70  public:
71  ServerBuilder();
73 
80  };
81 
87 
91 
96  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
97 
99  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
100  max_receive_message_size_ = max_receive_message_size;
101  return *this;
102  }
103 
105  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
106  max_send_message_size_ = max_send_message_size;
107  return *this;
108  }
109 
111  ServerBuilder& SetMaxMessageSize(int max_message_size) {
112  return SetMaxReceiveMessageSize(max_message_size);
113  }
114 
121  grpc_compression_algorithm algorithm, bool enabled);
122 
126 
132 
134  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
135 
136  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
137 
140 
143  template <class T>
144  ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) {
145  return SetOption(MakeChannelArgumentOption(arg, value));
146  }
147 
160  // TODO(dgq): the "port" part seems to be a misnomer.
162  std::shared_ptr<ServerCredentials> creds,
163  int* selected_port = nullptr);
164 
187  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
188  bool is_frequently_polled = true);
189 
191  std::unique_ptr<Server> BuildAndStart();
192 
194  static void InternalAddPluginFactory(
195  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
196 
200  ServerBuilder& EnableWorkaround(grpc_workaround_list id);
201 
202  private:
203  friend class ::grpc::testing::ServerBuilderPluginTest;
204 
205  struct Port {
206  grpc::string addr;
207  std::shared_ptr<ServerCredentials> creds;
208  int* selected_port;
209  };
210 
211  struct SyncServerSettings {
212  SyncServerSettings()
213  : num_cqs(GPR_MAX(1, gpr_cpu_num_cores())),
214  min_pollers(1),
215  max_pollers(2),
216  cq_timeout_msec(10000) {}
217 
219  int num_cqs;
220 
223  int min_pollers;
224 
227  int max_pollers;
228 
230  int cq_timeout_msec;
231  };
232 
233  typedef std::unique_ptr<grpc::string> HostString;
234  struct NamedService {
235  explicit NamedService(Service* s) : service(s) {}
236  NamedService(const grpc::string& h, Service* s)
237  : host(new grpc::string(h)), service(s) {}
238  HostString host;
239  Service* service;
240  };
241 
242  int max_receive_message_size_;
243  int max_send_message_size_;
244  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
245  std::vector<std::unique_ptr<NamedService>> services_;
246  std::vector<Port> ports_;
247 
248  SyncServerSettings sync_server_settings_;
249 
251  std::vector<ServerCompletionQueue*> cqs_;
252 
253  std::shared_ptr<ServerCredentials> creds_;
254  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
255  grpc_resource_quota* resource_quota_;
256  AsyncGenericService* generic_service_;
257  struct {
258  bool is_set;
260  } maybe_default_compression_level_;
261  struct {
262  bool is_set;
264  } maybe_default_compression_algorithm_;
265  uint32_t enabled_compression_algorithms_bitset_;
266 };
267 
268 } // namespace grpc
269 
270 #endif // GRPCXX_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:111
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:78
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.
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:71
ServerBuilder & AddListeningPort(const grpc::string &addr, 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...
ResourceQuota represents a bound on memory usage by the gRPC library.
Definition: resource_quota.h:49
bool is_set
Definition: server_builder.h:258
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:83
Definition: async_generic_service.h:60
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:105
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:76
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:71
ServerBuilder & SetOption(std::unique_ptr< ServerBuilderOption > option)
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:99
ServerBuilder & RegisterService(Service *service)
Register a service.
grpc_compression_level level
Definition: server_builder.h:259
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:75
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:564
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:77
Completion queue timeout in milliseconds.
Definition: server_builder.h:79
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:144
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:69
grpc_compression_algorithm algorithm
Definition: server_builder.h:263