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 19 #ifndef GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H 20 #define GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H 21 22 #include <atomic> 23 #include <vector> 24 25 #include <grpc/support/log.h> 26 #include <grpcpp/impl/rpc_method.h> 27 #include <grpcpp/support/interceptor.h> 28 #include <grpcpp/support/string_ref.h> 29 30 namespace grpc { 31 class ServerContextBase; 32 namespace internal { 33 class InterceptorBatchMethodsImpl; 34 } 35 36 namespace experimental { 37 class ServerRpcInfo; 38 39 // A factory interface for creation of server interceptors. A vector of 40 // factories can be provided to ServerBuilder which will be used to create a new 41 // vector of server interceptors per RPC. Server interceptor authors should 42 // create a subclass of ServerInterceptorFactorInterface which creates objects 43 // of their interceptors. 44 class ServerInterceptorFactoryInterface { 45 public: ~ServerInterceptorFactoryInterface()46 virtual ~ServerInterceptorFactoryInterface() {} 47 // Returns a pointer to an Interceptor object on successful creation, nullptr 48 // otherwise. If nullptr is returned, this server interceptor factory is 49 // ignored for the purposes of that RPC. 50 virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0; 51 }; 52 53 /// ServerRpcInfo represents the state of a particular RPC as it 54 /// appears to an interceptor. It is created and owned by the library and 55 /// passed to the CreateServerInterceptor method of the application's 56 /// ServerInterceptorFactoryInterface implementation 57 class ServerRpcInfo { 58 public: 59 /// Type categorizes RPCs by unary or streaming type 60 enum class Type { UNARY, CLIENT_STREAMING, SERVER_STREAMING, BIDI_STREAMING }; 61 ~ServerRpcInfo()62 ~ServerRpcInfo() {} 63 64 // Delete all copy and move constructors and assignments 65 ServerRpcInfo(const ServerRpcInfo&) = delete; 66 ServerRpcInfo& operator=(const ServerRpcInfo&) = delete; 67 ServerRpcInfo(ServerRpcInfo&&) = delete; 68 ServerRpcInfo& operator=(ServerRpcInfo&&) = delete; 69 70 // Getter methods 71 72 /// Return the fully-specified method name method()73 const char* method() const { return method_; } 74 75 /// Return the type of the RPC (unary or a streaming flavor) type()76 Type type() const { return type_; } 77 78 /// Return a pointer to the underlying ServerContext structure associated 79 /// with the RPC to support features that apply to it server_context()80 ServerContextBase* server_context() { return ctx_; } 81 82 private: 83 static_assert(Type::UNARY == 84 static_cast<Type>(internal::RpcMethod::NORMAL_RPC), 85 "violated expectation about Type enum"); 86 static_assert(Type::CLIENT_STREAMING == 87 static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING), 88 "violated expectation about Type enum"); 89 static_assert(Type::SERVER_STREAMING == 90 static_cast<Type>(internal::RpcMethod::SERVER_STREAMING), 91 "violated expectation about Type enum"); 92 static_assert(Type::BIDI_STREAMING == 93 static_cast<Type>(internal::RpcMethod::BIDI_STREAMING), 94 "violated expectation about Type enum"); 95 ServerRpcInfo(ServerContextBase * ctx,const char * method,internal::RpcMethod::RpcType type)96 ServerRpcInfo(ServerContextBase* ctx, const char* method, 97 internal::RpcMethod::RpcType type) 98 : ctx_(ctx), method_(method), type_(static_cast<Type>(type)) {} 99 100 // Runs interceptor at pos \a pos. RunInterceptor(experimental::InterceptorBatchMethods * interceptor_methods,size_t pos)101 void RunInterceptor( 102 experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) { 103 GPR_ASSERT(pos < interceptors_.size()); 104 interceptors_[pos]->Intercept(interceptor_methods); 105 } 106 RegisterInterceptors(const std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>> & creators)107 void RegisterInterceptors( 108 const std::vector< 109 std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>& 110 creators) { 111 for (const auto& creator : creators) { 112 auto* interceptor = creator->CreateServerInterceptor(this); 113 if (interceptor != nullptr) { 114 interceptors_.push_back( 115 std::unique_ptr<experimental::Interceptor>(interceptor)); 116 } 117 } 118 } 119 Ref()120 void Ref() { ref_.fetch_add(1, std::memory_order_relaxed); } Unref()121 void Unref() { 122 if (GPR_UNLIKELY(ref_.fetch_sub(1, std::memory_order_acq_rel) == 1)) { 123 delete this; 124 } 125 } 126 127 ServerContextBase* ctx_ = nullptr; 128 const char* method_ = nullptr; 129 const Type type_; 130 std::atomic<intptr_t> ref_{1}; 131 std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_; 132 133 friend class internal::InterceptorBatchMethodsImpl; 134 friend class grpc::ServerContextBase; 135 }; 136 137 } // namespace experimental 138 } // namespace grpc 139 140 #endif // GRPCPP_SUPPORT_SERVER_INTERCEPTOR_H 141