include/grpcpp/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 
23 
24 #include <list>
25 #include <memory>
26 #include <vector>
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() ABSL_LOCKS_EXCLUDED(mu_) override;
62 
67  void Wait() ABSL_LOCKS_EXCLUDED(mu_) 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:
115 
118  std::shared_ptr<Channel> InProcessChannelWithInterceptors(
119  const ChannelArguments& args,
120  std::vector<
121  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
123 
124  private:
126  };
127 
132 
133  protected:
136  bool RegisterService(const std::string* addr, Service* service) override;
137 
151  int AddListeningPort(const std::string& addr,
152  ServerCredentials* creds) override;
153 
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_server_config_fetcher* server_config_fetcher = nullptr,
183  grpc_resource_quota* server_rq = nullptr,
184  std::vector<
185  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
186  interceptor_creators = std::vector<std::unique_ptr<
188 
195  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
196 
197  grpc_server* server() override { return server_; }
198 
199  protected:
202  std::unique_ptr<HealthCheckServiceInterface> service) {
204  }
205 
207 
211  }
212 
213  private:
214  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
215  interceptor_creators() override {
216  return &interceptor_creators_;
217  }
218 
219  friend class AsyncGenericService;
220  friend class ServerBuilder;
221  friend class ServerInitializer;
222 
223  class SyncRequest;
224  class CallbackRequestBase;
225  template <class ServerContextType>
229 
235 
239 
244 
246  std::unique_ptr<ContextAllocator> context_allocator) {
248  }
249 
251  internal::Call* call) override;
252 
253  void ShutdownInternal(gpr_timespec deadline)
254  ABSL_LOCKS_EXCLUDED(mu_) override;
255 
256  int max_receive_message_size() const override {
258  }
259 
261 
263 
264  // Functions to manage the server shutdown ref count. Things that increase
265  // the ref count are the running state of the server (take a ref at start and
266  // drop it at shutdown) and each running callback RPC.
267  void Ref();
270 
271  std::vector<std::shared_ptr<internal::ExternalConnectionAcceptorImpl>>
273 
274  // A vector of interceptor factory objects.
275  // This should be destroyed after health_check_service_ and this requirement
276  // is satisfied by declaring interceptor_creators_ before
277  // health_check_service_. (C++ mandates that member objects be destroyed in
278  // the reverse order of initialization.)
279  std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
281 
283 
287  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
289 
293 
294  // Server status
296  bool started_;
298  bool shutdown_notified_
299  ABSL_GUARDED_BY(mu_); // Was notify called on the shutdown_cv_
301  bool shutdown_done_ ABSL_GUARDED_BY(mu_) = false;
303 
305 
306  std::shared_ptr<GlobalCallbacks> global_callbacks_;
307 
308  std::vector<std::string> services_;
311  bool has_callback_methods_ = false;
312 
313  // Pointer to the wrapped grpc_server.
315 
316  std::unique_ptr<ServerInitializer> server_initializer_;
317 
318  std::unique_ptr<ContextAllocator> context_allocator_;
319 
320  std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
322 
323  // When appropriate, use a default callback generic service to handle
324  // unimplemented methods
325  std::unique_ptr<CallbackGenericService> unimplemented_service_;
326 
327  // A special handler for resource exhausted in sync case
328  std::unique_ptr<internal::MethodHandler> resource_exhausted_handler_;
329 
330  // Handler for callback generic service, if any
331  std::unique_ptr<internal::MethodHandler> generic_handler_;
332 
333  // callback_cq_ references the callbackable completion queue associated
334  // with this server (if any). It is set on the first call to CallbackCQ().
335  // It is _not owned_ by the server; ownership belongs with its internal
336  // shutdown callback tag (invoked when the CQ is fully shutdown).
337  std::atomic<CompletionQueue*> callback_cq_{nullptr};
338 
339  // List of CQs passed in by user that must be Shutdown only after Server is
340  // Shutdown. Even though this is only used with NDEBUG, instantiate it in all
341  // cases since otherwise the size will be inconsistent.
342  std::vector<CompletionQueue*> cq_list_;
343 };
344 
345 } // namespace grpc
346 
347 #endif // GRPCPP_SERVER_H
grpc::experimental::ServerInterceptorFactoryInterface
Definition: impl/codegen/server_interceptor.h:45
compression.h
grpc::Server::server_initializer_
std::unique_ptr< ServerInitializer > server_initializer_
Definition: include/grpcpp/server.h:316
grpc::Server::max_receive_message_size_
int max_receive_message_size_
Definition: include/grpcpp/server.h:282
grpc::ServerCompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:436
grpc::Server::experimental_type::server_
Server * server_
Definition: include/grpcpp/server.h:125
grpc::AsyncGenericService
Definition: grpcpp/impl/codegen/async_generic_service.h:70
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
grpc::Server::RegisterCallbackGenericService
void RegisterCallbackGenericService(CallbackGenericService *service) override
Definition: server_cc.cc:1092
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: include/grpcpp/server.h:209
grpc::Server::unimplemented_service_
std::unique_ptr< CallbackGenericService > unimplemented_service_
Definition: include/grpcpp/server.h:325
grpc::Server::c_server
grpc_server * c_server()
Definition: server_cc.cc:989
grpc::Server::UnimplementedAsyncRequest
Definition: server_cc.cc:324
grpc
Definition: grpcpp/alarm.h:33
grpc::Server::server_
grpc_server * server_
Definition: include/grpcpp/server.h:314
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:729
grpc::Server::server
grpc_server * server() override
Definition: include/grpcpp/server.h:197
grpc_server_config_fetcher
Definition: src/core/lib/surface/server.h:497
grpc::Server::GlobalCallbacks::PreServerStart
virtual void PreServerStart(Server *)
Called before server is started.
Definition: include/grpcpp/server.h:85
grpc::Server::sync_server_cqs_
std::shared_ptr< std::vector< std::unique_ptr< ServerCompletionQueue > > > sync_server_cqs_
Definition: include/grpcpp/server.h:288
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_server_config_fetcher *server_config_fetcher=nullptr, grpc_resource_quota *server_rq=nullptr, std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >> interceptor_creators=std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >>())
false
#define false
Definition: setup_once.h:323
grpc::Server::started_
bool started_
Definition: include/grpcpp/server.h:296
callbacks
static unsigned int callbacks
Definition: benchmark-async-pummel.c:31
grpc::Server::experimental
experimental_type experimental()
Definition: include/grpcpp/server.h:131
grpc::internal::CallOpSetInterface
Definition: call_op_set_interface.h:36
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::Server::shutdown_refs_outstanding_
std::atomic_int shutdown_refs_outstanding_
Definition: include/grpcpp/server.h:302
grpc::Server::acceptors_
std::vector< std::shared_ptr< internal::ExternalConnectionAcceptorImpl > > acceptors_
Definition: include/grpcpp/server.h:272
grpc::Server::mu_
internal::Mutex mu_
Definition: include/grpcpp/server.h:295
health_check_service_interface.h
grpc::Server::shutdown_cv_
internal::CondVar shutdown_cv_
Definition: include/grpcpp/server.h:304
rpc_service_method.h
grpc::Server::ShutdownInternal
void ShutdownInternal(gpr_timespec deadline) ABSL_LOCKS_EXCLUDED(mu_) override
Definition: server_cc.cc:1245
grpc::Server::experimental_type::InProcessChannelWithInterceptors
std::shared_ptr< Channel > InProcessChannelWithInterceptors(const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Definition: server_cc.cc:1001
grpc::Server::max_receive_message_size
int max_receive_message_size() const override
Definition: include/grpcpp/server.h:256
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpcpp/impl/codegen/grpc_library.h:40
grpc::Server::has_callback_methods_
bool has_callback_methods_
Definition: include/grpcpp/server.h:311
grpc::Server::has_callback_generic_service_
bool has_callback_generic_service_
Definition: include/grpcpp/server.h:310
call
FilterStackCall * call
Definition: call.cc:750
grpc::Server::health_check_service_disabled_
bool health_check_service_disabled_
Definition: include/grpcpp/server.h:321
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: grpcpp/impl/codegen/service_type.h:58
grpc::Server::initializer
ServerInitializer * initializer()
Definition: server_cc.cc:1362
grpc::Server::SyncRequestThreadManager
Definition: server_cc.cc:776
grpc::ContextAllocator
Definition: grpcpp/impl/codegen/server_context.h:650
grpc::Server::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Definition: server_cc.cc:983
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc::Server::experimental_type::experimental_type
experimental_type(Server *server)
Definition: include/grpcpp/server.h:114
grpc::Server::CallbackCQ
CompletionQueue * CallbackCQ() ABSL_LOCKS_EXCLUDED(mu_) override
Definition: server_cc.cc:1366
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
channel_arguments.h
grpc::Server::Ref
void Ref()
Definition: server_cc.cc:1118
grpc::Server::SyncRequest
Definition: server_cc.cc:365
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpc::Server::RegisterAsyncGenericService
void RegisterAsyncGenericService(AsyncGenericService *service) override
Definition: server_cc.cc:1085
grpc::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: include/grpcpp/security/server_credentials.h:76
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
grpc::Server::generic_handler_
std::unique_ptr< internal::MethodHandler > generic_handler_
Definition: include/grpcpp/server.h:331
grpc::Server::global_callbacks_
std::shared_ptr< GlobalCallbacks > global_callbacks_
Definition: include/grpcpp/server.h:306
grpc::Server::health_check_service_
std::unique_ptr< HealthCheckServiceInterface > health_check_service_
Definition: include/grpcpp/server.h:320
completion_queue.h
grpc::Server::AddListeningPort
int AddListeningPort(const std::string &addr, ServerCredentials *creds) override
Definition: server_cc.cc:1110
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: include/grpcpp/server.h:201
channel.h
completion_queue.h
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
grpc::ServerInterface
Definition: grpcpp/impl/codegen/server_interface.h:61
grpc::Server::context_allocator_
std::unique_ptr< ContextAllocator > context_allocator_
Definition: include/grpcpp/server.h:318
grpc::Server::services_
std::vector< std::string > services_
Definition: include/grpcpp/server.h:308
grpc::Server::GlobalCallbacks::~GlobalCallbacks
virtual ~GlobalCallbacks()
Definition: include/grpcpp/server.h:77
grpc::Server::resource_exhausted_handler_
std::unique_ptr< internal::MethodHandler > resource_exhausted_handler_
Definition: include/grpcpp/server.h:328
config.h
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: include/grpcpp/impl/codegen/call.h:37
grpc::Server::GlobalCallbacks::UpdateArguments
virtual void UpdateArguments(ChannelArguments *)
Called before server is created.
Definition: include/grpcpp/server.h:79
grpc::HealthCheckServiceInterface
Definition: grpcpp/health_check_service_interface.h:31
grpc::CallbackGenericService
Definition: grpcpp/impl/codegen/async_generic_service.h:104
server_credentials.h
client_interceptor.h
grpc::Server::experimental_type
Definition: include/grpcpp/server.h:112
grpc_library.h
grpc::Server::RegisterService
bool RegisterService(const std::string *addr, Service *service) override
Definition: server_cc.cc:1026
grpc::Server::GlobalCallbacks
Definition: include/grpcpp/server.h:75
grpc::Server::callback_cq_
std::atomic< CompletionQueue * > callback_cq_
Definition: include/grpcpp/server.h:337
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
shutdown_
bool shutdown_
Definition: pick_first.cc:173
grpc::Server::UnrefAndWaitLocked
void UnrefAndWaitLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: server_cc.cc:1134
call.h
grpc::Server::GetHealthCheckService
HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: include/grpcpp/server.h:102
grpc::Server::InProcessChannel
std::shared_ptr< Channel > InProcessChannel(const ChannelArguments &args)
Establish a channel for in-process communication.
Definition: server_cc.cc:991
grpc::Server::RegisterContextAllocator
void RegisterContextAllocator(std::unique_ptr< ContextAllocator > context_allocator)
Definition: include/grpcpp/server.h:245
server
Definition: examples/python/async_streaming/server.py:1
grpc::Server::shutdown_done_cv_
internal::CondVar shutdown_done_cv_
Definition: include/grpcpp/server.h:300
ABSL_LOCKS_EXCLUDED
#define ABSL_LOCKS_EXCLUDED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:163
grpc::Server::Start
void Start(ServerCompletionQueue **cqs, size_t num_cqs) override
Definition: server_cc.cc:1145
grpc::Server
Definition: include/grpcpp/server.h:59
port_platform.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
grpc::Server::cq_list_
std::vector< CompletionQueue * > cq_list_
Definition: include/grpcpp/server.h:342
grpc::Server::interceptor_creators_
std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface > > interceptor_creators_
Definition: include/grpcpp/server.h:280
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::Server::context_allocator
ContextAllocator * context_allocator()
Definition: include/grpcpp/server.h:206
grpc::Server::ABSL_GUARDED_BY
bool shutdown_ ABSL_GUARDED_BY(mu_)
grpc::Server::Wait
void Wait() ABSL_LOCKS_EXCLUDED(mu_) override
Definition: server_cc.cc:1323
server_interface.h
grpc::Server::has_async_generic_service_
bool has_async_generic_service_
Definition: include/grpcpp/server.h:309
grpc::Server::UnimplementedAsyncResponse
Definition: server_cc.cc:341
grpc::internal::CondVar
Definition: include/grpcpp/impl/codegen/sync.h:124
internal
Definition: benchmark/test/output_test_helper.cc:20
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::Server::GlobalCallbacks::PreSynchronousRequest
virtual void PreSynchronousRequest(ServerContext *context)=0
Called before application callback for each synchronous server request.
atm.h
grpc::Server::UnrefWithPossibleNotify
void UnrefWithPossibleNotify() ABSL_LOCKS_EXCLUDED(mu_)
Definition: server_cc.cc:1122
grpc::CompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:104
gpr_timespec
Definition: gpr_types.h:50
grpc::Server::PerformOpsOnCall
void PerformOpsOnCall(internal::CallOpSetInterface *ops, internal::Call *call) override
Definition: server_cc.cc:1330
grpc::Server::~Server
~Server() ABSL_LOCKS_EXCLUDED(mu_) override
Definition: server_cc.cc:952
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc::Server::GlobalCallbacks::PostSynchronousRequest
virtual void PostSynchronousRequest(ServerContext *context)=0
Called after application callback for each synchronous server request.
grpc::Server::sync_req_mgrs_
std::vector< std::unique_ptr< SyncRequestThreadManager > > sync_req_mgrs_
Definition: include/grpcpp/server.h:292
grpc::Server::CallbackRequest
Definition: include/grpcpp/server.h:226
grpc::ServerInitializer
Definition: grpcpp/impl/server_initializer.h:31
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
grpc::Server::interceptor_creators
std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface > > * interceptor_creators() override
Definition: include/grpcpp/server.h:215
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
grpc::Server::GlobalCallbacks::AddPort
virtual void AddPort(Server *, const std::string &, ServerCredentials *, int)
Called after a server port is added.
Definition: include/grpcpp/server.h:87
status.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:16