GRPC C++  0.13.1-pre1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
server.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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 
41 #include <grpc++/impl/call.h>
45 #include <grpc++/impl/sync.h>
48 #include <grpc++/support/config.h>
49 #include <grpc++/support/status.h>
50 #include <grpc/compression.h>
51 
52 struct grpc_server;
53 
54 namespace grpc {
55 
56 class GenericServerContext;
57 class AsyncGenericService;
58 class ServerAsyncStreamingInterface;
59 class ServerContext;
60 class ThreadPoolInterface;
61 
65 class Server GRPC_FINAL : public ServerInterface, private GrpcLibrary {
66  public:
67  ~Server();
68 
73  void Wait() GRPC_OVERRIDE;
74 
80  public:
81  virtual ~GlobalCallbacks() {}
83  virtual void PreSynchronousRequest(ServerContext* context) = 0;
85  virtual void PostSynchronousRequest(ServerContext* context) = 0;
86  };
90  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
91 
92  private:
93  friend class AsyncGenericService;
94  friend class ServerBuilder;
95 
96  class SyncRequest;
97  class AsyncRequest;
98  class ShutdownRequest;
99 
100  class UnimplementedAsyncRequestContext;
101  class UnimplementedAsyncRequest;
102  class UnimplementedAsyncResponse;
103 
110  Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
111  int max_message_size, const ChannelArguments& args);
112 
115  bool RegisterService(const grpc::string* host,
116  Service* service) GRPC_OVERRIDE;
117 
120  void RegisterAsyncGenericService(AsyncGenericService* service) GRPC_OVERRIDE;
121 
133  int AddListeningPort(const grpc::string& addr,
135 
144  bool Start(ServerCompletionQueue** cqs, size_t num_cqs) GRPC_OVERRIDE;
145 
147  void RunRpc() GRPC_OVERRIDE;
148 
150  void ScheduleCallback() GRPC_OVERRIDE;
151 
152  void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE;
153 
154  void ShutdownInternal(gpr_timespec deadline) GRPC_OVERRIDE;
155 
156  int max_message_size() const GRPC_OVERRIDE { return max_message_size_; };
157 
158  grpc_server* server() GRPC_OVERRIDE { return server_; };
159 
160  const int max_message_size_;
161 
162  // Completion queue.
163  CompletionQueue cq_;
164 
165  // Sever status
166  grpc::mutex mu_;
167  bool started_;
168  bool shutdown_;
169  // The number of threads which are running callbacks.
170  int num_running_cb_;
171  grpc::condition_variable callback_cv_;
172 
173  std::shared_ptr<GlobalCallbacks> global_callbacks_;
174 
175  std::list<SyncRequest>* sync_methods_;
176  std::unique_ptr<RpcServiceMethod> unknown_method_;
177  bool has_generic_service_;
178 
179  // Pointer to the c grpc server.
180  grpc_server* const server_;
181 
182  ThreadPoolInterface* thread_pool_;
183  // Whether the thread pool is created and owned by the server.
184  bool thread_pool_owned_;
185 };
186 
187 } // namespace grpc
188 
189 #endif // GRPCXX_SERVER_H
std::string string
Definition: config.h:112
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:494
Options for channel creation.
Definition: channel_arguments.h:52
Definition: service_type.h:63
Definition: sync_no_cxx11.h:45
virtual ~GlobalCallbacks()
Definition: server.h:81
Definition: async_generic_service.h:60
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Set the global callback object.
Definition: sync_no_cxx11.h:87
virtual void PreSynchronousRequest(ServerContext *context)=0
Called before application callback for each synchronous server request.
Definition: grpc_library.h:49
Definition: server_credentials.h:49
Models a gRPC server.
Definition: server.h:65
Definition: call.h:576
Definition: server_context.h:90
Models a gRPC server.
Definition: server_interface.h:57
#define GRPC_FINAL
Definition: config.h:71
Global Callbacks.
Definition: server.h:79
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:194
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:77
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:55