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