server_interface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #ifndef GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
  19. #define GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
  20. // IWYU pragma: private
  21. #include <grpc/impl/codegen/port_platform.h>
  22. #include <grpc/impl/codegen/grpc_types.h>
  23. #include <grpcpp/impl/codegen/byte_buffer.h>
  24. #include <grpcpp/impl/codegen/call.h>
  25. #include <grpcpp/impl/codegen/call_hook.h>
  26. #include <grpcpp/impl/codegen/completion_queue_tag.h>
  27. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  28. #include <grpcpp/impl/codegen/interceptor_common.h>
  29. #include <grpcpp/impl/codegen/rpc_service_method.h>
  30. #include <grpcpp/impl/codegen/server_context.h>
  31. namespace grpc {
  32. class AsyncGenericService;
  33. class Channel;
  34. class CompletionQueue;
  35. class GenericServerContext;
  36. class ServerCompletionQueue;
  37. class ServerCredentials;
  38. class Service;
  39. extern CoreCodegenInterface* g_core_codegen_interface;
  40. /// Models a gRPC server.
  41. ///
  42. /// Servers are configured and started via \a grpc::ServerBuilder.
  43. namespace internal {
  44. class ServerAsyncStreamingInterface;
  45. } // namespace internal
  46. class CallbackGenericService;
  47. namespace experimental {
  48. class ServerInterceptorFactoryInterface;
  49. } // namespace experimental
  50. class ServerInterface : public internal::CallHook {
  51. public:
  52. ~ServerInterface() override {}
  53. /// \a Shutdown does the following things:
  54. ///
  55. /// 1. Shutdown the server: deactivate all listening ports, mark it in
  56. /// "shutdown mode" so that further call Request's or incoming RPC matches
  57. /// are no longer allowed. Also return all Request'ed-but-not-yet-active
  58. /// calls as failed (!ok). This refers to calls that have been requested
  59. /// at the server by the server-side library or application code but that
  60. /// have not yet been matched to incoming RPCs from the client. Note that
  61. /// this would even include default calls added automatically by the gRPC
  62. /// C++ API without the user's input (e.g., "Unimplemented RPC method")
  63. ///
  64. /// 2. Block until all rpc method handlers invoked automatically by the sync
  65. /// API finish.
  66. ///
  67. /// 3. If all pending calls complete (and all their operations are
  68. /// retrieved by Next) before \a deadline expires, this finishes
  69. /// gracefully. Otherwise, forcefully cancel all pending calls associated
  70. /// with the server after \a deadline expires. In the case of the sync API,
  71. /// if the RPC function for a streaming call has already been started and
  72. /// takes a week to complete, the RPC function won't be forcefully
  73. /// terminated (since that would leave state corrupt and incomplete) and
  74. /// the method handler will just keep running (which will prevent the
  75. /// server from completing the "join" operation that it needs to do at
  76. /// shutdown time).
  77. ///
  78. /// All completion queue associated with the server (for example, for async
  79. /// serving) must be shutdown *after* this method has returned:
  80. /// See \a ServerBuilder::AddCompletionQueue for details.
  81. /// They must also be drained (by repeated Next) after being shutdown.
  82. ///
  83. /// \param deadline How long to wait until pending rpcs are forcefully
  84. /// terminated.
  85. template <class T>
  86. void Shutdown(const T& deadline) {
  87. ShutdownInternal(TimePoint<T>(deadline).raw_time());
  88. }
  89. /// Shutdown the server without a deadline and forced cancellation.
  90. ///
  91. /// All completion queue associated with the server (for example, for async
  92. /// serving) must be shutdown *after* this method has returned:
  93. /// See \a ServerBuilder::AddCompletionQueue for details.
  94. void Shutdown() {
  95. ShutdownInternal(
  96. g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
  97. }
  98. /// Block waiting for all work to complete.
  99. ///
  100. /// \warning The server must be either shutting down or some other thread must
  101. /// call \a Shutdown for this function to ever return.
  102. virtual void Wait() = 0;
  103. protected:
  104. friend class ::grpc::Service;
  105. /// Register a service. This call does not take ownership of the service.
  106. /// The service must exist for the lifetime of the Server instance.
  107. virtual bool RegisterService(const std::string* host, Service* service) = 0;
  108. /// Register a generic service. This call does not take ownership of the
  109. /// service. The service must exist for the lifetime of the Server instance.
  110. virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
  111. /// Register a callback generic service. This call does not take ownership of
  112. /// the service. The service must exist for the lifetime of the Server
  113. /// instance. May not be abstract since this is a post-1.0 API addition.
  114. virtual void RegisterCallbackGenericService(CallbackGenericService*
  115. /*service*/) {}
  116. /// Tries to bind \a server to the given \a addr.
  117. ///
  118. /// It can be invoked multiple times.
  119. ///
  120. /// \param addr The address to try to bind to the server (eg, localhost:1234,
  121. /// 192.168.1.1:31416, [::1]:27182, etc.).
  122. /// \params creds The credentials associated with the server.
  123. ///
  124. /// \return bound port number on success, 0 on failure.
  125. ///
  126. /// \warning It's an error to call this method on an already started server.
  127. virtual int AddListeningPort(const std::string& addr,
  128. ServerCredentials* creds) = 0;
  129. /// Start the server.
  130. ///
  131. /// \param cqs Completion queues for handling asynchronous services. The
  132. /// caller is required to keep all completion queues live until the server is
  133. /// destroyed.
  134. /// \param num_cqs How many completion queues does \a cqs hold.
  135. virtual void Start(::grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
  136. virtual void ShutdownInternal(gpr_timespec deadline) = 0;
  137. virtual int max_receive_message_size() const = 0;
  138. virtual grpc_server* server() = 0;
  139. void PerformOpsOnCall(internal::CallOpSetInterface* ops,
  140. internal::Call* call) override = 0;
  141. class BaseAsyncRequest : public internal::CompletionQueueTag {
  142. public:
  143. BaseAsyncRequest(ServerInterface* server, ::grpc::ServerContext* context,
  144. internal::ServerAsyncStreamingInterface* stream,
  145. ::grpc::CompletionQueue* call_cq,
  146. ::grpc::ServerCompletionQueue* notification_cq, void* tag,
  147. bool delete_on_finalize);
  148. ~BaseAsyncRequest() override;
  149. bool FinalizeResult(void** tag, bool* status) override;
  150. private:
  151. void ContinueFinalizeResultAfterInterception();
  152. protected:
  153. ServerInterface* const server_;
  154. ::grpc::ServerContext* const context_;
  155. internal::ServerAsyncStreamingInterface* const stream_;
  156. ::grpc::CompletionQueue* const call_cq_;
  157. ::grpc::ServerCompletionQueue* const notification_cq_;
  158. void* const tag_;
  159. const bool delete_on_finalize_;
  160. grpc_call* call_;
  161. internal::Call call_wrapper_;
  162. internal::InterceptorBatchMethodsImpl interceptor_methods_;
  163. bool done_intercepting_;
  164. };
  165. /// RegisteredAsyncRequest is not part of the C++ API
  166. class RegisteredAsyncRequest : public BaseAsyncRequest {
  167. public:
  168. RegisteredAsyncRequest(ServerInterface* server,
  169. ::grpc::ServerContext* context,
  170. internal::ServerAsyncStreamingInterface* stream,
  171. ::grpc::CompletionQueue* call_cq,
  172. ::grpc::ServerCompletionQueue* notification_cq,
  173. void* tag, const char* name,
  174. internal::RpcMethod::RpcType type);
  175. bool FinalizeResult(void** tag, bool* status) override {
  176. /* If we are done intercepting, then there is nothing more for us to do */
  177. if (done_intercepting_) {
  178. return BaseAsyncRequest::FinalizeResult(tag, status);
  179. }
  180. call_wrapper_ = ::grpc::internal::Call(
  181. call_, server_, call_cq_, server_->max_receive_message_size(),
  182. context_->set_server_rpc_info(name_, type_,
  183. *server_->interceptor_creators()));
  184. return BaseAsyncRequest::FinalizeResult(tag, status);
  185. }
  186. protected:
  187. void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
  188. ::grpc::ServerCompletionQueue* notification_cq);
  189. const char* name_;
  190. const internal::RpcMethod::RpcType type_;
  191. };
  192. class NoPayloadAsyncRequest final : public RegisteredAsyncRequest {
  193. public:
  194. NoPayloadAsyncRequest(internal::RpcServiceMethod* registered_method,
  195. ServerInterface* server,
  196. ::grpc::ServerContext* context,
  197. internal::ServerAsyncStreamingInterface* stream,
  198. ::grpc::CompletionQueue* call_cq,
  199. ::grpc::ServerCompletionQueue* notification_cq,
  200. void* tag)
  201. : RegisteredAsyncRequest(
  202. server, context, stream, call_cq, notification_cq, tag,
  203. registered_method->name(), registered_method->method_type()) {
  204. IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
  205. }
  206. // uses RegisteredAsyncRequest::FinalizeResult
  207. };
  208. template <class Message>
  209. class PayloadAsyncRequest final : public RegisteredAsyncRequest {
  210. public:
  211. PayloadAsyncRequest(internal::RpcServiceMethod* registered_method,
  212. ServerInterface* server, ::grpc::ServerContext* context,
  213. internal::ServerAsyncStreamingInterface* stream,
  214. ::grpc::CompletionQueue* call_cq,
  215. ::grpc::ServerCompletionQueue* notification_cq,
  216. void* tag, Message* request)
  217. : RegisteredAsyncRequest(
  218. server, context, stream, call_cq, notification_cq, tag,
  219. registered_method->name(), registered_method->method_type()),
  220. registered_method_(registered_method),
  221. request_(request) {
  222. IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
  223. notification_cq);
  224. }
  225. ~PayloadAsyncRequest() override {
  226. payload_.Release(); // We do not own the payload_
  227. }
  228. bool FinalizeResult(void** tag, bool* status) override {
  229. /* If we are done intercepting, then there is nothing more for us to do */
  230. if (done_intercepting_) {
  231. return RegisteredAsyncRequest::FinalizeResult(tag, status);
  232. }
  233. if (*status) {
  234. if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
  235. payload_.bbuf_ptr(), request_)
  236. .ok()) {
  237. // If deserialization fails, we cancel the call and instantiate
  238. // a new instance of ourselves to request another call. We then
  239. // return false, which prevents the call from being returned to
  240. // the application.
  241. g_core_codegen_interface->grpc_call_cancel_with_status(
  242. call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
  243. g_core_codegen_interface->grpc_call_unref(call_);
  244. new PayloadAsyncRequest(registered_method_, server_, context_,
  245. stream_, call_cq_, notification_cq_, tag_,
  246. request_);
  247. delete this;
  248. return false;
  249. }
  250. }
  251. /* Set interception point for recv message */
  252. interceptor_methods_.AddInterceptionHookPoint(
  253. experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
  254. interceptor_methods_.SetRecvMessage(request_, nullptr);
  255. return RegisteredAsyncRequest::FinalizeResult(tag, status);
  256. }
  257. private:
  258. internal::RpcServiceMethod* const registered_method_;
  259. Message* const request_;
  260. ByteBuffer payload_;
  261. };
  262. class GenericAsyncRequest : public BaseAsyncRequest {
  263. public:
  264. GenericAsyncRequest(ServerInterface* server, GenericServerContext* context,
  265. internal::ServerAsyncStreamingInterface* stream,
  266. ::grpc::CompletionQueue* call_cq,
  267. ::grpc::ServerCompletionQueue* notification_cq,
  268. void* tag, bool delete_on_finalize);
  269. bool FinalizeResult(void** tag, bool* status) override;
  270. private:
  271. grpc_call_details call_details_;
  272. };
  273. template <class Message>
  274. void RequestAsyncCall(internal::RpcServiceMethod* method,
  275. ::grpc::ServerContext* context,
  276. internal::ServerAsyncStreamingInterface* stream,
  277. ::grpc::CompletionQueue* call_cq,
  278. ::grpc::ServerCompletionQueue* notification_cq,
  279. void* tag, Message* message) {
  280. GPR_CODEGEN_ASSERT(method);
  281. new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
  282. notification_cq, tag, message);
  283. }
  284. void RequestAsyncCall(internal::RpcServiceMethod* method,
  285. ::grpc::ServerContext* context,
  286. internal::ServerAsyncStreamingInterface* stream,
  287. ::grpc::CompletionQueue* call_cq,
  288. ::grpc::ServerCompletionQueue* notification_cq,
  289. void* tag) {
  290. GPR_CODEGEN_ASSERT(method);
  291. new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
  292. notification_cq, tag);
  293. }
  294. void RequestAsyncGenericCall(GenericServerContext* context,
  295. internal::ServerAsyncStreamingInterface* stream,
  296. ::grpc::CompletionQueue* call_cq,
  297. ::grpc::ServerCompletionQueue* notification_cq,
  298. void* tag) {
  299. new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
  300. tag, true);
  301. }
  302. private:
  303. // EXPERIMENTAL
  304. // Getter method for the vector of interceptor factory objects.
  305. // Returns a nullptr (rather than being pure) since this is a post-1.0 method
  306. // and adding a new pure method to an interface would be a breaking change
  307. // (even though this is private and non-API)
  308. virtual std::vector<
  309. std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
  310. interceptor_creators() {
  311. return nullptr;
  312. }
  313. // A method to get the callbackable completion queue associated with this
  314. // server. If the return value is nullptr, this server doesn't support
  315. // callback operations.
  316. // TODO(vjpai): Consider a better default like using a global CQ
  317. // Returns nullptr (rather than being pure) since this is a post-1.0 method
  318. // and adding a new pure method to an interface would be a breaking change
  319. // (even though this is private and non-API)
  320. virtual ::grpc::CompletionQueue* CallbackCQ() { return nullptr; }
  321. };
  322. } // namespace grpc
  323. #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H