GRPC C++  1.22.0
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_impl {
30 class ServerContext;
31 }
32 
33 namespace grpc {
34 
35 namespace internal {
36 class InterceptorBatchMethodsImpl;
37 }
38 
39 namespace experimental {
40 class ServerRpcInfo;
41 
42 // A factory interface for creation of server interceptors. A vector of
43 // factories can be provided to ServerBuilder which will be used to create a new
44 // vector of server interceptors per RPC. Server interceptor authors should
45 // create a subclass of ServerInterceptorFactorInterface which creates objects
46 // of their interceptors.
48  public:
50  // Returns a pointer to an Interceptor object on successful creation, nullptr
51  // otherwise. If nullptr is returned, this server interceptor factory is
52  // ignored for the purposes of that RPC.
53  virtual Interceptor* CreateServerInterceptor(ServerRpcInfo* info) = 0;
54 };
55 
61  public:
63  enum class Type { UNARY, CLIENT_STREAMING, SERVER_STREAMING, BIDI_STREAMING };
64 
66 
67  // Delete all copy and move constructors and assignments
68  ServerRpcInfo(const ServerRpcInfo&) = delete;
69  ServerRpcInfo& operator=(const ServerRpcInfo&) = delete;
70  ServerRpcInfo(ServerRpcInfo&&) = delete;
71  ServerRpcInfo& operator=(ServerRpcInfo&&) = delete;
72 
73  // Getter methods
74 
76  const char* method() const { return method_; }
77 
79  Type type() const { return type_; }
80 
84 
85  private:
86  static_assert(Type::UNARY ==
87  static_cast<Type>(internal::RpcMethod::NORMAL_RPC),
88  "violated expectation about Type enum");
89  static_assert(Type::CLIENT_STREAMING ==
90  static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING),
91  "violated expectation about Type enum");
92  static_assert(Type::SERVER_STREAMING ==
93  static_cast<Type>(internal::RpcMethod::SERVER_STREAMING),
94  "violated expectation about Type enum");
95  static_assert(Type::BIDI_STREAMING ==
96  static_cast<Type>(internal::RpcMethod::BIDI_STREAMING),
97  "violated expectation about Type enum");
98 
99  ServerRpcInfo(grpc_impl::ServerContext* ctx, const char* method,
101  : ctx_(ctx), method_(method), type_(static_cast<Type>(type)) {
102  ref_.store(1);
103  }
104 
105  // Runs interceptor at pos \a pos.
106  void RunInterceptor(
107  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
108  GPR_CODEGEN_ASSERT(pos < interceptors_.size());
109  interceptors_[pos]->Intercept(interceptor_methods);
110  }
111 
112  void RegisterInterceptors(
113  const std::vector<
114  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
115  creators) {
116  for (const auto& creator : creators) {
117  auto* interceptor = creator->CreateServerInterceptor(this);
118  if (interceptor != nullptr) {
119  interceptors_.push_back(
120  std::unique_ptr<experimental::Interceptor>(interceptor));
121  }
122  }
123  }
124 
125  void Ref() { ref_++; }
126  void Unref() {
127  if (--ref_ == 0) {
128  delete this;
129  }
130  }
131 
132  grpc_impl::ServerContext* ctx_ = nullptr;
133  const char* method_ = nullptr;
134  const Type type_;
135  std::atomic_int ref_;
136  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
137 
140 };
141 
142 } // namespace experimental
143 } // namespace grpc
144 
145 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERCEPTOR_H
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
Class that is passed as an argument to the Intercept method of the application&#39;s Interceptor interfac...
Definition: interceptor.h:91
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: server_interceptor.h:79
RpcType
Definition: rpc_method.h:31
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:25
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:114
Type
Type categorizes RPCs by unary or streaming type.
Definition: server_interceptor.h:63
ServerRpcInfo represents the state of a particular RPC as it appears to an interceptor.
Definition: server_interceptor.h:60
const char * method() const
Return the fully-specified method name.
Definition: server_interceptor.h:76
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
virtual ~ServerInterceptorFactoryInterface()
Definition: server_interceptor.h:49
Interface for an interceptor.
Definition: interceptor.h:215
Definition: interceptor_common.h:36
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
~ServerRpcInfo()
Definition: server_interceptor.h:65
grpc_impl::ServerContext * server_context()
Return a pointer to the underlying ServerContext structure associated with the RPC to support feature...
Definition: server_interceptor.h:83