GRPC C++  1.17.0
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 
27 
28 namespace grpc {
29 
30 class ClientContext;
31 class Channel;
32 
33 namespace internal {
34 class InterceptorBatchMethodsImpl;
35 }
36 
37 namespace experimental {
38 class ClientRpcInfo;
39 
41  public:
43  virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0;
44 };
45 } // namespace experimental
46 
47 namespace internal {
50 }
51 
52 namespace experimental {
54  public:
56 
58 
59  ClientRpcInfo(const ClientRpcInfo&) = delete;
60  ClientRpcInfo(ClientRpcInfo&&) = default;
61  ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
62 
63  // Getter methods
64  const char* method() { return method_; }
65  ChannelInterface* channel() { return channel_; }
66  grpc::ClientContext* client_context() { return ctx_; }
67 
68  private:
69  ClientRpcInfo(grpc::ClientContext* ctx, const char* method,
70  grpc::ChannelInterface* channel)
71  : ctx_(ctx), method_(method), channel_(channel) {}
72  // Runs interceptor at pos \a pos.
73  void RunInterceptor(
74  experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
75  GPR_CODEGEN_ASSERT(pos < interceptors_.size());
76  interceptors_[pos]->Intercept(interceptor_methods);
77  }
78 
79  void RegisterInterceptors(
80  const std::vector<std::unique_ptr<
82  size_t interceptor_pos) {
83  if (interceptor_pos > creators.size()) {
84  // No interceptors to register
85  return;
86  }
87  for (auto it = creators.begin() + interceptor_pos; it != creators.end();
88  ++it) {
89  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
90  (*it)->CreateClientInterceptor(this)));
91  }
93  interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
95  ->CreateClientInterceptor(this)));
96  }
97  }
98 
99  grpc::ClientContext* ctx_ = nullptr;
100  const char* method_ = nullptr;
101  grpc::ChannelInterface* channel_ = nullptr;
102  std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
103  bool hijacked_ = false;
104  size_t hijacked_interceptor_ = 0;
105 
107  friend class grpc::ClientContext;
108 };
109 
110 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
111 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
112 // Registers a global client interceptor factory object, which is used for all
113 // RPCs made in this process. If the argument is nullptr, the global
114 // interceptor factory is deregistered. The application is responsible for
115 // maintaining the life of the object while gRPC operations are in progress. It
116 // is unsafe to try to register/deregister if any gRPC operation is in progress.
117 // For safety, it is in the best interests of the developer to register the
118 // global interceptor factory once at the start of the process before any gRPC
119 // operations have begun. Deregistration is optional since gRPC does not
120 // maintain any references to the object.
123 
124 } // namespace experimental
125 } // namespace grpc
126 
127 #endif // GRPCPP_IMPL_CODEGEN_CLIENT_INTERCEPTOR_H
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
~ClientRpcInfo()
Definition: client_interceptor.h:57
void RegisterGlobalClientInterceptorFactory(ClientInterceptorFactoryInterface *factory)
ChannelInterface * channel()
Definition: client_interceptor.h:65
virtual ~ClientInterceptorFactoryInterface()
Definition: client_interceptor.h:42
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:165
grpc::ClientContext * client_context()
Definition: client_interceptor.h:66
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
Definition: interceptor.h:123
Codegen interface for grpc::Channel.
Definition: channel_interface.h:60
Definition: interceptor_common.h:36
experimental::ClientInterceptorFactoryInterface * g_global_client_interceptor_factory
ClientRpcInfo()
Definition: client_interceptor.h:55
const char * method()
Definition: client_interceptor.h:64
Definition: client_interceptor.h:53