diff --git a/BUILD b/BUILD index 9c37cdd6b00..c8ad45a7887 100644 --- a/BUILD +++ b/BUILD @@ -2066,6 +2066,7 @@ grpc_cc_library( "include/grpcpp/impl/codegen/call.h", "include/grpcpp/impl/codegen/call_hook.h", "include/grpcpp/impl/codegen/call_op_set.h", + "include/grpcpp/impl/codegen/call_op_set_interface.h", "include/grpcpp/impl/codegen/callback_common.h", "include/grpcpp/impl/codegen/channel_interface.h", "include/grpcpp/impl/codegen/client_callback.h", @@ -2080,6 +2081,7 @@ grpc_cc_library( "include/grpcpp/impl/codegen/grpc_library.h", "include/grpcpp/impl/codegen/intercepted_channel.h", "include/grpcpp/impl/codegen/interceptor.h", + "include/grpcpp/impl/codegen/interceptor_common.h", "include/grpcpp/impl/codegen/metadata_map.h", "include/grpcpp/impl/codegen/method_handler_impl.h", "include/grpcpp/impl/codegen/rpc_method.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dca88c63d9..3b41ec6c6ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3114,6 +3114,7 @@ foreach(_hdr include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h include/grpcpp/impl/codegen/call_op_set.h + include/grpcpp/impl/codegen/call_op_set_interface.h include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h @@ -3128,6 +3129,7 @@ foreach(_hdr include/grpcpp/impl/codegen/grpc_library.h include/grpcpp/impl/codegen/intercepted_channel.h include/grpcpp/impl/codegen/interceptor.h + include/grpcpp/impl/codegen/interceptor_common.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/rpc_method.h @@ -3692,6 +3694,7 @@ foreach(_hdr include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h include/grpcpp/impl/codegen/call_op_set.h + include/grpcpp/impl/codegen/call_op_set_interface.h include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h @@ -3706,6 +3709,7 @@ foreach(_hdr include/grpcpp/impl/codegen/grpc_library.h include/grpcpp/impl/codegen/intercepted_channel.h include/grpcpp/impl/codegen/interceptor.h + include/grpcpp/impl/codegen/interceptor_common.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/rpc_method.h @@ -4104,6 +4108,7 @@ foreach(_hdr include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h include/grpcpp/impl/codegen/call_op_set.h + include/grpcpp/impl/codegen/call_op_set_interface.h include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h @@ -4118,6 +4123,7 @@ foreach(_hdr include/grpcpp/impl/codegen/grpc_library.h include/grpcpp/impl/codegen/intercepted_channel.h include/grpcpp/impl/codegen/interceptor.h + include/grpcpp/impl/codegen/interceptor_common.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/rpc_method.h @@ -4288,6 +4294,7 @@ foreach(_hdr include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h include/grpcpp/impl/codegen/call_op_set.h + include/grpcpp/impl/codegen/call_op_set_interface.h include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h @@ -4302,6 +4309,7 @@ foreach(_hdr include/grpcpp/impl/codegen/grpc_library.h include/grpcpp/impl/codegen/intercepted_channel.h include/grpcpp/impl/codegen/interceptor.h + include/grpcpp/impl/codegen/interceptor_common.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/rpc_method.h @@ -4616,6 +4624,7 @@ foreach(_hdr include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h include/grpcpp/impl/codegen/call_op_set.h + include/grpcpp/impl/codegen/call_op_set_interface.h include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h @@ -4630,6 +4639,7 @@ foreach(_hdr include/grpcpp/impl/codegen/grpc_library.h include/grpcpp/impl/codegen/intercepted_channel.h include/grpcpp/impl/codegen/interceptor.h + include/grpcpp/impl/codegen/interceptor_common.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/rpc_method.h diff --git a/Makefile b/Makefile index 7781b3d5772..aafdebb6ca5 100644 --- a/Makefile +++ b/Makefile @@ -5461,6 +5461,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ + include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -5475,6 +5476,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ + include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/rpc_method.h \ @@ -6048,6 +6050,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ + include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -6062,6 +6065,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ + include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/rpc_method.h \ @@ -6445,6 +6449,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ + include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -6459,6 +6464,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ + include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/rpc_method.h \ @@ -6606,6 +6612,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ + include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -6620,6 +6627,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ + include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/rpc_method.h \ @@ -6939,6 +6947,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ + include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -6953,6 +6962,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ + include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/rpc_method.h \ diff --git a/build.yaml b/build.yaml index 6719c3e9c02..e5955007d4d 100644 --- a/build.yaml +++ b/build.yaml @@ -1223,6 +1223,7 @@ filegroups: - include/grpcpp/impl/codegen/call.h - include/grpcpp/impl/codegen/call_hook.h - include/grpcpp/impl/codegen/call_op_set.h + - include/grpcpp/impl/codegen/call_op_set_interface.h - include/grpcpp/impl/codegen/callback_common.h - include/grpcpp/impl/codegen/channel_interface.h - include/grpcpp/impl/codegen/client_callback.h @@ -1237,6 +1238,7 @@ filegroups: - include/grpcpp/impl/codegen/grpc_library.h - include/grpcpp/impl/codegen/intercepted_channel.h - include/grpcpp/impl/codegen/interceptor.h + - include/grpcpp/impl/codegen/interceptor_common.h - include/grpcpp/impl/codegen/metadata_map.h - include/grpcpp/impl/codegen/method_handler_impl.h - include/grpcpp/impl/codegen/rpc_method.h diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index fb5d12ea171..fdbe7db9af8 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -129,6 +129,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/call.h', 'include/grpcpp/impl/codegen/call_hook.h', 'include/grpcpp/impl/codegen/call_op_set.h', + 'include/grpcpp/impl/codegen/call_op_set_interface.h', 'include/grpcpp/impl/codegen/callback_common.h', 'include/grpcpp/impl/codegen/channel_interface.h', 'include/grpcpp/impl/codegen/client_callback.h', @@ -143,6 +144,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/grpc_library.h', 'include/grpcpp/impl/codegen/intercepted_channel.h', 'include/grpcpp/impl/codegen/interceptor.h', + 'include/grpcpp/impl/codegen/interceptor_common.h', 'include/grpcpp/impl/codegen/metadata_map.h', 'include/grpcpp/impl/codegen/method_handler_impl.h', 'include/grpcpp/impl/codegen/rpc_method.h', diff --git a/include/grpcpp/impl/codegen/call_op_set.h b/include/grpcpp/impl/codegen/call_op_set.h index c7149081114..98eb586065f 100644 --- a/include/grpcpp/impl/codegen/call_op_set.h +++ b/include/grpcpp/impl/codegen/call_op_set.h @@ -1,6 +1,6 @@ /* * - * Copyright 2015 gRPC authors. + * Copyright 2018 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,16 +30,15 @@ #include #include #include +#include #include -#include #include #include #include #include +#include #include -#include #include -#include #include #include @@ -207,38 +206,6 @@ class WriteOptions { namespace internal { -/// Internal methods for setting the state -class InternalInterceptorBatchMethods - : public experimental::InterceptorBatchMethods { - public: - virtual ~InternalInterceptorBatchMethods() {} - - virtual void AddInterceptionHookPoint( - experimental::InterceptionHookPoints type) = 0; - - virtual void SetSendMessage(ByteBuffer* buf) = 0; - - virtual void SetSendInitialMetadata( - std::multimap* metadata) = 0; - - virtual void SetSendStatus(grpc_status_code* code, - grpc::string* error_details, - grpc::string* error_message) = 0; - - virtual void SetSendTrailingMetadata( - std::multimap* metadata) = 0; - - virtual void SetRecvMessage(void* message) = 0; - - virtual void SetRecvInitialMetadata(internal::MetadataMap* map) = 0; - - virtual void SetRecvStatus(Status* status) = 0; - - virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) = 0; - - virtual std::unique_ptr GetInterceptedChannel() = 0; -}; - /// Default argument for CallOpSet. I is unused by the class, but can be /// used for generating multiple names for the same thing. template @@ -783,364 +750,11 @@ class CallOpClientRecvStatus { grpc_slice error_message_; }; -/// An abstract collection of call ops, used to generate the -/// grpc_call_op structure to pass down to the lower layers, -/// and as it is-a CompletionQueueTag, also massages the final -/// completion into the correct form for consumption in the C++ -/// API. -class CallOpSetInterface : public CompletionQueueTag { - public: - /// Fills in grpc_op, starting from ops[*nops] and moving - /// upwards. - virtual void FillOps(internal::Call* call) = 0; - - /// Get the tag to be used at the core completion queue. Generally, the - /// value of cq_tag will be "this". However, it can be overridden if we - /// want core to process the tag differently (e.g., as a core callback) - virtual void* cq_tag() = 0; - - // This will be called while interceptors are run if the RPC is a hijacked - // RPC. This should set hijacking state for each of the ops. - virtual void SetHijackingState() = 0; - - // Should be called after interceptors are done running - virtual void ContinueFillOpsAfterInterception() = 0; - - // Should be called after interceptors are done running on the finalize result - // path - virtual void ContinueFinalizeResultAfterInterception() = 0; -}; - template , class Op2 = CallNoOp<2>, class Op3 = CallNoOp<3>, class Op4 = CallNoOp<4>, class Op5 = CallNoOp<5>, class Op6 = CallNoOp<6>> class CallOpSet; -class InterceptorBatchMethodsImpl : public InternalInterceptorBatchMethods { - public: - InterceptorBatchMethodsImpl() { - for (auto i = 0; - i < static_cast( - experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS); - i++) { - hooks_[i] = false; - } - } - - virtual ~InterceptorBatchMethodsImpl() {} - - virtual bool QueryInterceptionHookPoint( - experimental::InterceptionHookPoints type) override { - return hooks_[static_cast(type)]; - } - - virtual void Proceed() override { /* fill this */ - if (call_->client_rpc_info() != nullptr) { - return ProceedClient(); - } - GPR_CODEGEN_ASSERT(call_->server_rpc_info() != nullptr); - ProceedServer(); - } - - virtual void Hijack() override { - // Only the client can hijack when sending down initial metadata - GPR_CODEGEN_ASSERT(!reverse_ && ops_ != nullptr && - call_->client_rpc_info() != nullptr); - // It is illegal to call Hijack twice - GPR_CODEGEN_ASSERT(!ran_hijacking_interceptor_); - auto* rpc_info = call_->client_rpc_info(); - rpc_info->hijacked_ = true; - rpc_info->hijacked_interceptor_ = curr_iteration_; - ClearHookPoints(); - ops_->SetHijackingState(); - ran_hijacking_interceptor_ = true; - rpc_info->RunInterceptor(this, curr_iteration_); - } - - virtual void AddInterceptionHookPoint( - experimental::InterceptionHookPoints type) override { - hooks_[static_cast(type)] = true; - } - - virtual ByteBuffer* GetSendMessage() override { return send_message_; } - - virtual std::multimap* GetSendInitialMetadata() - override { - return send_initial_metadata_; - } - - virtual Status GetSendStatus() override { - return Status(static_cast(*code_), *error_message_, - *error_details_); - } - - virtual void ModifySendStatus(const Status& status) override { - *code_ = static_cast(status.error_code()); - *error_details_ = status.error_details(); - *error_message_ = status.error_message(); - } - - virtual std::multimap* GetSendTrailingMetadata() - override { - return send_trailing_metadata_; - } - - virtual void* GetRecvMessage() override { return recv_message_; } - - virtual std::multimap* - GetRecvInitialMetadata() override { - return recv_initial_metadata_->map(); - } - - virtual Status* GetRecvStatus() override { return recv_status_; } - - virtual std::multimap* - GetRecvTrailingMetadata() override { - return recv_trailing_metadata_->map(); - } - - virtual void SetSendMessage(ByteBuffer* buf) override { send_message_ = buf; } - - virtual void SetSendInitialMetadata( - std::multimap* metadata) override { - send_initial_metadata_ = metadata; - } - - virtual void SetSendStatus(grpc_status_code* code, - grpc::string* error_details, - grpc::string* error_message) override { - code_ = code; - error_details_ = error_details; - error_message_ = error_message; - } - - virtual void SetSendTrailingMetadata( - std::multimap* metadata) override { - send_trailing_metadata_ = metadata; - } - - virtual void SetRecvMessage(void* message) override { - recv_message_ = message; - } - - virtual void SetRecvInitialMetadata(internal::MetadataMap* map) override { - recv_initial_metadata_ = map; - } - - virtual void SetRecvStatus(Status* status) override { recv_status_ = status; } - - virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) override { - recv_trailing_metadata_ = map; - } - - virtual std::unique_ptr GetInterceptedChannel() override { - auto* info = call_->client_rpc_info(); - if (info == nullptr) { - return std::unique_ptr(nullptr); - } - // The intercepted channel starts from the interceptor just after the - // current interceptor - return std::unique_ptr(new internal::InterceptedChannel( - reinterpret_cast(info->channel()), - curr_iteration_ + 1)); - } - - // Clears all state - void ClearState() { - reverse_ = false; - ran_hijacking_interceptor_ = false; - ClearHookPoints(); - } - - // Prepares for Post_recv operations - void SetReverse() { - reverse_ = true; - ran_hijacking_interceptor_ = false; - ClearHookPoints(); - } - - // This needs to be set before interceptors are run - void SetCall(Call* call) { call_ = call; } - - // This needs to be set before interceptors are run using RunInterceptors(). - // Alternatively, RunInterceptors(std::function f) can be used. - void SetCallOpSetInterface(CallOpSetInterface* ops) { ops_ = ops; } - - // Returns true if no interceptors are run. This should be used only by - // subclasses of CallOpSetInterface. SetCall and SetCallOpSetInterface should - // have been called before this. After all the interceptors are done running, - // either ContinueFillOpsAfterInterception or - // ContinueFinalizeOpsAfterInterception will be called. Note that neither of - // them is invoked if there were no interceptors registered. - bool RunInterceptors() { - GPR_CODEGEN_ASSERT(ops_); - auto* client_rpc_info = call_->client_rpc_info(); - if (client_rpc_info != nullptr) { - if (client_rpc_info->interceptors_.size() == 0) { - return true; - } else { - RunClientInterceptors(); - return false; - } - } - - auto* server_rpc_info = call_->server_rpc_info(); - if (server_rpc_info == nullptr || - server_rpc_info->interceptors_.size() == 0) { - return true; - } - RunServerInterceptors(); - return false; - } - - // Returns true if no interceptors are run. Returns false otherwise if there - // are interceptors registered. After the interceptors are done running \a f - // will be invoked. This is to be used only by BaseAsyncRequest and - // SyncRequest. - bool RunInterceptors(std::function f) { - // This is used only by the server for initial call request - GPR_CODEGEN_ASSERT(reverse_ == true); - GPR_CODEGEN_ASSERT(call_->client_rpc_info() == nullptr); - auto* server_rpc_info = call_->server_rpc_info(); - if (server_rpc_info == nullptr || - server_rpc_info->interceptors_.size() == 0) { - return true; - } - callback_ = std::move(f); - RunServerInterceptors(); - return false; - } - - private: - void RunClientInterceptors() { - auto* rpc_info = call_->client_rpc_info(); - if (!reverse_) { - curr_iteration_ = 0; - } else { - if (rpc_info->hijacked_) { - curr_iteration_ = rpc_info->hijacked_interceptor_; - } else { - curr_iteration_ = rpc_info->interceptors_.size() - 1; - } - } - rpc_info->RunInterceptor(this, curr_iteration_); - } - - void RunServerInterceptors() { - auto* rpc_info = call_->server_rpc_info(); - if (!reverse_) { - curr_iteration_ = 0; - } else { - curr_iteration_ = rpc_info->interceptors_.size() - 1; - } - rpc_info->RunInterceptor(this, curr_iteration_); - } - - void ProceedClient() { - auto* rpc_info = call_->client_rpc_info(); - if (rpc_info->hijacked_ && !reverse_ && - curr_iteration_ == rpc_info->hijacked_interceptor_ && - !ran_hijacking_interceptor_) { - // We now need to provide hijacked recv ops to this interceptor - ClearHookPoints(); - ops_->SetHijackingState(); - ran_hijacking_interceptor_ = true; - rpc_info->RunInterceptor(this, curr_iteration_); - return; - } - if (!reverse_) { - curr_iteration_++; - // We are going down the stack of interceptors - if (curr_iteration_ < static_cast(rpc_info->interceptors_.size())) { - if (rpc_info->hijacked_ && - curr_iteration_ > rpc_info->hijacked_interceptor_) { - // This is a hijacked RPC and we are done with hijacking - ops_->ContinueFillOpsAfterInterception(); - } else { - rpc_info->RunInterceptor(this, curr_iteration_); - } - } else { - // we are done running all the interceptors without any hijacking - ops_->ContinueFillOpsAfterInterception(); - } - } else { - curr_iteration_--; - // We are going up the stack of interceptors - if (curr_iteration_ >= 0) { - // Continue running interceptors - rpc_info->RunInterceptor(this, curr_iteration_); - } else { - // we are done running all the interceptors without any hijacking - ops_->ContinueFinalizeResultAfterInterception(); - } - } - } - - void ProceedServer() { - auto* rpc_info = call_->server_rpc_info(); - if (!reverse_) { - curr_iteration_++; - if (curr_iteration_ < static_cast(rpc_info->interceptors_.size())) { - return rpc_info->RunInterceptor(this, curr_iteration_); - } else if (ops_) { - return ops_->ContinueFillOpsAfterInterception(); - } - } else { - curr_iteration_--; - // We are going up the stack of interceptors - if (curr_iteration_ >= 0) { - // Continue running interceptors - return rpc_info->RunInterceptor(this, curr_iteration_); - } else if (ops_) { - return ops_->ContinueFinalizeResultAfterInterception(); - } - } - GPR_CODEGEN_ASSERT(callback_); - callback_(); - } - - void ClearHookPoints() { - for (auto i = 0; - i < static_cast( - experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS); - i++) { - hooks_[i] = false; - } - } - - std::array( - experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS)> - hooks_; - - int curr_iteration_ = 0; // Current iterator - bool reverse_ = false; - bool ran_hijacking_interceptor_ = false; - Call* call_ = - nullptr; // The Call object is present along with CallOpSet object - CallOpSetInterface* ops_ = nullptr; - std::function callback_; - - ByteBuffer* send_message_ = nullptr; - - std::multimap* send_initial_metadata_; - - grpc_status_code* code_ = nullptr; - grpc::string* error_details_ = nullptr; - grpc::string* error_message_ = nullptr; - Status send_status_; - - std::multimap* send_trailing_metadata_ = nullptr; - - void* recv_message_ = nullptr; - - internal::MetadataMap* recv_initial_metadata_ = nullptr; - - Status* recv_status_ = nullptr; - - internal::MetadataMap* recv_trailing_metadata_ = nullptr; -}; - /// Primary implementation of CallOpSetInterface. /// Since we cannot use variadic templates, we declare slots up to /// the maximum count of ops we'll need in a set. We leverage the diff --git a/include/grpcpp/impl/codegen/call_op_set_interface.h b/include/grpcpp/impl/codegen/call_op_set_interface.h new file mode 100644 index 00000000000..815227a2996 --- /dev/null +++ b/include/grpcpp/impl/codegen/call_op_set_interface.h @@ -0,0 +1,59 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPCPP_IMPL_CODEGEN_CALL_OP_SET_INTERFACE_H +#define GRPCPP_IMPL_CODEGEN_CALL_OP_SET_INTERFACE_H + +#include + +namespace grpc { +namespace internal { + +class Call; + +/// An abstract collection of call ops, used to generate the +/// grpc_call_op structure to pass down to the lower layers, +/// and as it is-a CompletionQueueTag, also massages the final +/// completion into the correct form for consumption in the C++ +/// API. +class CallOpSetInterface : public CompletionQueueTag { + public: + /// Fills in grpc_op, starting from ops[*nops] and moving + /// upwards. + virtual void FillOps(internal::Call* call) = 0; + + /// Get the tag to be used at the core completion queue. Generally, the + /// value of cq_tag will be "this". However, it can be overridden if we + /// want core to process the tag differently (e.g., as a core callback) + virtual void* cq_tag() = 0; + + // This will be called while interceptors are run if the RPC is a hijacked + // RPC. This should set hijacking state for each of the ops. + virtual void SetHijackingState() = 0; + + // Should be called after interceptors are done running + virtual void ContinueFillOpsAfterInterception() = 0; + + // Should be called after interceptors are done running on the finalize result + // path + virtual void ContinueFinalizeResultAfterInterception() = 0; +}; +} // namespace internal +} // namespace grpc + +#endif // GRPCPP_IMPL_CODEGEN_CALL_OP_SET_INTERFACE_H diff --git a/include/grpcpp/impl/codegen/interceptor_common.h b/include/grpcpp/impl/codegen/interceptor_common.h new file mode 100644 index 00000000000..21b27f36724 --- /dev/null +++ b/include/grpcpp/impl/codegen/interceptor_common.h @@ -0,0 +1,390 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H +#define GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H + +#include +#include + +#include + +namespace grpc { +namespace internal { + +/// Internal methods for setting the state +class InternalInterceptorBatchMethods + : public experimental::InterceptorBatchMethods { + public: + virtual ~InternalInterceptorBatchMethods() {} + + virtual void AddInterceptionHookPoint( + experimental::InterceptionHookPoints type) = 0; + + virtual void SetSendMessage(ByteBuffer* buf) = 0; + + virtual void SetSendInitialMetadata( + std::multimap* metadata) = 0; + + virtual void SetSendStatus(grpc_status_code* code, + grpc::string* error_details, + grpc::string* error_message) = 0; + + virtual void SetSendTrailingMetadata( + std::multimap* metadata) = 0; + + virtual void SetRecvMessage(void* message) = 0; + + virtual void SetRecvInitialMetadata(internal::MetadataMap* map) = 0; + + virtual void SetRecvStatus(Status* status) = 0; + + virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) = 0; + + virtual std::unique_ptr GetInterceptedChannel() = 0; +}; + +class InterceptorBatchMethodsImpl : public InternalInterceptorBatchMethods { + public: + InterceptorBatchMethodsImpl() { + for (auto i = 0; + i < static_cast( + experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS); + i++) { + hooks_[i] = false; + } + } + + virtual ~InterceptorBatchMethodsImpl() {} + + virtual bool QueryInterceptionHookPoint( + experimental::InterceptionHookPoints type) override { + return hooks_[static_cast(type)]; + } + + virtual void Proceed() override { /* fill this */ + if (call_->client_rpc_info() != nullptr) { + return ProceedClient(); + } + GPR_CODEGEN_ASSERT(call_->server_rpc_info() != nullptr); + ProceedServer(); + } + + virtual void Hijack() override { + // Only the client can hijack when sending down initial metadata + GPR_CODEGEN_ASSERT(!reverse_ && ops_ != nullptr && + call_->client_rpc_info() != nullptr); + // It is illegal to call Hijack twice + GPR_CODEGEN_ASSERT(!ran_hijacking_interceptor_); + auto* rpc_info = call_->client_rpc_info(); + rpc_info->hijacked_ = true; + rpc_info->hijacked_interceptor_ = curr_iteration_; + ClearHookPoints(); + ops_->SetHijackingState(); + ran_hijacking_interceptor_ = true; + rpc_info->RunInterceptor(this, curr_iteration_); + } + + virtual void AddInterceptionHookPoint( + experimental::InterceptionHookPoints type) override { + hooks_[static_cast(type)] = true; + } + + virtual ByteBuffer* GetSendMessage() override { return send_message_; } + + virtual std::multimap* GetSendInitialMetadata() + override { + return send_initial_metadata_; + } + + virtual Status GetSendStatus() override { + return Status(static_cast(*code_), *error_message_, + *error_details_); + } + + virtual void ModifySendStatus(const Status& status) override { + *code_ = static_cast(status.error_code()); + *error_details_ = status.error_details(); + *error_message_ = status.error_message(); + } + + virtual std::multimap* GetSendTrailingMetadata() + override { + return send_trailing_metadata_; + } + + virtual void* GetRecvMessage() override { return recv_message_; } + + virtual std::multimap* + GetRecvInitialMetadata() override { + return recv_initial_metadata_->map(); + } + + virtual Status* GetRecvStatus() override { return recv_status_; } + + virtual std::multimap* + GetRecvTrailingMetadata() override { + return recv_trailing_metadata_->map(); + } + + virtual void SetSendMessage(ByteBuffer* buf) override { send_message_ = buf; } + + virtual void SetSendInitialMetadata( + std::multimap* metadata) override { + send_initial_metadata_ = metadata; + } + + virtual void SetSendStatus(grpc_status_code* code, + grpc::string* error_details, + grpc::string* error_message) override { + code_ = code; + error_details_ = error_details; + error_message_ = error_message; + } + + virtual void SetSendTrailingMetadata( + std::multimap* metadata) override { + send_trailing_metadata_ = metadata; + } + + virtual void SetRecvMessage(void* message) override { + recv_message_ = message; + } + + virtual void SetRecvInitialMetadata(internal::MetadataMap* map) override { + recv_initial_metadata_ = map; + } + + virtual void SetRecvStatus(Status* status) override { recv_status_ = status; } + + virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) override { + recv_trailing_metadata_ = map; + } + + virtual std::unique_ptr GetInterceptedChannel() override { + auto* info = call_->client_rpc_info(); + if (info == nullptr) { + return std::unique_ptr(nullptr); + } + // The intercepted channel starts from the interceptor just after the + // current interceptor + return std::unique_ptr(new internal::InterceptedChannel( + reinterpret_cast(info->channel()), + curr_iteration_ + 1)); + } + + // Clears all state + void ClearState() { + reverse_ = false; + ran_hijacking_interceptor_ = false; + ClearHookPoints(); + } + + // Prepares for Post_recv operations + void SetReverse() { + reverse_ = true; + ran_hijacking_interceptor_ = false; + ClearHookPoints(); + } + + // This needs to be set before interceptors are run + void SetCall(Call* call) { call_ = call; } + + // This needs to be set before interceptors are run using RunInterceptors(). + // Alternatively, RunInterceptors(std::function f) can be used. + void SetCallOpSetInterface(CallOpSetInterface* ops) { ops_ = ops; } + + // Returns true if no interceptors are run. This should be used only by + // subclasses of CallOpSetInterface. SetCall and SetCallOpSetInterface should + // have been called before this. After all the interceptors are done running, + // either ContinueFillOpsAfterInterception or + // ContinueFinalizeOpsAfterInterception will be called. Note that neither of + // them is invoked if there were no interceptors registered. + bool RunInterceptors() { + GPR_CODEGEN_ASSERT(ops_); + auto* client_rpc_info = call_->client_rpc_info(); + if (client_rpc_info != nullptr) { + if (client_rpc_info->interceptors_.size() == 0) { + return true; + } else { + RunClientInterceptors(); + return false; + } + } + + auto* server_rpc_info = call_->server_rpc_info(); + if (server_rpc_info == nullptr || + server_rpc_info->interceptors_.size() == 0) { + return true; + } + RunServerInterceptors(); + return false; + } + + // Returns true if no interceptors are run. Returns false otherwise if there + // are interceptors registered. After the interceptors are done running \a f + // will be invoked. This is to be used only by BaseAsyncRequest and + // SyncRequest. + bool RunInterceptors(std::function f) { + // This is used only by the server for initial call request + GPR_CODEGEN_ASSERT(reverse_ == true); + GPR_CODEGEN_ASSERT(call_->client_rpc_info() == nullptr); + auto* server_rpc_info = call_->server_rpc_info(); + if (server_rpc_info == nullptr || + server_rpc_info->interceptors_.size() == 0) { + return true; + } + callback_ = std::move(f); + RunServerInterceptors(); + return false; + } + + private: + void RunClientInterceptors() { + auto* rpc_info = call_->client_rpc_info(); + if (!reverse_) { + curr_iteration_ = 0; + } else { + if (rpc_info->hijacked_) { + curr_iteration_ = rpc_info->hijacked_interceptor_; + } else { + curr_iteration_ = rpc_info->interceptors_.size() - 1; + } + } + rpc_info->RunInterceptor(this, curr_iteration_); + } + + void RunServerInterceptors() { + auto* rpc_info = call_->server_rpc_info(); + if (!reverse_) { + curr_iteration_ = 0; + } else { + curr_iteration_ = rpc_info->interceptors_.size() - 1; + } + rpc_info->RunInterceptor(this, curr_iteration_); + } + + void ProceedClient() { + auto* rpc_info = call_->client_rpc_info(); + if (rpc_info->hijacked_ && !reverse_ && + curr_iteration_ == rpc_info->hijacked_interceptor_ && + !ran_hijacking_interceptor_) { + // We now need to provide hijacked recv ops to this interceptor + ClearHookPoints(); + ops_->SetHijackingState(); + ran_hijacking_interceptor_ = true; + rpc_info->RunInterceptor(this, curr_iteration_); + return; + } + if (!reverse_) { + curr_iteration_++; + // We are going down the stack of interceptors + if (curr_iteration_ < static_cast(rpc_info->interceptors_.size())) { + if (rpc_info->hijacked_ && + curr_iteration_ > rpc_info->hijacked_interceptor_) { + // This is a hijacked RPC and we are done with hijacking + ops_->ContinueFillOpsAfterInterception(); + } else { + rpc_info->RunInterceptor(this, curr_iteration_); + } + } else { + // we are done running all the interceptors without any hijacking + ops_->ContinueFillOpsAfterInterception(); + } + } else { + curr_iteration_--; + // We are going up the stack of interceptors + if (curr_iteration_ >= 0) { + // Continue running interceptors + rpc_info->RunInterceptor(this, curr_iteration_); + } else { + // we are done running all the interceptors without any hijacking + ops_->ContinueFinalizeResultAfterInterception(); + } + } + } + + void ProceedServer() { + auto* rpc_info = call_->server_rpc_info(); + if (!reverse_) { + curr_iteration_++; + if (curr_iteration_ < static_cast(rpc_info->interceptors_.size())) { + return rpc_info->RunInterceptor(this, curr_iteration_); + } else if (ops_) { + return ops_->ContinueFillOpsAfterInterception(); + } + } else { + curr_iteration_--; + // We are going up the stack of interceptors + if (curr_iteration_ >= 0) { + // Continue running interceptors + return rpc_info->RunInterceptor(this, curr_iteration_); + } else if (ops_) { + return ops_->ContinueFinalizeResultAfterInterception(); + } + } + GPR_CODEGEN_ASSERT(callback_); + callback_(); + } + + void ClearHookPoints() { + for (auto i = 0; + i < static_cast( + experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS); + i++) { + hooks_[i] = false; + } + } + + std::array( + experimental::InterceptionHookPoints::NUM_INTERCEPTION_HOOKS)> + hooks_; + + int curr_iteration_ = 0; // Current iterator + bool reverse_ = false; + bool ran_hijacking_interceptor_ = false; + Call* call_ = + nullptr; // The Call object is present along with CallOpSet object + CallOpSetInterface* ops_ = nullptr; + std::function callback_; + + ByteBuffer* send_message_ = nullptr; + + std::multimap* send_initial_metadata_; + + grpc_status_code* code_ = nullptr; + grpc::string* error_details_ = nullptr; + grpc::string* error_message_ = nullptr; + Status send_status_; + + std::multimap* send_trailing_metadata_ = nullptr; + + void* recv_message_ = nullptr; + + internal::MetadataMap* recv_initial_metadata_ = nullptr; + + Status* recv_status_ = nullptr; + + internal::MetadataMap* recv_trailing_metadata_ = nullptr; +}; + +} // namespace internal +} // namespace grpc + +#endif // GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index a59021756a5..f2bb5df7d33 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -944,6 +944,7 @@ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ +include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -960,6 +961,7 @@ include/grpcpp/impl/codegen/create_auth_context.h \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ +include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index b162f557557..81d74cd9724 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -945,6 +945,7 @@ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ include/grpcpp/impl/codegen/call_op_set.h \ +include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ @@ -962,6 +963,7 @@ include/grpcpp/impl/codegen/create_auth_context.h \ include/grpcpp/impl/codegen/grpc_library.h \ include/grpcpp/impl/codegen/intercepted_channel.h \ include/grpcpp/impl/codegen/interceptor.h \ +include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 85c76bad3a4..9d7d487e0df 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -11177,6 +11177,7 @@ "include/grpcpp/impl/codegen/call.h", "include/grpcpp/impl/codegen/call_hook.h", "include/grpcpp/impl/codegen/call_op_set.h", + "include/grpcpp/impl/codegen/call_op_set_interface.h", "include/grpcpp/impl/codegen/callback_common.h", "include/grpcpp/impl/codegen/channel_interface.h", "include/grpcpp/impl/codegen/client_callback.h", @@ -11191,6 +11192,7 @@ "include/grpcpp/impl/codegen/grpc_library.h", "include/grpcpp/impl/codegen/intercepted_channel.h", "include/grpcpp/impl/codegen/interceptor.h", + "include/grpcpp/impl/codegen/interceptor_common.h", "include/grpcpp/impl/codegen/metadata_map.h", "include/grpcpp/impl/codegen/method_handler_impl.h", "include/grpcpp/impl/codegen/rpc_method.h", @@ -11250,6 +11252,7 @@ "include/grpcpp/impl/codegen/call.h", "include/grpcpp/impl/codegen/call_hook.h", "include/grpcpp/impl/codegen/call_op_set.h", + "include/grpcpp/impl/codegen/call_op_set_interface.h", "include/grpcpp/impl/codegen/callback_common.h", "include/grpcpp/impl/codegen/channel_interface.h", "include/grpcpp/impl/codegen/client_callback.h", @@ -11264,6 +11267,7 @@ "include/grpcpp/impl/codegen/grpc_library.h", "include/grpcpp/impl/codegen/intercepted_channel.h", "include/grpcpp/impl/codegen/interceptor.h", + "include/grpcpp/impl/codegen/interceptor_common.h", "include/grpcpp/impl/codegen/metadata_map.h", "include/grpcpp/impl/codegen/method_handler_impl.h", "include/grpcpp/impl/codegen/rpc_method.h",