diff options
author | 2018-10-16 14:07:11 -0700 | |
---|---|---|
committer | 2018-10-16 14:11:07 -0700 | |
commit | b232bacf2792430801e50c716e4f92b6f6f1551a (patch) | |
tree | e04946e65ffb21b2c960cddabfb97e3b5c2a33bc /include/grpcpp | |
parent | f9fe345553df800ce0787346dfffeae4e8164399 (diff) |
Starting on server interceptors
Diffstat (limited to 'include/grpcpp')
-rw-r--r-- | include/grpcpp/impl/codegen/call.h | 26 | ||||
-rw-r--r-- | include/grpcpp/impl/codegen/client_interceptor.h | 47 | ||||
-rw-r--r-- | include/grpcpp/impl/codegen/interceptor.h | 8 | ||||
-rw-r--r-- | include/grpcpp/impl/codegen/server_interceptor.h | 89 |
4 files changed, 117 insertions, 53 deletions
diff --git a/include/grpcpp/impl/codegen/call.h b/include/grpcpp/impl/codegen/call.h index ebb5d41001..2f5deaae86 100644 --- a/include/grpcpp/impl/codegen/call.h +++ b/include/grpcpp/impl/codegen/call.h @@ -35,6 +35,7 @@ #include <grpcpp/impl/codegen/config.h> #include <grpcpp/impl/codegen/core_codegen_interface.h> #include <grpcpp/impl/codegen/serialization_traits.h> +#include <grpcpp/impl/codegen/server_interceptor.h> #include <grpcpp/impl/codegen/slice.h> #include <grpcpp/impl/codegen/status.h> #include <grpcpp/impl/codegen/string_ref.h> @@ -764,14 +765,14 @@ class Call final { cq_(nullptr), call_(nullptr), max_receive_message_size_(-1), - rpc_info_(nullptr) {} + client_rpc_info_(nullptr) {} /** call is owned by the caller */ Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq) : call_hook_(call_hook), cq_(cq), call_(call), max_receive_message_size_(-1), - rpc_info_(nullptr) {} + client_rpc_info_(nullptr) {} Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq, experimental::ClientRpcInfo* rpc_info) @@ -779,7 +780,7 @@ class Call final { cq_(cq), call_(call), max_receive_message_size_(-1), - rpc_info_(rpc_info) {} + client_rpc_info_(rpc_info) {} Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq, int max_receive_message_size) @@ -787,7 +788,7 @@ class Call final { cq_(cq), call_(call), max_receive_message_size_(max_receive_message_size), - rpc_info_(nullptr) {} + client_rpc_info_(nullptr) {} void PerformOps(CallOpSetInterface* ops) { call_hook_->PerformOpsOnCall(ops, this); @@ -797,14 +798,17 @@ class Call final { CompletionQueue* cq() const { return cq_; } int max_receive_message_size() const { return max_receive_message_size_; } - experimental::ClientRpcInfo* rpc_info() const { return rpc_info_; } + experimental::ClientRpcInfo* client_rpc_info() const { + return client_rpc_info_; + } private: CallHook* call_hook_; CompletionQueue* cq_; grpc_call* call_; int max_receive_message_size_; - experimental::ClientRpcInfo* rpc_info_; + experimental::ClientRpcInfo* client_rpc_info_; + experimental::ServerRpcInfo* server_rpc_info_; }; /// An abstract collection of call ops, used to generate the @@ -861,7 +865,7 @@ class InterceptorBatchMethodsImpl virtual void Proceed() override { /* fill this */ curr_iteration_ = reverse_ ? curr_iteration_ - 1 : curr_iteration_ + 1; - auto* rpc_info = call_->rpc_info(); + auto* rpc_info = call_->client_rpc_info(); if (rpc_info->hijacked_ && (!reverse_ && curr_iteration_ == rpc_info->hijacked_interceptor_ + 1)) { /* We now need to provide hijacked recv ops to this interceptor */ @@ -898,7 +902,7 @@ class InterceptorBatchMethodsImpl virtual void Hijack() override { /* fill this */ GPR_CODEGEN_ASSERT(!reverse_); - auto* rpc_info = call_->rpc_info(); + auto* rpc_info = call_->client_rpc_info(); rpc_info->hijacked_ = true; rpc_info->hijacked_interceptor_ = curr_iteration_; ClearHookPoints(); @@ -993,11 +997,11 @@ class InterceptorBatchMethodsImpl /* This needs to be set before interceptors are run */ void SetCall(Call* call) { call_ = call; } - void SetCallOpSet(CallOpSetInterface* ops) { ops_ = ops; } + void SetCallOpSetInterface(CallOpSetInterface* ops) { ops_ = ops; } /* Returns true if no interceptors are run */ bool RunInterceptors() { - auto* rpc_info = call_->rpc_info(); + auto* rpc_info = call_->client_rpc_info(); if (rpc_info == nullptr || rpc_info->interceptors_.size() == 0) { return true; } @@ -1174,7 +1178,7 @@ class CallOpSet : public CallOpSetInterface, this->Op4::SetInterceptionHookPoint(&interceptor_methods_); this->Op5::SetInterceptionHookPoint(&interceptor_methods_); this->Op6::SetInterceptionHookPoint(&interceptor_methods_); - interceptor_methods_.SetCallOpSet(this); + interceptor_methods_.SetCallOpSetInterface(this); interceptor_methods_.SetCall(&call_); // interceptor_methods_.SetFunctions(ContinueFillOpsAfterInterception, // SetHijackingState, ContinueFinalizeResultAfterInterception); diff --git a/include/grpcpp/impl/codegen/client_interceptor.h b/include/grpcpp/impl/codegen/client_interceptor.h index c1feecd0ae..0a3a2ab07f 100644 --- a/include/grpcpp/impl/codegen/client_interceptor.h +++ b/include/grpcpp/impl/codegen/client_interceptor.h @@ -38,17 +38,10 @@ class CallNoOp; namespace experimental { class ClientRpcInfo; -class ClientInterceptor { - public: - virtual ~ClientInterceptor() {} - - virtual void Intercept(InterceptorBatchMethods* methods) = 0; -}; - class ClientInterceptorFactoryInterface { public: virtual ~ClientInterceptorFactoryInterface() {} - virtual ClientInterceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0; + virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0; }; class ClientRpcInfo { @@ -60,7 +53,7 @@ class ClientRpcInfo { experimental::ClientInterceptorFactoryInterface>>& creators) : ctx_(ctx), method_(method), channel_(channel) { for (const auto& creator : creators) { - interceptors_.push_back(std::unique_ptr<experimental::ClientInterceptor>( + interceptors_.push_back(std::unique_ptr<experimental::Interceptor>( creator->CreateClientInterceptor(this))); } } @@ -74,32 +67,7 @@ class ClientRpcInfo { const char* method() { return method_; } const Channel* channel() { return channel_; } grpc::ClientContext* client_context() { return ctx_; } - // const grpc::InterceptedMessage& outgoing_message(); - // grpc::InterceptedMessage *mutable_outgoing_message(); - // const grpc::InterceptedMessage& received_message(); - // grpc::InterceptedMessage *mutable_received_message(); - - // const std::multimap<grpc::string, grpc::string>* client_initial_metadata() - // { return &ctx_->send_initial_metadata_; } const - // std::multimap<grpc::string_ref, grpc::string_ref>* - // server_initial_metadata() { return &ctx_->GetServerInitialMetadata(); } - // const std::multimap<grpc::string_ref, grpc::string_ref>* - // server_trailing_metadata() { return &ctx_->GetServerTrailingMetadata(); } - // const Status *status(); - - // template <class M> - // void set_outgoing_message(M* msg); // edit outgoing message - // template <class M> - // void set_received_message(M* msg); // edit received message - // for hijacking (can be called multiple times for streaming) - // template <class M> - // void inject_received_message(M* msg); - // void set_client_initial_metadata( - // const std::multimap<grpc::string, grpc::string>& overwrite); - // void set_server_initial_metadata(const std::multimap<grpc::string, - // grpc::string>& overwrite); void set_server_trailing_metadata(const - // std::multimap<grpc::string, grpc::string>& overwrite); void - // set_status(Status status); + public: /* Runs interceptor at pos \a pos. If \a reverse is set, the interceptor order * is the reverse */ @@ -110,20 +78,15 @@ class ClientRpcInfo { interceptors_[pos]->Intercept(interceptor_methods); } + private: grpc::ClientContext* ctx_ = nullptr; const char* method_ = nullptr; const grpc::Channel* channel_ = nullptr; public: - std::vector<std::unique_ptr<experimental::ClientInterceptor>> interceptors_; + std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_; bool hijacked_ = false; int hijacked_interceptor_ = false; - // template <class Op1 = internal::CallNoOp<1>, class Op2 = - // internal::CallNoOp<2>, - // class Op3 = internal::CallNoOp<3>, class Op4 = - // internal::CallNoOp<4>, class Op5 = internal::CallNoOp<5>, class Op6 - // = internal::CallNoOp<6>> - // friend class internal::InterceptorBatchMethodsImpl; }; } // namespace experimental diff --git a/include/grpcpp/impl/codegen/interceptor.h b/include/grpcpp/impl/codegen/interceptor.h index 390e5ab7fb..3e16b99326 100644 --- a/include/grpcpp/impl/codegen/interceptor.h +++ b/include/grpcpp/impl/codegen/interceptor.h @@ -119,6 +119,14 @@ class InterceptorBatchMethods { virtual void SetRecvTrailingMetadata(internal::MetadataMap* map) = 0; }; + +class Interceptor { + public: + virtual ~Interceptor() {} + + virtual void Intercept(InterceptorBatchMethods* methods) = 0; +}; + } // namespace experimental } // namespace grpc diff --git a/include/grpcpp/impl/codegen/server_interceptor.h b/include/grpcpp/impl/codegen/server_interceptor.h new file mode 100644 index 0000000000..9eb1a61385 --- /dev/null +++ b/include/grpcpp/impl/codegen/server_interceptor.h @@ -0,0 +1,89 @@ +/* + * + * 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_SERVER_INTERCEPTOR_H +#define GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H + +#include <vector> + +#include <grpc/impl/codegen/log.h> +#include <grpcpp/impl/codegen/interceptor.h> +#include <grpcpp/impl/codegen/string_ref.h> + +namespace grpc { + +class ServerContext; + +namespace internal { +template <int I> +class CallNoOp; +} + +namespace experimental { +class ServerRpcInfo; + +class ServerInterceptorFactoryInterface { + public: + virtual ~ServerInterceptorFactoryInterface() {} + virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0; +}; + +class ServerRpcInfo { + public: + ServerRpcInfo() {} + ServerRpcInfo(grpc::ServerContext* ctx, const char* method, + const std::vector<std::unique_ptr< + experimental::ServerInterceptorFactoryInterface>>& creators) + : ctx_(ctx), method_(method) { + for (const auto& creator : creators) { + interceptors_.push_back(std::unique_ptr<experimental::Interceptor>( + creator->CreateServerInterceptor(this))); + } + } + ~ServerRpcInfo(){}; + + ServerRpcInfo(const ServerRpcInfo&) = delete; + ServerRpcInfo(ServerRpcInfo&&) = default; + ServerRpcInfo& operator=(ServerRpcInfo&&) = default; + + // Getter methods + const char* method() { return method_; } + grpc::ServerContext* server_context() { return ctx_; } + + public: + /* Runs interceptor at pos \a pos. If \a reverse is set, the interceptor order + * is the reverse */ + void RunInterceptor( + experimental::InterceptorBatchMethods* interceptor_methods, + unsigned int pos) { + GPR_CODEGEN_ASSERT(pos < interceptors_.size()); + interceptors_[pos]->Intercept(interceptor_methods); + } + + private: + grpc::ServerContext* ctx_ = nullptr; + const char* method_ = nullptr; + + public: + std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_; +}; + +} // namespace experimental +} // namespace grpc + +#endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H |