GRPC C++  1.0.0
server.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, 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_H
35 #define GRPCXX_SERVER_H
36 
37 #include <list>
38 #include <memory>
39 #include <vector>
40 
42 #include <grpc++/impl/call.h>
46 #include <grpc++/impl/sync.h>
49 #include <grpc++/support/config.h>
50 #include <grpc++/support/status.h>
51 #include <grpc/compression.h>
52 
53 struct grpc_server;
54 
55 namespace grpc {
56 
57 class GenericServerContext;
58 class AsyncGenericService;
59 class ServerAsyncStreamingInterface;
60 class ServerContext;
61 class ServerInitializer;
62 class ThreadPoolInterface;
63 
68  public:
69  ~Server();
70 
75  void Wait() GRPC_OVERRIDE;
76 
82  public:
83  virtual ~GlobalCallbacks() {}
85  virtual void UpdateArguments(ChannelArguments* args) {}
87  virtual void PreSynchronousRequest(ServerContext* context) = 0;
89  virtual void PostSynchronousRequest(ServerContext* context) = 0;
90  };
94  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
95 
96  // Returns a \em raw pointer to the underlying grpc_server instance.
98 
99  // Returns a \em raw pointer to the underlying CompletionQueue.
101 
102  private:
103  friend class AsyncGenericService;
104  friend class ServerBuilder;
105  friend class ServerInitializer;
106 
107  class SyncRequest;
108  class AsyncRequest;
109  class ShutdownRequest;
110 
111  class UnimplementedAsyncRequestContext;
112  class UnimplementedAsyncRequest;
113  class UnimplementedAsyncResponse;
114 
121  Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
122  int max_message_size, ChannelArguments* args);
123 
126  bool RegisterService(const grpc::string* host,
127  Service* service) GRPC_OVERRIDE;
128 
131  void RegisterAsyncGenericService(AsyncGenericService* service) GRPC_OVERRIDE;
132 
144  int AddListeningPort(const grpc::string& addr,
146 
155  bool Start(ServerCompletionQueue** cqs, size_t num_cqs) GRPC_OVERRIDE;
156 
158  void RunRpc() GRPC_OVERRIDE;
159 
161  void ScheduleCallback() GRPC_OVERRIDE;
162 
163  void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE;
164 
165  void ShutdownInternal(gpr_timespec deadline) GRPC_OVERRIDE;
166 
167  int max_message_size() const GRPC_OVERRIDE { return max_message_size_; };
168 
169  grpc_server* server() GRPC_OVERRIDE { return server_; };
170 
171  ServerInitializer* initializer();
172 
173  const int max_message_size_;
174 
175  // Completion queue.
176  CompletionQueue cq_;
177 
178  // Sever status
179  grpc::mutex mu_;
180  bool started_;
181  bool shutdown_;
182  bool shutdown_notified_;
183  // The number of threads which are running callbacks.
184  int num_running_cb_;
185  grpc::condition_variable callback_cv_;
186 
187  grpc::condition_variable shutdown_cv_;
188 
189  std::shared_ptr<GlobalCallbacks> global_callbacks_;
190 
191  std::list<SyncRequest>* sync_methods_;
192  std::vector<grpc::string> services_;
193  std::unique_ptr<RpcServiceMethod> unknown_method_;
194  bool has_generic_service_;
195 
196  // Pointer to the c grpc server.
197  grpc_server* server_;
198 
199  ThreadPoolInterface* thread_pool_;
200  // Whether the thread pool is created and owned by the server.
201  bool thread_pool_owned_;
202 
203  std::unique_ptr<ServerInitializer> server_initializer_;
204 };
205 
206 } // namespace grpc
207 
208 #endif // GRPCXX_SERVER_H
std::string string
Definition: config.h:118
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:563
Options for channel creation.
Definition: channel_arguments.h:52
Definition: service_type.h:64
Definition: sync_no_cxx11.h:45
Definition: server_initializer.h:47
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:57
virtual ~GlobalCallbacks()
Definition: server.h:83
Definition: async_generic_service.h:60
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Set the global callback object.
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:53
Definition: sync_no_cxx11.h:87
CompletionQueue * completion_queue()
virtual void PreSynchronousRequest(ServerContext *context)=0
Called before application callback for each synchronous server request.
Definition: server_credentials.h:50
Models a gRPC server.
Definition: server.h:67
Definition: call.h:645
Definition: alarm.h:48
Definition: server_context.h:91
Models a gRPC server.
Definition: server_interface.h:58
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:97
#define GRPC_FINAL
Definition: config.h:72
Global Callbacks.
Definition: server.h:81
Definition: time.h:63
grpc_server * c_server()
virtual void UpdateArguments(ChannelArguments *args)
Called before server is created.
Definition: server.h:85
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:235
void Wait() GRPC_OVERRIDE
Block waiting for all work to complete.
virtual void PostSynchronousRequest(ServerContext *context)=0
Called after application callback for each synchronous server request.
#define GRPC_OVERRIDE
Definition: config.h:78
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:61