blob: 2cfeb359fc08c2162e20f064ea65c61ffcdc56dd [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_SERVER_H
35#define GRPCXX_SERVER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Craig Tillercbd04852015-02-10 17:39:54 -080037#include <list>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <memory>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
40#include <grpc++/completion_queue.h>
41#include <grpc++/config.h>
Craig Tillerbb5227f2015-02-11 13:34:48 -080042#include <grpc++/impl/call.h>
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070043#include <grpc++/impl/grpc_library.h>
Craig Tiller1c9a2a92015-02-12 14:10:25 -080044#include <grpc++/impl/service_type.h>
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020045#include <grpc++/impl/sync.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046#include <grpc++/status.h>
47
48struct grpc_server;
49
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050namespace grpc {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080051class AsynchronousService;
Yang Gao005eb882015-03-11 22:17:13 -070052class GenericServerContext;
Yang Gao49996492015-03-12 16:40:19 -070053class AsyncGenericService;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054class RpcService;
55class RpcServiceMethod;
yangg9e21f722014-12-08 15:49:52 -080056class ServerCredentials;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057class ThreadPoolInterface;
58
59// Currently it only supports handling rpcs in a single thread.
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070060class Server GRPC_FINAL : public GrpcLibrary,
61 private CallHook,
Craig Tillercf133f42015-02-26 14:05:56 -080062 private AsynchronousService::DispatchImpl {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063 public:
64 ~Server();
65
66 // Shutdown the server, block until all rpc processing finishes.
67 void Shutdown();
68
Craig Tiller6e57b9e2015-02-24 15:46:22 -080069 // 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 private:
Yang Gao49996492015-03-12 16:40:19 -070075 friend class AsyncGenericService;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076 friend class ServerBuilder;
77
Craig Tiller1c9a2a92015-02-12 14:10:25 -080078 class SyncRequest;
79 class AsyncRequest;
Craig Tillerbce999f2015-05-27 09:55:51 -070080 class ShutdownRequest;
Craig Tillercbd04852015-02-10 17:39:54 -080081
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082 // ServerBuilder use only
Yang Gao3921c562015-04-30 16:07:06 -070083 Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
84 int max_message_size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 // Register a service. This call does not take ownership of the service.
86 // The service must exist for the lifetime of the Server instance.
Craig Tiller0db1bef2015-02-09 13:47:39 -080087 bool RegisterService(RpcService* service);
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080088 bool RegisterAsyncService(AsynchronousService* service);
Yang Gao49996492015-03-12 16:40:19 -070089 void RegisterAsyncGenericService(AsyncGenericService* service);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 // Add a listening port. Can be called multiple times.
Nicolas Noblecfd60732015-03-18 16:27:43 -070091 int AddListeningPort(const grpc::string& addr, ServerCredentials* creds);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 // Start the server.
Craig Tiller0db1bef2015-02-09 13:47:39 -080093 bool Start();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 void HandleQueueClosed();
96 void RunRpc();
97 void ScheduleCallback();
98
Craig Tillercf133f42015-02-26 14:05:56 -080099 void PerformOpsOnCall(CallOpBuffer* ops, Call* call) GRPC_OVERRIDE;
Craig Tillerbb5227f2015-02-11 13:34:48 -0800100
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800101 // DispatchImpl
102 void RequestAsyncCall(void* registered_method, ServerContext* context,
Yang Gao7694c352015-03-03 09:48:06 -0800103 grpc::protobuf::Message* request,
Craig Tiller573523f2015-02-17 07:38:26 -0800104 ServerAsyncStreamingInterface* stream,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700105 CompletionQueue* call_cq,
106 ServerCompletionQueue* notification_cq,
107 void* tag) GRPC_OVERRIDE;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800108
Yang Gao49996492015-03-12 16:40:19 -0700109 void RequestAsyncGenericCall(GenericServerContext* context,
110 ServerAsyncStreamingInterface* stream,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700111 CompletionQueue* cq,
112 ServerCompletionQueue* notification_cq,
113 void* tag);
Yang Gao1c402332015-03-05 16:39:25 -0800114
Yang Gao3921c562015-04-30 16:07:06 -0700115 const int max_message_size_;
116
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 // Completion queue.
Craig Tiller3b29b562015-02-11 12:58:46 -0800118 CompletionQueue cq_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
120 // Sever status
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200121 grpc::mutex mu_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122 bool started_;
123 bool shutdown_;
124 // The number of threads which are running callbacks.
125 int num_running_cb_;
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200126 grpc::condition_variable callback_cv_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127
Nicolas Noble30862032015-04-24 18:17:45 -0700128 std::list<SyncRequest>* sync_methods_;
Craig Tillercbd04852015-02-10 17:39:54 -0800129
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130 // Pointer to the c grpc server.
Craig Tiller42bc87c2015-02-23 08:50:19 -0800131 grpc_server* const server_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 ThreadPoolInterface* thread_pool_;
134 // Whether the thread pool is created and owned by the server.
135 bool thread_pool_owned_;
Nicolas Noble30862032015-04-24 18:17:45 -0700136 private:
Yang Gao3921c562015-04-30 16:07:06 -0700137 Server() : max_message_size_(-1), server_(NULL) { abort(); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138};
139
140} // namespace grpc
141
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100142#endif // GRPCXX_SERVER_H