GRPC C++  1.22.0-dev
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_impl {
30 
31 class Channel;
32 }
33 
34 namespace grpc {
35 
36 class ClientContext;
37 
38 namespace internal {
39 class InterceptorBatchMethodsImpl;
40 }
41 
42 namespace experimental {
43 class ClientRpcInfo;
44 
45 // A factory interface for creation of client interceptors. A vector of
46 // factories can be provided at channel creation which will be used to create a
47 // new vector of client interceptors per RPC. Client interceptor authors should
48 // create a subclass of ClientInterceptorFactorInterface which creates objects
49 // of their interceptors.
51  public:
53  // Returns a pointer to an Interceptor object on successful creation, nullptr
54  // otherwise. If nullptr is returned, this server interceptor factory is
55  // ignored for the purposes of that RPC.
56  virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0;
57 };
58 } // namespace experimental
59 
60 namespace internal {
63 }
64 
69 namespace experimental {
71  public:
72  // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN
73  // from the list of possible Types.
75  enum class Type {
76  UNARY,
77  CLIENT_STREAMING,
78  SERVER_STREAMING,
79  BIDI_STREAMING,
80  UNKNOWN // UNKNOWN is not API and will be removed later
81  };
82 
84 
85  // Delete copy constructor but allow default move constructor
86  ClientRpcInfo(const ClientRpcInfo&) = delete;
87  ClientRpcInfo(ClientRpcInfo&&) = default;
88 
89  // Getter methods
90 
92  const char* method() const { return method_; }
93 
95  ChannelInterface* channel() { return channel_; }
96 
99  grpc::ClientContext* client_context() { return ctx_; }
100 
102  Type type() const { return type_; }
103 
104  private:
105  static_assert(Type::UNARY ==
106  static_cast<Type>(internal::RpcMethod::NORMAL_RPC),
107  "violated expectation about Type enum");
108  static_assert(Type::CLIENT_STREAMING ==
109  static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING),
110  "violated expectation about Type enum");
111  static_assert(Type::SERVER_STREAMING ==
112  static_cast<Type>(internal::RpcMethod::SERVER_STREAMING),
113  "violated expectation about Type enum");
114  static_assert(Type::BIDI_STREAMING ==
115  static_cast<Type>(internal::RpcMethod::BIDI_STREAMING),
116  "violated expectation about Type enum");
117 
118  // Default constructor should only be used by ClientContext
119  ClientRpcInfo() = default;
120 
121  // Constructor will only be called from ClientContext
123  const char* method, grpc::ChannelInterface* channel)
124  : ctx_(ctx),
125  type_(static_cast<Type>(type)),
126  method_(method),
127  channel_(channel) {}
128 
129  // Move assignment should only be used by ClientContext
130  // TODO(yashykt): Delete move assignment
131  ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
132 
133  // Runs interceptor at pos \a pos.
134  void RunInterceptor(
135  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
136  GPR_CODEGEN_ASSERT(pos < interceptors_.size());
137  interceptors_[pos]->Intercept(interceptor_methods);
138  }
139 
140  void RegisterInterceptors(
141  const std::vector<std::unique_ptr<
143  size_t interceptor_pos) {
144  if (interceptor_pos > creators.size()) {
145  // No interceptors to register
146  return;
147  }
148  for (auto it = creators.begin() + interceptor_pos; it != creators.end();
149  ++it) {
150  auto* interceptor = (*it)->CreateClientInterceptor(this);
151  if (interceptor != nullptr) {
152  interceptors_.push_back(
153  std::unique_ptr<experimental::Interceptor>(interceptor));
154  }
155  }
157  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
159  ->CreateClientInterceptor(this)));
160  }
161  }
162 
163  grpc::ClientContext* ctx_ = nullptr;
164  // TODO(yashykt): make type_ const once move-assignment is deleted
165  Type type_{Type::UNKNOWN};
166  const char* method_ = nullptr;
167  grpc::ChannelInterface* channel_ = nullptr;
168  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
169  bool hijacked_ = false;
170  size_t hijacked_interceptor_ = 0;
171 
173  friend class grpc::ClientContext;
174 };
175 
176 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
177 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
178 // Registers a global client interceptor factory object, which is used for all
179 // RPCs made in this process. The application is responsible for maintaining the
180 // life of the object while gRPC operations are in progress. The global
181 // interceptor factory should only be registered once at the start of the
182 // process before any gRPC operations have begun.
185 
186 // For testing purposes only
188 
189 } // namespace experimental
190 } // namespace grpc
191 
192 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_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
~ClientRpcInfo()
Definition: client_interceptor.h:83
void RegisterGlobalClientInterceptorFactory(ClientInterceptorFactoryInterface *factory)
ChannelInterface * channel()
Return a pointer to the channel on which the RPC is being sent.
Definition: client_interceptor.h:95
virtual ~ClientInterceptorFactoryInterface()
Definition: client_interceptor.h:52
RpcType
Definition: rpc_method.h:31
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:179
grpc::ClientContext * client_context()
Return a pointer to the underlying ClientContext structure associated with the RPC to support feature...
Definition: client_interceptor.h:99
Type type() const
Return the type of the RPC (unary or a streaming flavor)
Definition: client_interceptor.h:102
::grpc_impl::Channel Channel
Definition: channel.h:26
const char * method() const
Return the fully-specified method name.
Definition: client_interceptor.h:92
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Interface for an interceptor.
Definition: interceptor.h:213
Codegen interface for grpc::Channel.
Definition: channel_interface.h:69
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
void TestOnlyResetGlobalClientInterceptorFactory()
experimental::ClientInterceptorFactoryInterface * g_global_client_interceptor_factory
Unknown error.
Definition: status_code_enum.h:35
Type
Type categorizes RPCs by unary or streaming type.
Definition: client_interceptor.h:75
Definition: client_interceptor.h:70