impl/codegen/server_interceptor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
21 
22 // IWYU pragma: private, include <grpcpp/support/server_interceptor.h>
23 
24 #include <atomic>
25 #include <vector>
26 
30 
31 namespace grpc {
32 class ServerContextBase;
33 namespace internal {
34 class InterceptorBatchMethodsImpl;
35 }
36 
37 namespace experimental {
38 class ServerRpcInfo;
39 
40 // A factory interface for creation of server interceptors. A vector of
41 // factories can be provided to ServerBuilder which will be used to create a new
42 // vector of server interceptors per RPC. Server interceptor authors should
43 // create a subclass of ServerInterceptorFactorInterface which creates objects
44 // of their interceptors.
46  public:
48  // Returns a pointer to an Interceptor object on successful creation, nullptr
49  // otherwise. If nullptr is returned, this server interceptor factory is
50  // ignored for the purposes of that RPC.
52 };
53 
59  public:
62 
64 
65  // Delete all copy and move constructors and assignments
66  ServerRpcInfo(const ServerRpcInfo&) = delete;
67  ServerRpcInfo& operator=(const ServerRpcInfo&) = delete;
68  ServerRpcInfo(ServerRpcInfo&&) = delete;
70 
71  // Getter methods
72 
74  const char* method() const { return method_; }
75 
77  Type type() const { return type_; }
78 
82 
83  private:
84  static_assert(Type::UNARY ==
86  "violated expectation about Type enum");
87  static_assert(Type::CLIENT_STREAMING ==
89  "violated expectation about Type enum");
90  static_assert(Type::SERVER_STREAMING ==
92  "violated expectation about Type enum");
93  static_assert(Type::BIDI_STREAMING ==
95  "violated expectation about Type enum");
96 
99  : ctx_(ctx), method_(method), type_(static_cast<Type>(type)) {}
100 
101  // Runs interceptor at pos \a pos.
103  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
105  interceptors_[pos]->Intercept(interceptor_methods);
106  }
107 
109  const std::vector<
110  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
111  creators) {
112  for (const auto& creator : creators) {
113  auto* interceptor = creator->CreateServerInterceptor(this);
114  if (interceptor != nullptr) {
115  interceptors_.push_back(
116  std::unique_ptr<experimental::Interceptor>(interceptor));
117  }
118  }
119  }
120 
121  void Ref() { ref_.fetch_add(1, std::memory_order_relaxed); }
122  void Unref() {
123  if (GPR_UNLIKELY(ref_.fetch_sub(1, std::memory_order_acq_rel) == 1)) {
124  delete this;
125  }
126  }
127 
129  const char* method_ = nullptr;
130  const Type type_;
131  std::atomic<intptr_t> ref_{1};
132  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
133 
136 };
137 
138 } // namespace experimental
139 } // namespace grpc
140 
141 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
grpc::experimental::ServerInterceptorFactoryInterface
Definition: impl/codegen/server_interceptor.h:45
grpc::experimental::ServerRpcInfo::Unref
void Unref()
Definition: impl/codegen/server_interceptor.h:122
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
ctx
Definition: benchmark-async.c:30
grpc::experimental::ServerRpcInfo::method_
const char * method_
Definition: impl/codegen/server_interceptor.h:129
grpc
Definition: grpcpp/alarm.h:33
grpc::experimental::ServerRpcInfo::ServerRpcInfo
ServerRpcInfo(const ServerRpcInfo &)=delete
grpc::experimental::ServerRpcInfo::Type::UNARY
@ UNARY
grpc::experimental::ServerRpcInfo::operator=
ServerRpcInfo & operator=(const ServerRpcInfo &)=delete
grpc::experimental::ServerRpcInfo::type
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: impl/codegen/server_interceptor.h:77
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:37
grpc::experimental::ServerRpcInfo::server_context
ServerContextBase * server_context()
Definition: impl/codegen/server_interceptor.h:81
grpc::experimental::ServerInterceptorFactoryInterface::~ServerInterceptorFactoryInterface
virtual ~ServerInterceptorFactoryInterface()
Definition: impl/codegen/server_interceptor.h:47
grpc::experimental::ServerRpcInfo::ctx_
ServerContextBase * ctx_
Definition: impl/codegen/server_interceptor.h:128
grpc::experimental::ServerRpcInfo::RegisterInterceptors
void RegisterInterceptors(const std::vector< std::unique_ptr< experimental::ServerInterceptorFactoryInterface >> &creators)
Definition: impl/codegen/server_interceptor.h:108
grpc::experimental::ServerRpcInfo::method
const char * method() const
Return the fully-specified method name.
Definition: impl/codegen/server_interceptor.h:74
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: grpcpp/impl/codegen/rpc_method.h:34
grpc::experimental::ServerRpcInfo::Ref
void Ref()
Definition: impl/codegen/server_interceptor.h:121
grpc::ServerContextBase
Base class of ServerContext.
Definition: grpcpp/impl/codegen/server_context.h:126
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
GPR_UNLIKELY
#define GPR_UNLIKELY(x)
Definition: impl/codegen/port_platform.h:770
grpc::experimental::ServerRpcInfo::~ServerRpcInfo
~ServerRpcInfo()
Definition: impl/codegen/server_interceptor.h:63
grpc::experimental::ServerRpcInfo::RunInterceptor
void RunInterceptor(experimental::InterceptorBatchMethods *interceptor_methods, size_t pos)
Definition: impl/codegen/server_interceptor.h:102
rpc_method.h
grpc::internal::RpcMethod::RpcType
RpcType
Definition: grpcpp/impl/codegen/rpc_method.h:33
grpc::experimental::InterceptorBatchMethods
Definition: impl/codegen/interceptor.h:98
grpc::experimental::ServerInterceptorFactoryInterface::CreateServerInterceptor
virtual Interceptor * CreateServerInterceptor(ServerRpcInfo *info)=0
string_ref.h
grpc::experimental::ServerRpcInfo::ServerRpcInfo
ServerRpcInfo(ServerContextBase *ctx, const char *method, internal::RpcMethod::RpcType type)
Definition: impl/codegen/server_interceptor.h:97
grpc::experimental::ServerRpcInfo::Type::BIDI_STREAMING
@ BIDI_STREAMING
grpc::experimental::ServerRpcInfo::ref_
std::atomic< intptr_t > ref_
Definition: impl/codegen/server_interceptor.h:131
grpc::experimental::Interceptor
Definition: impl/codegen/interceptor.h:221
grpc::experimental::ServerRpcInfo
Definition: impl/codegen/server_interceptor.h:58
grpc::experimental::ServerRpcInfo::Type::CLIENT_STREAMING
@ CLIENT_STREAMING
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:37
grpc::internal::RpcMethod::SERVER_STREAMING
@ SERVER_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:36
grpc::internal::RpcMethod::CLIENT_STREAMING
@ CLIENT_STREAMING
Definition: grpcpp/impl/codegen/rpc_method.h:35
grpc::experimental::ServerRpcInfo::interceptors_
std::vector< std::unique_ptr< experimental::Interceptor > > interceptors_
Definition: impl/codegen/server_interceptor.h:132
grpc::experimental::ServerRpcInfo::type_
const Type type_
Definition: impl/codegen/server_interceptor.h:130
grpc::experimental::ServerRpcInfo::Type::SERVER_STREAMING
@ SERVER_STREAMING
interceptor.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:17