GRPC C++  1.23.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 <condition_variable>
23 #include <list>
24 #include <memory>
25 #include <mutex>
26 #include <vector>
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 
104  grpc_server* c_server();
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 
185  Server(int max_message_size, ChannelArguments* args,
186  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
187  sync_server_cqs,
188  int min_pollers, int max_pollers, int sync_cq_timeout_msec,
189  std::vector<
190  std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
191  acceptors,
192  grpc_resource_quota* server_rq = nullptr,
193  std::vector<std::unique_ptr<
195  interceptor_creators = std::vector<std::unique_ptr<
197 
204  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
205 
206  grpc_server* server() override { return server_; }
207 
208  protected:
211  std::unique_ptr<grpc::HealthCheckServiceInterface> service) {
212  health_check_service_ = std::move(service);
213  }
214 
217  return health_check_service_disabled_;
218  }
219 
220  private:
221  std::vector<
222  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>*
223  interceptor_creators() override {
224  return &interceptor_creators_;
225  }
226 
230 
231  class SyncRequest;
232  class CallbackRequestBase;
233  template <class ServerContextType>
234  class CallbackRequest;
235  class UnimplementedAsyncRequest;
236  class UnimplementedAsyncResponse;
237 
242  class SyncRequestThreadManager;
243 
246  void RegisterAsyncGenericService(grpc::AsyncGenericService* service) override;
247 
252  class experimental_registration_type final
254  public:
255  explicit experimental_registration_type(Server* server) : server_(server) {}
256  void RegisterCallbackGenericService(
257  grpc::experimental::CallbackGenericService* service) override {
258  server_->RegisterCallbackGenericService(service);
259  }
260 
261  private:
262  Server* server_;
263  };
264 
266  void RegisterCallbackGenericService(
268 
272  experimental_registration_interface* experimental_registration() override {
273  return &experimental_registration_;
274  }
275 
276  void PerformOpsOnCall(grpc::internal::CallOpSetInterface* ops,
277  grpc::internal::Call* call) override;
278 
279  void ShutdownInternal(gpr_timespec deadline) override;
280 
281  int max_receive_message_size() const override {
282  return max_receive_message_size_;
283  }
284 
285  CompletionQueue* CallbackCQ() override;
286 
287  grpc_impl::ServerInitializer* initializer();
288 
289  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
290  acceptors_;
291 
292  // A vector of interceptor factory objects.
293  // This should be destroyed after health_check_service_ and this requirement
294  // is satisfied by declaring interceptor_creators_ before
295  // health_check_service_. (C++ mandates that member objects be destroyed in
296  // the reverse order of initialization.)
297  std::vector<
298  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
299  interceptor_creators_;
300 
301  const int max_receive_message_size_;
302 
306  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
307  sync_server_cqs_;
308 
311  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
312 
313  // Outstanding unmatched callback requests, indexed by method.
314  // NOTE: Using a gpr_atm rather than atomic_int because atomic_int isn't
315  // copyable or movable and thus will cause compilation errors. We
316  // actually only want to extend the vector before the threaded use
317  // starts, but this is still a limitation.
318  std::vector<gpr_atm> callback_unmatched_reqs_count_;
319 
320  // List of callback requests to start when server actually starts.
321  std::list<CallbackRequestBase*> callback_reqs_to_start_;
322 
323  // For registering experimental callback generic service; remove when that
324  // method longer experimental
325  experimental_registration_type experimental_registration_{this};
326 
327  // Server status
329  bool started_;
330  bool shutdown_;
331  bool shutdown_notified_; // Was notify called on the shutdown_cv_
332 
333  grpc::internal::CondVar shutdown_cv_;
334 
335  // It is ok (but not required) to nest callback_reqs_mu_ under mu_ .
336  // Incrementing callback_reqs_outstanding_ is ok without a lock but it must be
337  // decremented under the lock in case it is the last request and enables the
338  // server shutdown. The increment is performance-critical since it happens
339  // during periods of increasing load; the decrement happens only when memory
340  // is maxed out, during server shutdown, or (possibly in a future version)
341  // during decreasing load, so it is less performance-critical.
342  grpc::internal::Mutex callback_reqs_mu_;
343  grpc::internal::CondVar callback_reqs_done_cv_;
344  std::atomic<intptr_t> callback_reqs_outstanding_{0};
345 
346  std::shared_ptr<GlobalCallbacks> global_callbacks_;
347 
348  std::vector<grpc::string> services_;
349  bool has_async_generic_service_{false};
350  bool has_callback_generic_service_{false};
351 
352  // Pointer to the wrapped grpc_server.
353  grpc_server* server_;
354 
355  std::unique_ptr<grpc_impl::ServerInitializer> server_initializer_;
356 
357  std::unique_ptr<grpc::HealthCheckServiceInterface> health_check_service_;
358  bool health_check_service_disabled_;
359 
360  // When appropriate, use a default callback generic service to handle
361  // unimplemented methods
362  std::unique_ptr<grpc::experimental::CallbackGenericService>
363  unimplemented_service_;
364 
365  // A special handler for resource exhausted in sync case
366  std::unique_ptr<grpc::internal::MethodHandler> resource_exhausted_handler_;
367 
368  // Handler for callback generic service, if any
369  std::unique_ptr<grpc::internal::MethodHandler> generic_handler_;
370 
371  // callback_cq_ references the callbackable completion queue associated
372  // with this server (if any). It is set on the first call to CallbackCQ().
373  // It is _not owned_ by the server; ownership belongs with its internal
374  // shutdown callback tag (invoked when the CQ is fully shutdown).
375  // It is protected by mu_
376  CompletionQueue* callback_cq_ = nullptr;
377 };
378 
379 } // namespace grpc_impl
380 
381 #endif // GRPCPP_SERVER_IMPL_H
grpc::HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: server_impl.h:107
std::string string
Definition: config.h:35
bool health_check_service_disabled() const
NOTE: This method is not part of the public API for this class.
Definition: server_impl.h:216
Represents a gRPC server.
Definition: server_impl.h:64
NOTE: class experimental_registration_interface is not part of the public API of this class TODO(vjpa...
Definition: server_interface.h:128
::grpc_impl::Server Server
Definition: server.h:26
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
Global callbacks are a set of hooks that are called when server events occur.
Definition: server_impl.h:80
virtual void PreServerStart(Server *server)
Called before server is started.
Definition: server_impl.h:90
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
virtual void AddPort(Server *server, const grpc::string &addr, grpc::ServerCredentials *creds, int port)
Called after a server port is added.
Definition: server_impl.h:92
Definition: async_generic_service.h:74
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_impl.h:117
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:25
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
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:210
virtual void UpdateArguments(ChannelArguments *args)
Called before server is created.
Definition: server_impl.h:84
The gRPC server uses this interface to expose the health checking service without depending on protob...
Definition: health_check_service_interface_impl.h:28
Definition: sync.h:47
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Options for channel creation.
Definition: channel_arguments_impl.h:43
Definition: server_interface.h:57
::grpc_impl::HealthCheckServiceInterface HealthCheckServiceInterface
Definition: health_check_service_interface.h:29
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:129
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials_impl.h:39
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
experimental_type(Server *server)
Definition: server_impl.h:119
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:101
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:661
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_impl.h:136
Analogous to struct timespec.
Definition: gpr_types.h:47
::grpc_impl::ServerInitializer ServerInitializer
Definition: server_initializer.h:26
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:390
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder_impl.h:86
virtual ~GlobalCallbacks()
Definition: server_impl.h:82
grpc_server * server() override
Definition: server_impl.h:206
Definition: server_initializer_impl.h:34
Straightforward wrapping of the C call object.
Definition: call.h:38
Definition: sync.h:118