GRPC C++  1.0.0
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 <map>
38 #include <memory>
39 #include <vector>
40 
43 #include <grpc++/support/config.h>
44 #include <grpc/compression.h>
45 
46 namespace grpc {
47 
48 class AsyncGenericService;
49 class CompletionQueue;
50 class RpcService;
51 class Server;
52 class ServerCompletionQueue;
53 class ServerCredentials;
54 class Service;
55 
56 namespace testing {
57 class ServerBuilderPluginTest;
58 } // namespace testing
59 
62  public:
63  ServerBuilder();
64 
69  ServerBuilder& RegisterService(Service* service);
70 
73  ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
74 
79  ServerBuilder& RegisterService(const grpc::string& host, Service* service);
80 
82  ServerBuilder& SetMaxMessageSize(int max_message_size) {
83  max_message_size_ = max_message_size;
84  return *this;
85  }
86 
92  ServerBuilder& SetCompressionAlgorithmSupportStatus(
93  grpc_compression_algorithm algorithm, bool enabled);
94 
97  ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
98 
102  ServerBuilder& SetDefaultCompressionAlgorithm(
103  grpc_compression_algorithm algorithm);
104 
105  ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
106 
117  // TODO(dgq): the "port" part seems to be a misnomer.
118  ServerBuilder& AddListeningPort(const grpc::string& addr,
119  std::shared_ptr<ServerCredentials> creds,
120  int* selected_port = nullptr);
121 
143  std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
144  bool is_frequently_polled = true);
145 
147  std::unique_ptr<Server> BuildAndStart();
148 
150  static void InternalAddPluginFactory(
151  std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
152 
153  private:
154  friend class ::grpc::testing::ServerBuilderPluginTest;
155 
156  struct Port {
157  grpc::string addr;
158  std::shared_ptr<ServerCredentials> creds;
159  int* selected_port;
160  };
161 
162  typedef std::unique_ptr<grpc::string> HostString;
163  struct NamedService {
164  explicit NamedService(Service* s) : service(s) {}
165  NamedService(const grpc::string& h, Service* s)
166  : host(new grpc::string(h)), service(s) {}
167  HostString host;
168  Service* service;
169  };
170 
171  int max_message_size_;
172  std::vector<std::unique_ptr<ServerBuilderOption>> options_;
173  std::vector<std::unique_ptr<NamedService>> services_;
174  std::vector<Port> ports_;
175  std::vector<ServerCompletionQueue*> cqs_;
176  std::shared_ptr<ServerCredentials> creds_;
177  std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
178  AsyncGenericService* generic_service_;
179  struct {
180  bool is_set;
182  } maybe_default_compression_level_;
183  struct {
184  bool is_set;
186  } maybe_default_compression_algorithm_;
187  uint32_t enabled_compression_algorithms_bitset_;
188 };
189 
190 } // namespace grpc
191 
192 #endif // GRPCXX_SERVER_BUILDER_H
ServerBuilder & SetMaxMessageSize(int max_message_size)
Set max message size in bytes.
Definition: server_builder.h:82
std::string string
Definition: config.h:118
Definition: service_type.h:64
bool is_set
Definition: server_builder.h:180
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:84
Definition: async_generic_service.h:60
grpc_compression_algorithm
Definition: compression_types.h:72
Definition: alarm.h:48
grpc_compression_level level
Definition: server_builder.h:181
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:61
grpc_compression_algorithm algorithm
Definition: server_builder.h:185