GRPC C++  1.36.1
client_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_CLIENT_INTERCEPTOR_H
20 #define GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
21 
22 #include <memory>
23 #include <vector>
24 
28 
29 namespace grpc {
30 
31 class Channel;
32 class ClientContext;
33 
34 namespace internal {
35 class InterceptorBatchMethodsImpl;
36 }
37 
38 namespace experimental {
39 class ClientRpcInfo;
40 
41 // A factory interface for creation of client interceptors. A vector of
42 // factories can be provided at channel creation which will be used to create a
43 // new vector of client interceptors per RPC. Client interceptor authors should
44 // create a subclass of ClientInterceptorFactorInterface 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 } // namespace experimental
55 
56 namespace internal {
57 extern experimental::ClientInterceptorFactoryInterface*
59 }
60 
65 namespace experimental {
67  public:
68  // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN
69  // from the list of possible Types.
71  enum class Type {
72  UNARY,
76  UNKNOWN // UNKNOWN is not API and will be removed later
77  };
78 
80 
81  // Delete copy constructor but allow default move constructor
82  ClientRpcInfo(const ClientRpcInfo&) = delete;
83  ClientRpcInfo(ClientRpcInfo&&) = default;
84 
85  // Getter methods
86 
88  const char* method() const { return method_; }
89 
91  ChannelInterface* channel() { return channel_; }
92 
95  grpc::ClientContext* client_context() { return ctx_; }
96 
98  Type type() const { return type_; }
99 
100  private:
101  static_assert(Type::UNARY ==
103  "violated expectation about Type enum");
104  static_assert(Type::CLIENT_STREAMING ==
106  "violated expectation about Type enum");
107  static_assert(Type::SERVER_STREAMING ==
109  "violated expectation about Type enum");
110  static_assert(Type::BIDI_STREAMING ==
112  "violated expectation about Type enum");
113 
114  // Default constructor should only be used by ClientContext
115  ClientRpcInfo() = default;
116 
117  // Constructor will only be called from ClientContext
119  const char* method, grpc::ChannelInterface* channel)
120  : ctx_(ctx),
121  type_(static_cast<Type>(type)),
122  method_(method),
123  channel_(channel) {}
124 
125  // Move assignment should only be used by ClientContext
126  // TODO(yashykt): Delete move assignment
127  ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
128 
129  // Runs interceptor at pos \a pos.
130  void RunInterceptor(
131  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
132  GPR_CODEGEN_ASSERT(pos < interceptors_.size());
133  interceptors_[pos]->Intercept(interceptor_methods);
134  }
135 
136  void RegisterInterceptors(
137  const std::vector<std::unique_ptr<
138  experimental::ClientInterceptorFactoryInterface>>& creators,
139  size_t interceptor_pos) {
140  if (interceptor_pos > creators.size()) {
141  // No interceptors to register
142  return;
143  }
144  // NOTE: The following is not a range-based for loop because it will only
145  // iterate over a portion of the creators vector.
146  for (auto it = creators.begin() + interceptor_pos; it != creators.end();
147  ++it) {
148  auto* interceptor = (*it)->CreateClientInterceptor(this);
149  if (interceptor != nullptr) {
150  interceptors_.push_back(
151  std::unique_ptr<experimental::Interceptor>(interceptor));
152  }
153  }
155  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
157  ->CreateClientInterceptor(this)));
158  }
159  }
160 
161  grpc::ClientContext* ctx_ = nullptr;
162  // TODO(yashykt): make type_ const once move-assignment is deleted
163  Type type_{Type::UNKNOWN};
164  const char* method_ = nullptr;
165  grpc::ChannelInterface* channel_ = nullptr;
166  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
167  bool hijacked_ = false;
168  size_t hijacked_interceptor_ = 0;
169 
171  friend class grpc::ClientContext;
172 };
173 
174 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
175 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
176 // Registers a global client interceptor factory object, which is used for all
177 // RPCs made in this process. The application is responsible for maintaining the
178 // life of the object while gRPC operations are in progress. The global
179 // interceptor factory should only be registered once at the start of the
180 // process before any gRPC operations have begun.
183 
184 // For testing purposes only
186 
187 } // namespace experimental
188 } // namespace grpc
189 
190 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
grpc::experimental::ClientRpcInfo::type
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: client_interceptor.h:98
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::experimental::ClientRpcInfo::method
const char * method() const
Return the fully-specified method name.
Definition: client_interceptor.h:88
grpc::experimental::ClientRpcInfo::Type
Type
Type categorizes RPCs by unary or streaming type.
Definition: client_interceptor.h:71
grpc::internal::RpcMethod::CLIENT_STREAMING
@ CLIENT_STREAMING
Definition: rpc_method.h:33
grpc::experimental::ClientRpcInfo
Definition: client_interceptor.h:66
grpc::internal::g_global_client_interceptor_factory
experimental::ClientInterceptorFactoryInterface * g_global_client_interceptor_factory
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
grpc::experimental::ClientInterceptorFactoryInterface::~ClientInterceptorFactoryInterface
virtual ~ClientInterceptorFactoryInterface()
Definition: client_interceptor.h:48
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:192
grpc::experimental::RegisterGlobalClientInterceptorFactory
void RegisterGlobalClientInterceptorFactory(ClientInterceptorFactoryInterface *factory)
grpc::experimental::ClientRpcInfo::~ClientRpcInfo
~ClientRpcInfo()
Definition: client_interceptor.h:79
grpc::experimental::ClientRpcInfo::Type::SERVER_STREAMING
@ SERVER_STREAMING
grpc::experimental::ClientRpcInfo::Type::BIDI_STREAMING
@ BIDI_STREAMING
grpc::experimental::ClientRpcInfo::Type::UNARY
@ UNARY
grpc::experimental::ClientRpcInfo::client_context
grpc::ClientContext * client_context()
Return a pointer to the underlying ClientContext structure associated with the RPC to support feature...
Definition: client_interceptor.h:95
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:71
grpc::experimental::ClientRpcInfo::channel
ChannelInterface * channel()
Return a pointer to the channel on which the RPC is being sent.
Definition: client_interceptor.h:91
grpc::internal::RpcMethod::SERVER_STREAMING
@ SERVER_STREAMING
Definition: rpc_method.h:34
grpc::experimental::ClientInterceptorFactoryInterface::CreateClientInterceptor
virtual Interceptor * CreateClientInterceptor(ClientRpcInfo *info)=0
rpc_method.h
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::internal::RpcMethod::NORMAL_RPC
@ NORMAL_RPC
Definition: rpc_method.h:32
grpc::experimental::Interceptor
Interface for an interceptor.
Definition: interceptor.h:216
string_ref.h
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:46
grpc::experimental::InterceptorBatchMethods
Class that is passed as an argument to the Intercept method of the application's Interceptor interfac...
Definition: interceptor.h:93
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::experimental::ClientRpcInfo::Type::CLIENT_STREAMING
@ CLIENT_STREAMING
grpc::experimental::ClientRpcInfo::Type::UNKNOWN
@ UNKNOWN
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:36
grpc::experimental::TestOnlyResetGlobalClientInterceptorFactory
void TestOnlyResetGlobalClientInterceptorFactory()
interceptor.h