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