GRPC C++  1.33.1
server.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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_H
20 #define GRPCPP_SERVER_H
21 
22 #include <list>
23 #include <memory>
24 #include <vector>
25 
27 
28 #include <grpc/compression.h>
29 #include <grpc/support/atm.h>
30 #include <grpcpp/channel.h>
33 #include <grpcpp/impl/call.h>
41 #include <grpcpp/support/config.h>
42 #include <grpcpp/support/status.h>
43 
44 struct grpc_server;
45 
46 namespace grpc {
47 class AsyncGenericService;
48 class ServerContext;
49 class ServerInitializer;
50 
51 namespace internal {
52 class ExternalConnectionAcceptorImpl;
53 } // namespace internal
54 
59 class Server : public ServerInterface, private GrpcLibraryCodegen {
60  public:
61  ~Server();
62 
67  void Wait() override;
68 
76  public:
77  virtual ~GlobalCallbacks() {}
79  virtual void UpdateArguments(ChannelArguments* /*args*/) {}
81  virtual void PreSynchronousRequest(ServerContext* context) = 0;
83  virtual void PostSynchronousRequest(ServerContext* context) = 0;
85  virtual void PreServerStart(Server* /*server*/) {}
87  virtual void AddPort(Server* /*server*/, const std::string& /*addr*/,
88  ServerCredentials* /*creds*/, int /*port*/) {}
89  };
95  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
96 
100 
103  return health_check_service_.get();
104  }
105 
107  std::shared_ptr<Channel> InProcessChannel(const ChannelArguments& args);
108 
113  public:
114  explicit experimental_type(Server* server) : server_(server) {}
115 
118  std::shared_ptr<Channel> InProcessChannelWithInterceptors(
119  const ChannelArguments& args,
120  std::vector<
121  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
122  interceptor_creators);
123 
124  private:
125  Server* server_;
126  };
127 
132 
133  protected:
136  bool RegisterService(const std::string* host, Service* service) override;
137 
151  int AddListeningPort(const std::string& addr,
152  ServerCredentials* creds) override;
153 
176  Server(ChannelArguments* args,
177  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
178  sync_server_cqs,
179  int min_pollers, int max_pollers, int sync_cq_timeout_msec,
180  std::vector<std::shared_ptr<internal::ExternalConnectionAcceptorImpl>>
181  acceptors,
182  grpc_resource_quota* server_rq = nullptr,
183  std::vector<
184  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
185  interceptor_creators = std::vector<std::unique_ptr<
187 
194  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
195 
196  grpc_server* server() override { return server_; }
197 
198  protected:
201  std::unique_ptr<HealthCheckServiceInterface> service) {
202  health_check_service_ = std::move(service);
203  }
204 
207  return health_check_service_disabled_;
208  }
209 
210  private:
211  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
212  interceptor_creators() override {
213  return &interceptor_creators_;
214  }
215 
216  friend class AsyncGenericService;
217  friend class ServerBuilder;
218  friend class ServerInitializer;
219 
220  class SyncRequest;
221  class CallbackRequestBase;
222  template <class ServerContextType>
223  class CallbackRequest;
224  class UnimplementedAsyncRequest;
225  class UnimplementedAsyncResponse;
226 
231  class SyncRequestThreadManager;
232 
235  void RegisterAsyncGenericService(AsyncGenericService* service) override;
236 
237 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
238  void RegisterCallbackGenericService(CallbackGenericService* service) override;
242 #else
243  class experimental_registration_type final
249  public:
250  explicit experimental_registration_type(Server* server) : server_(server) {}
251  void RegisterCallbackGenericService(
252  experimental::CallbackGenericService* service) override {
253  server_->RegisterCallbackGenericService(service);
254  }
255 
256  private:
257  Server* server_;
258  };
259 
261  void RegisterCallbackGenericService(
262  experimental::CallbackGenericService* service);
263 
267  experimental_registration_interface* experimental_registration() override {
268  return &experimental_registration_;
269  }
270 #endif
271 
272  void PerformOpsOnCall(internal::CallOpSetInterface* ops,
273  internal::Call* call) override;
274 
275  void ShutdownInternal(gpr_timespec deadline) override;
276 
277  int max_receive_message_size() const override {
278  return max_receive_message_size_;
279  }
280 
281  CompletionQueue* CallbackCQ() override;
282 
283  ServerInitializer* initializer();
284 
285  // Functions to manage the server shutdown ref count. Things that increase
286  // the ref count are the running state of the server (take a ref at start and
287  // drop it at shutdown) and each running callback RPC.
288  void Ref();
289  void UnrefWithPossibleNotify() /* LOCKS_EXCLUDED(mu_) */;
290  void UnrefAndWaitLocked() /* EXCLUSIVE_LOCKS_REQUIRED(mu_) */;
291 
292  std::vector<std::shared_ptr<internal::ExternalConnectionAcceptorImpl>>
293  acceptors_;
294 
295  // A vector of interceptor factory objects.
296  // This should be destroyed after health_check_service_ and this requirement
297  // is satisfied by declaring interceptor_creators_ before
298  // health_check_service_. (C++ mandates that member objects be destroyed in
299  // the reverse order of initialization.)
300  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
301  interceptor_creators_;
302 
303  int max_receive_message_size_;
304 
308  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
309  sync_server_cqs_;
310 
313  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
314 
315 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
316  // For registering experimental callback generic service; remove when that
317  // method longer experimental
318  experimental_registration_type experimental_registration_{this};
319 #endif
320 
321  // Server status
322  internal::Mutex mu_;
323  bool started_;
324  bool shutdown_;
325  bool shutdown_notified_; // Was notify called on the shutdown_cv_
326  internal::CondVar shutdown_done_cv_;
327  bool shutdown_done_ = false;
328  std::atomic_int shutdown_refs_outstanding_{1};
329 
330  internal::CondVar shutdown_cv_;
331 
332  std::shared_ptr<GlobalCallbacks> global_callbacks_;
333 
334  std::vector<std::string> services_;
335  bool has_async_generic_service_ = false;
336  bool has_callback_generic_service_ = false;
337  bool has_callback_methods_ = false;
338 
339  // Pointer to the wrapped grpc_server.
340  grpc_server* server_;
341 
342  std::unique_ptr<ServerInitializer> server_initializer_;
343 
344  std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
345  bool health_check_service_disabled_;
346 
347  // When appropriate, use a default callback generic service to handle
348  // unimplemented methods
349 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
350  std::unique_ptr<CallbackGenericService> unimplemented_service_;
351 #else
352  std::unique_ptr<experimental::CallbackGenericService> unimplemented_service_;
353 #endif
354 
355  // A special handler for resource exhausted in sync case
356  std::unique_ptr<internal::MethodHandler> resource_exhausted_handler_;
357 
358  // Handler for callback generic service, if any
359  std::unique_ptr<internal::MethodHandler> generic_handler_;
360 
361  // callback_cq_ references the callbackable completion queue associated
362  // with this server (if any). It is set on the first call to CallbackCQ().
363  // It is _not owned_ by the server; ownership belongs with its internal
364  // shutdown callback tag (invoked when the CQ is fully shutdown).
365  CompletionQueue* callback_cq_ /* GUARDED_BY(mu_) */ = nullptr;
366 
367  // List of CQs passed in by user that must be Shutdown only after Server is
368  // Shutdown. Even though this is only used with NDEBUG, instantiate it in all
369  // cases since otherwise the size will be inconsistent.
370  std::vector<CompletionQueue*> cq_list_;
371 };
372 
373 } // namespace grpc
374 
375 #endif // GRPCPP_SERVER_H
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
grpc::Server::InProcessChannel
std::shared_ptr< Channel > InProcessChannel(const ChannelArguments &args)
Establish a channel for in-process communication.
compression.h
grpc::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:527
grpc::Server
Represents a gRPC server.
Definition: server.h:59
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:691
grpc::Server::health_check_service_disabled
bool health_check_service_disabled() const
NOTE: This method is not part of the public API for this class.
Definition: server.h:206
grpc::ServerInitializer
Definition: server_initializer.h:31
grpc::Server::GlobalCallbacks::PostSynchronousRequest
virtual void PostSynchronousRequest(ServerContext *context)=0
Called after application callback for each synchronous server request.
grpc::HealthCheckServiceInterface
The gRPC server uses this interface to expose the health checking service without depending on protob...
Definition: health_check_service_interface.h:31
grpc::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:62
health_check_service_interface.h
grpc::Server::experimental_type::experimental_type
experimental_type(Server *server)
Definition: server.h:114
rpc_service_method.h
grpc::Server::GlobalCallbacks::PreSynchronousRequest
virtual void PreSynchronousRequest(ServerContext *context)=0
Called before application callback for each synchronous server request.
grpc::Server::Start
void Start(ServerCompletionQueue **cqs, size_t num_cqs) override
Start the server.
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
status.h
grpc::Server::Wait
void Wait() override
Block until the server shuts down.
grpc::Server::GlobalCallbacks::AddPort
virtual void AddPort(Server *, const std::string &, ServerCredentials *, int)
Called after a server port is added.
Definition: server.h:87
grpc::Server::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: server.h:77
grpc::ChannelArguments
Options for channel creation.
Definition: channel_arguments.h:39
grpc::Server::~Server
~Server()
grpc::Server::ServerInitializer
friend class ServerInitializer
Definition: server.h:218
channel_arguments.h
completion_queue.h
grpc::Server::RegisterService
bool RegisterService(const std::string *host, Service *service) override
Register a service.
grpc::Server::AddListeningPort
int AddListeningPort(const std::string &addr, ServerCredentials *creds) override
Try binding the server to the given addr endpoint (port, and optionally including IP address to bind ...
grpc::ServerInterface::experimental_registration_interface
NOTE: class experimental_registration_interface is not part of the public API of this class TODO(vjpa...
Definition: server_interface.h:144
grpc_server
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
grpc::Server::server
grpc_server * server() override
Definition: server.h:196
grpc::Server::GlobalCallbacks::UpdateArguments
virtual void UpdateArguments(ChannelArguments *)
Called before server is created.
Definition: server.h:79
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:106
grpc::Server::GlobalCallbacks::PreServerStart
virtual void PreServerStart(Server *)
Called before server is started.
Definition: server.h:85
grpc::AsyncGenericService
Definition: async_generic_service.h:68
channel.h
client_interceptor.h
grpc_library.h
grpc::Server::experimental_type
NOTE: class experimental_type is not part of the public API of this class.
Definition: server.h:112
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:43
server_credentials.h
grpc::Server::GetHealthCheckService
HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: server.h:102
config.h
grpc::Server::experimental_type::InProcessChannelWithInterceptors
std::shared_ptr< Channel > InProcessChannelWithInterceptors(const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Establish a channel for in-process communication with client interceptors.
grpc::Server::Server
Server(ChannelArguments *args, std::shared_ptr< std::vector< std::unique_ptr< ServerCompletionQueue >>> sync_server_cqs, int min_pollers, int max_pollers, int sync_cq_timeout_msec, std::vector< std::shared_ptr< internal::ExternalConnectionAcceptorImpl >> acceptors, grpc_resource_quota *server_rq=nullptr, std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >> interceptor_creators=std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >>())
NOTE: This is NOT a public API.
port_platform.h
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:418
call.h
grpc::Server::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Set the global callback object.
grpc::Server::c_server
grpc_server * c_server()
Returns a raw pointer to the underlying grpc_server instance.
server_interface.h
grpc::Server::set_health_check_service
void set_health_check_service(std::unique_ptr< HealthCheckServiceInterface > service)
NOTE: This method is not part of the public API for this class.
Definition: server.h:200
completion_queue.h
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:90
grpc::ServerInterface
Definition: server_interface.h:65
atm.h
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::Server::GlobalCallbacks
Global callbacks are a set of hooks that are called when server events occur.
Definition: server.h:75
grpc::Server::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server.h:131