GRPC C++  1.2.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 
44 #include <grpc++/support/config.h>
45 #include <grpc/compression.h>
46 #include <grpc/support/cpu.h>
47 #include <grpc/support/useful.h>
48 
49 struct grpc_resource_quota;
50 
51 namespace grpc {
52 
53 class AsyncGenericService;
54 class ResourceQuota;
55 class CompletionQueue;
56 class RpcService;
57 class Server;
58 class ServerCompletionQueue;
59 class ServerCredentials;
60 class Service;
61 
62 namespace testing {
63 class ServerBuilderPluginTest;
64 } // namespace testing
65 
68  public:
69  ServerBuilder();
71 
73 
79 
83 
88  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
89 
91  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
92  max_receive_message_size_ = max_receive_message_size;
93  return *this;
94  }
95 
97  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
98  max_send_message_size_ = max_send_message_size;
99  return *this;
100  }
101 
103  ServerBuilder& SetMaxMessageSize(int max_message_size) {
104  return SetMaxReceiveMessageSize(max_message_size);
105  }
106 
113  grpc_compression_algorithm algorithm, bool enabled);
114 
118 
124 
126  ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
127 
128  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
129 
132 
143  // TODO(dgq): the "port" part seems to be a misnomer.
145  std::shared_ptr<ServerCredentials> creds,
146  int* selected_port = nullptr);
147 
169  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
170  bool is_frequently_polled = true);
171 
173  std::unique_ptr<Server> BuildAndStart();
174 
176  static void InternalAddPluginFactory(
177  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
178 
179  private:
180  friend class ::grpc::testing::ServerBuilderPluginTest;
181 
182  struct Port {
183  grpc::string addr;
184  std::shared_ptr<ServerCredentials> creds;
185  int* selected_port;
186  };
187 
188  struct SyncServerSettings {
189  SyncServerSettings()
190  : num_cqs(1),
191  min_pollers(1),
192  max_pollers(INT_MAX),
193  cq_timeout_msec(1000) {}
194 
195  // Number of server completion queues to create to listen to incoming RPCs.
196  int num_cqs;
197 
198  // Minimum number of threads per completion queue that should be listening
199  // to incoming RPCs.
200  int min_pollers;
201 
202  // Maximum number of threads per completion queue that can be listening to
203  // incoming RPCs.
204  int max_pollers;
205 
206  // The timeout for server completion queue's AsyncNext call.
207  int cq_timeout_msec;
208  };
209 
210  typedef std::unique_ptr<grpc::string> HostString;
211  struct NamedService {
212  explicit NamedService(Service* s) : service(s) {}
213  NamedService(const grpc::string& h, Service* s)
214  : host(new grpc::string(h)), service(s) {}
215  HostString host;
216  Service* service;
217  };
218 
219  int max_receive_message_size_;
220  int max_send_message_size_;
221  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
222  std::vector<std::unique_ptr<NamedService>> services_;
223  std::vector<Port> ports_;
224 
225  SyncServerSettings sync_server_settings_;
226 
227  // List of completion queues added via AddCompletionQueue() method
228  std::vector<ServerCompletionQueue*> cqs_;
229 
230  std::shared_ptr<ServerCredentials> creds_;
231  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
232  grpc_resource_quota* resource_quota_;
233  AsyncGenericService* generic_service_;
234  struct {
235  bool is_set;
237  } maybe_default_compression_level_;
238  struct {
239  bool is_set;
241  } maybe_default_compression_algorithm_;
242  uint32_t enabled_compression_algorithms_bitset_;
243 };
244 
245 } // namespace grpc
246 
247 #endif // GRPCXX_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:103
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:72
std::string string
Definition: config.h:50
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:235
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:97
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:72
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:91
ServerBuilder & RegisterService(Service *service)
Register a service.
grpc_compression_level level
Definition: server_builder.h:236
SyncServerOption
Definition: server_builder.h:72
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:503
std::unique_ptr< ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
Definition: server_builder.h:72
Definition: server_builder.h:72
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:67
grpc_compression_algorithm algorithm
Definition: server_builder.h:240