GRPC C++  1.30.0
server_impl.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_IMPL_H
20 #define GRPCPP_SERVER_IMPL_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_impl.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 
49 namespace internal {
50 class ExternalConnectionAcceptorImpl;
51 } // namespace internal
52 
53 } // namespace grpc
54 
55 namespace grpc_impl {
57 class ServerContext;
58 class ServerInitializer;
59 
65  public:
66  ~Server();
67 
72  void Wait() override;
73 
81  public:
82  virtual ~GlobalCallbacks() {}
84  virtual void UpdateArguments(ChannelArguments* /*args*/) {}
86  virtual void PreSynchronousRequest(grpc_impl::ServerContext* context) = 0;
88  virtual void PostSynchronousRequest(grpc_impl::ServerContext* context) = 0;
90  virtual void PreServerStart(Server* /*server*/) {}
92  virtual void AddPort(Server* /*server*/, const grpc::string& /*addr*/,
93  grpc::ServerCredentials* /*creds*/, int /*port*/) {}
94  };
100  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
101 
105 
108  return health_check_service_.get();
109  }
110 
112  std::shared_ptr<Channel> InProcessChannel(const ChannelArguments& args);
113 
118  public:
119  explicit experimental_type(Server* server) : server_(server) {}
120 
123  std::shared_ptr<Channel> InProcessChannelWithInterceptors(
124  const ChannelArguments& args,
125  std::vector<std::unique_ptr<
127  interceptor_creators);
128 
129  private:
130  Server* server_;
131  };
132 
137 
138  protected:
141  bool RegisterService(const grpc::string* host,
142  grpc::Service* service) override;
143 
157  int AddListeningPort(const grpc::string& addr,
158  grpc::ServerCredentials* creds) override;
159 
182  Server(ChannelArguments* args,
183  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
184  sync_server_cqs,
185  int min_pollers, int max_pollers, int sync_cq_timeout_msec,
186  std::vector<
187  std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
188  acceptors,
189  grpc_resource_quota* server_rq = nullptr,
190  std::vector<std::unique_ptr<
192  interceptor_creators = std::vector<std::unique_ptr<
194 
201  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
202 
203  grpc_server* server() override { return server_; }
204 
205  protected:
208  std::unique_ptr<grpc::HealthCheckServiceInterface> service) {
209  health_check_service_ = std::move(service);
210  }
211 
214  return health_check_service_disabled_;
215  }
216 
217  private:
218  std::vector<
219  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>*
220  interceptor_creators() override {
221  return &interceptor_creators_;
222  }
223 
227 
228  class SyncRequest;
229  class CallbackRequestBase;
230  template <class ServerContextType>
231  class CallbackRequest;
232  class UnimplementedAsyncRequest;
233  class UnimplementedAsyncResponse;
234 
239  class SyncRequestThreadManager;
240 
243  void RegisterAsyncGenericService(grpc::AsyncGenericService* service) override;
244 
245 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
246  void RegisterCallbackGenericService(
250  grpc::CallbackGenericService* service) override;
251 #else
252  class experimental_registration_type final
258  public:
259  explicit experimental_registration_type(Server* server) : server_(server) {}
260  void RegisterCallbackGenericService(
261  grpc::experimental::CallbackGenericService* service) override {
262  server_->RegisterCallbackGenericService(service);
263  }
264 
265  private:
266  Server* server_;
267  };
268 
270  void RegisterCallbackGenericService(
272 
276  experimental_registration_interface* experimental_registration() override {
277  return &experimental_registration_;
278  }
279 #endif
280 
281  void PerformOpsOnCall(grpc::internal::CallOpSetInterface* ops,
282  grpc::internal::Call* call) override;
283 
284  void ShutdownInternal(gpr_timespec deadline) override;
285 
286  int max_receive_message_size() const override {
287  return max_receive_message_size_;
288  }
289 
290  CompletionQueue* CallbackCQ() override;
291 
292  grpc_impl::ServerInitializer* initializer();
293 
294  // Functions to manage the server shutdown ref count. Things that increase
295  // the ref count are the running state of the server (take a ref at start and
296  // drop it at shutdown) and each running callback RPC.
297  void Ref();
298  void UnrefWithPossibleNotify() /* LOCKS_EXCLUDED(mu_) */;
299  void UnrefAndWaitLocked() /* EXCLUSIVE_LOCKS_REQUIRED(mu_) */;
300 
301  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
302  acceptors_;
303 
304  // A vector of interceptor factory objects.
305  // This should be destroyed after health_check_service_ and this requirement
306  // is satisfied by declaring interceptor_creators_ before
307  // health_check_service_. (C++ mandates that member objects be destroyed in
308  // the reverse order of initialization.)
309  std::vector<
310  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
311  interceptor_creators_;
312 
313  int max_receive_message_size_;
314 
318  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
319  sync_server_cqs_;
320 
323  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
324 
325 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
326  // For registering experimental callback generic service; remove when that
327  // method longer experimental
328  experimental_registration_type experimental_registration_{this};
329 #endif
330 
331  // Server status
333  bool started_;
334  bool shutdown_;
335  bool shutdown_notified_; // Was notify called on the shutdown_cv_
336  grpc::internal::CondVar shutdown_done_cv_;
337  bool shutdown_done_ = false;
338  std::atomic_int shutdown_refs_outstanding_{1};
339 
340  grpc::internal::CondVar shutdown_cv_;
341 
342  std::shared_ptr<GlobalCallbacks> global_callbacks_;
343 
344  std::vector<grpc::string> services_;
345  bool has_async_generic_service_ = false;
346  bool has_callback_generic_service_ = false;
347  bool has_callback_methods_ = false;
348 
349  // Pointer to the wrapped grpc_server.
350  grpc_server* server_;
351 
352  std::unique_ptr<grpc_impl::ServerInitializer> server_initializer_;
353 
354  std::unique_ptr<grpc::HealthCheckServiceInterface> health_check_service_;
355  bool health_check_service_disabled_;
356 
357  // When appropriate, use a default callback generic service to handle
358  // unimplemented methods
359 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
360  std::unique_ptr<grpc::CallbackGenericService> unimplemented_service_;
361 #else
362  std::unique_ptr<grpc::experimental::CallbackGenericService>
363  unimplemented_service_;
364 #endif
365 
366  // A special handler for resource exhausted in sync case
367  std::unique_ptr<grpc::internal::MethodHandler> resource_exhausted_handler_;
368 
369  // Handler for callback generic service, if any
370  std::unique_ptr<grpc::internal::MethodHandler> generic_handler_;
371 
372  // callback_cq_ references the callbackable completion queue associated
373  // with this server (if any). It is set on the first call to CallbackCQ().
374  // It is _not owned_ by the server; ownership belongs with its internal
375  // shutdown callback tag (invoked when the CQ is fully shutdown).
376  CompletionQueue* callback_cq_ /* GUARDED_BY(mu_) */ = nullptr;
377 
378  // List of CQs passed in by user that must be Shutdown only after Server is
379  // Shutdown. Even though this is only used with NDEBUG, instantiate it in all
380  // cases since otherwise the size will be inconsistent.
381  std::vector<CompletionQueue*> cq_list_;
382 };
383 
384 } // namespace grpc_impl
385 
386 #endif // GRPCPP_SERVER_IMPL_H
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
compression.h
grpc::internal::CondVar
Definition: sync.h:118
grpc_impl::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder_impl.h:94
grpc_impl::Server::GetHealthCheckService
grpc::HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: server_impl.h:107
grpc_impl::Server::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: server_impl.h:82
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:694
grpc::HealthCheckServiceInterface
::grpc_impl::HealthCheckServiceInterface HealthCheckServiceInterface
Definition: health_check_service_interface.h:29
channel_impl.h
grpc_impl::ServerInitializer
Definition: server_initializer_impl.h:34
grpc::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:38
health_check_service_interface.h
rpc_service_method.h
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
status.h
grpc_impl::Server::GlobalCallbacks::AddPort
virtual void AddPort(Server *, const grpc::string &, grpc::ServerCredentials *, int)
Called after a server port is added.
Definition: server_impl.h:92
grpc_impl::Server::~Server
~Server()
grpc_impl::Server::Wait
void Wait() override
Block until the server shuts down.
grpc_impl::Server::GlobalCallbacks::PostSynchronousRequest
virtual void PostSynchronousRequest(grpc_impl::ServerContext *context)=0
Called after application callback for each synchronous server request.
grpc_impl::Server
Represents a gRPC server.
Definition: server_impl.h:64
grpc_impl::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:424
grpc_impl::Server::AddListeningPort
int AddListeningPort(const grpc::string &addr, grpc::ServerCredentials *creds) override
Try binding the server to the given addr endpoint (port, and optionally including IP address to bind ...
grpc_impl::Server::set_health_check_service
void set_health_check_service(std::unique_ptr< grpc::HealthCheckServiceInterface > service)
NOTE: This method is not part of the public API for this class.
Definition: server_impl.h:207
grpc_impl::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context_impl.h:510
grpc_impl::HealthCheckServiceInterface
The gRPC server uses this interface to expose the health checking service without depending on protob...
Definition: health_check_service_interface_impl.h:28
grpc_impl::Server::GlobalCallbacks::UpdateArguments
virtual void UpdateArguments(ChannelArguments *)
Called before server is created.
Definition: server_impl.h:84
grpc::internal::CallOpSetInterface
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call_op_set_interface.h:34
grpc_impl::Server::Start
void Start(ServerCompletionQueue **cqs, size_t num_cqs) override
Start the server.
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:147
grpc::ServerContext
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:26
grpc_impl::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< grpc::internal::ExternalConnectionAcceptorImpl >> acceptors, grpc_resource_quota *server_rq=nullptr, std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators=std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >>())
NOTE: This is NOT a public API.
grpc_server
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
channel_arguments_impl.h
grpc_impl::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials_impl.h:40
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:125
grpc::AsyncGenericService
Definition: async_generic_service.h:77
grpc_impl::ChannelArguments
Options for channel creation.
Definition: channel_arguments_impl.h:43
grpc::ServerInitializer
::grpc_impl::ServerInitializer ServerInitializer
Definition: server_initializer.h:26
grpc_impl::Server::experimental_type::InProcessChannelWithInterceptors
std::shared_ptr< Channel > InProcessChannelWithInterceptors(const ChannelArguments &args, std::vector< std::unique_ptr< grpc::experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Establish a channel for in-process communication with client interceptors.
grpc_impl::Server::GlobalCallbacks::PreServerStart
virtual void PreServerStart(Server *)
Called before server is started.
Definition: server_impl.h:90
client_interceptor.h
grpc_library.h
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:47
server_credentials.h
grpc_impl::Server::GlobalCallbacks::PreSynchronousRequest
virtual void PreSynchronousRequest(grpc_impl::ServerContext *context)=0
Called before application callback for each synchronous server request.
grpc_impl::Server::experimental_type
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_impl.h:117
grpc_impl::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_impl.h:213
grpc_impl::Server::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Set the global callback object.
config.h
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:49
port_platform.h
call.h
grpc::string
std::string string
Definition: config.h:35
grpc_impl::Server::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_impl.h:136
grpc_impl::Server::GlobalCallbacks
Global callbacks are a set of hooks that are called when server events occur.
Definition: server_impl.h:80
grpc_impl::Server::InProcessChannel
std::shared_ptr< Channel > InProcessChannel(const ChannelArguments &args)
Establish a channel for in-process communication.
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
server_interface.h
grpc_impl::Server::c_server
grpc_server * c_server()
Returns a raw pointer to the underlying grpc_server instance.
completion_queue_impl.h
grpc::internal::Mutex
Definition: sync.h:47
grpc::ServerInterface
Definition: server_interface.h:68
atm.h
completion_queue_impl.h
grpc_impl::Server::RegisterService
bool RegisterService(const grpc::string *host, grpc::Service *service) override
Register a service.
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc_impl::Server::experimental_type::experimental_type
experimental_type(Server *server)
Definition: server_impl.h:119
grpc_impl::Server::server
grpc_server * server() override
Definition: server_impl.h:203