gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
server.h
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 
40 #include <grpc++/completion_queue.h>
41 #include <grpc++/config.h>
42 #include <grpc++/impl/call.h>
43 #include <grpc++/impl/grpc_library.h>
44 #include <grpc++/impl/service_type.h>
45 #include <grpc++/impl/sync.h>
46 #include <grpc++/status.h>
47 
48 struct grpc_server;
49 
50 namespace grpc {
51 class AsynchronousService;
52 class GenericServerContext;
53 class AsyncGenericService;
54 class RpcService;
55 class RpcServiceMethod;
56 class ServerCredentials;
57 class ThreadPoolInterface;
58 
59 // Currently it only supports handling rpcs in a single thread.
60 class Server GRPC_FINAL : public GrpcLibrary,
61  private CallHook,
62  private AsynchronousService::DispatchImpl {
63  public:
64  ~Server();
65 
66  // Shutdown the server, block until all rpc processing finishes.
67  void Shutdown();
68 
69  // Block waiting for all work to complete (the server must either
70  // be shutting down or some other thread must call Shutdown for this
71  // function to ever return)
72  void Wait();
73 
74  private:
75  friend class AsyncGenericService;
76  friend class ServerBuilder;
77 
78  class SyncRequest;
79  class AsyncRequest;
80 
81  // ServerBuilder use only
82  Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
83  int max_message_size);
84  // Register a service. This call does not take ownership of the service.
85  // The service must exist for the lifetime of the Server instance.
86  bool RegisterService(RpcService* service);
87  bool RegisterAsyncService(AsynchronousService* service);
88  void RegisterAsyncGenericService(AsyncGenericService* service);
89  // Add a listening port. Can be called multiple times.
90  int AddListeningPort(const grpc::string& addr, ServerCredentials* creds);
91  // Start the server.
92  bool Start();
93 
94  void HandleQueueClosed();
95  void RunRpc();
96  void ScheduleCallback();
97 
98  void PerformOpsOnCall(CallOpBuffer* ops, Call* call) GRPC_OVERRIDE;
99 
100  // DispatchImpl
101  void RequestAsyncCall(void* registered_method, ServerContext* context,
102  grpc::protobuf::Message* request,
103  ServerAsyncStreamingInterface* stream,
104  CompletionQueue* call_cq,
105  ServerCompletionQueue* notification_cq,
106  void* tag) GRPC_OVERRIDE;
107 
108  void RequestAsyncGenericCall(GenericServerContext* context,
109  ServerAsyncStreamingInterface* stream,
110  CompletionQueue* cq,
111  ServerCompletionQueue* notification_cq,
112  void* tag);
113 
114  const int max_message_size_;
115 
116  // Completion queue.
117  CompletionQueue cq_;
118 
119  // Sever status
120  grpc::mutex mu_;
121  bool started_;
122  bool shutdown_;
123  // The number of threads which are running callbacks.
124  int num_running_cb_;
125  grpc::condition_variable callback_cv_;
126 
127  std::list<SyncRequest>* sync_methods_;
128 
129  // Pointer to the c grpc server.
130  grpc_server* const server_;
131 
132  ThreadPoolInterface* thread_pool_;
133  // Whether the thread pool is created and owned by the server.
134  bool thread_pool_owned_;
135  private:
136  Server() : max_message_size_(-1), server_(NULL) { abort(); }
137 };
138 
139 } // namespace grpc
140 
141 #endif // GRPCXX_SERVER_H
Definition: sync_no_cxx11.h:45
Definition: _completion_queue.h:40
Definition: server.c:100
Definition: sync_no_cxx11.h:84
Definition: proto_utils.cc:45
Definition: chttp2_transport.c:307
Definition: _call.h:44
Definition: _server_credentials.h:40
Definition: channel_create.c:62
Definition: _server.h:42
Definition: server.c:127