Go to the documentation of this file.
19 #ifndef GRPCPP_IMPL_INTERCEPTOR_COMMON_H
20 #define GRPCPP_IMPL_INTERCEPTOR_COMMON_H
25 #include "absl/log/absl_check.h"
45 static_cast<size_t>(i) + 1)) {
46 hooks_[
static_cast<size_t>(i)] =
false;
54 return hooks_[
static_cast<size_t>(type)];
59 return ProceedClient();
67 ABSL_CHECK(!reverse_ && ops_ !=
nullptr &&
70 ABSL_CHECK(!ran_hijacking_interceptor_);
72 rpc_info->hijacked_ =
true;
73 rpc_info->hijacked_interceptor_ = current_interceptor_index_;
76 ran_hijacking_interceptor_ =
true;
77 rpc_info->RunInterceptor(
this, current_interceptor_index_);
81 hooks_[
static_cast<size_t>(type)] =
true;
85 ABSL_CHECK_NE(orig_send_message_,
nullptr);
86 if (*orig_send_message_ !=
nullptr) {
87 ABSL_CHECK(serializer_(*orig_send_message_).ok());
88 *orig_send_message_ =
nullptr;
94 ABSL_CHECK_NE(orig_send_message_,
nullptr);
95 return *orig_send_message_;
99 ABSL_CHECK_NE(orig_send_message_,
nullptr);
100 *orig_send_message_ = message;
106 return send_initial_metadata_;
121 return send_trailing_metadata_;
128 return recv_initial_metadata_->
map();
134 ABSL_CHECK(hooks_[
static_cast<size_t>(
136 *fail_send_message_ =
true;
141 return recv_trailing_metadata_->
map();
145 bool* fail_send_message,
146 std::function<
Status(
const void*)> serializer) {
148 orig_send_message_ = msg;
149 fail_send_message_ = fail_send_message;
150 serializer_ = serializer;
154 std::multimap<std::string, std::string>* metadata) {
155 send_initial_metadata_ = metadata;
159 std::string* error_message) {
161 error_details_ = error_details;
162 error_message_ = error_message;
166 std::multimap<std::string, std::string>* metadata) {
167 send_trailing_metadata_ = metadata;
171 recv_message_ = message;
172 hijacked_recv_message_failed_ = hijacked_recv_message_failed;
176 recv_initial_metadata_ = map;
182 recv_trailing_metadata_ = map;
187 if (info ==
nullptr) {
188 return std::unique_ptr<ChannelInterface>(
nullptr);
193 info->channel(), current_interceptor_index_ + 1));
197 ABSL_CHECK(hooks_[
static_cast<size_t>(
199 *hijacked_recv_message_failed_ =
true;
205 ran_hijacking_interceptor_ =
false;
212 ran_hijacking_interceptor_ =
false;
227 if (client_rpc_info !=
nullptr) {
228 return client_rpc_info->interceptors_.empty();
232 return server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty();
243 if (client_rpc_info !=
nullptr) {
244 if (client_rpc_info->interceptors_.empty()) {
247 RunClientInterceptors();
253 if (server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty()) {
256 RunServerInterceptors();
266 ABSL_CHECK_EQ(reverse_,
true);
269 if (server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty()) {
272 callback_ = std::move(f);
273 RunServerInterceptors();
278 void RunClientInterceptors() {
281 current_interceptor_index_ = 0;
283 if (rpc_info->hijacked_) {
284 current_interceptor_index_ = rpc_info->hijacked_interceptor_;
286 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
289 rpc_info->RunInterceptor(
this, current_interceptor_index_);
292 void RunServerInterceptors() {
295 current_interceptor_index_ = 0;
297 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
299 rpc_info->RunInterceptor(
this, current_interceptor_index_);
302 void ProceedClient() {
304 if (rpc_info->hijacked_ && !reverse_ &&
305 current_interceptor_index_ == rpc_info->hijacked_interceptor_ &&
306 !ran_hijacking_interceptor_) {
310 ran_hijacking_interceptor_ =
true;
311 rpc_info->RunInterceptor(
this, current_interceptor_index_);
315 current_interceptor_index_++;
317 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
318 if (rpc_info->hijacked_ &&
319 current_interceptor_index_ > rpc_info->hijacked_interceptor_) {
323 rpc_info->RunInterceptor(
this, current_interceptor_index_);
331 if (current_interceptor_index_ > 0) {
333 current_interceptor_index_--;
334 rpc_info->RunInterceptor(
this, current_interceptor_index_);
342 void ProceedServer() {
345 current_interceptor_index_++;
346 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
347 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
353 if (current_interceptor_index_ > 0) {
355 current_interceptor_index_--;
356 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
361 ABSL_CHECK(callback_);
365 void ClearHookPoints() {
369 static_cast<size_t>(i) + 1)) {
370 hooks_[
static_cast<size_t>(i)] =
false;
379 size_t current_interceptor_index_ = 0;
380 bool reverse_ =
false;
381 bool ran_hijacking_interceptor_ =
false;
382 Call* call_ =
nullptr;
384 CallOpSetInterface* ops_ =
nullptr;
385 std::function<void(
void)> callback_;
387 ByteBuffer* send_message_ =
nullptr;
388 bool* fail_send_message_ =
nullptr;
389 const void** orig_send_message_ =
nullptr;
390 std::function<
Status(
const void*)> serializer_;
392 std::multimap<std::string, std::string>* send_initial_metadata_;
395 std::string* error_details_ =
nullptr;
396 std::string* error_message_ =
nullptr;
398 std::multimap<std::string, std::string>* send_trailing_metadata_ =
nullptr;
400 void* recv_message_ =
nullptr;
401 bool* hijacked_recv_message_failed_ =
nullptr;
403 MetadataMap* recv_initial_metadata_ =
nullptr;
405 Status* recv_status_ =
nullptr;
407 MetadataMap* recv_trailing_metadata_ =
nullptr;
427 ABSL_CHECK(
false) <<
"It is illegal to call Hijack on a method which has a "
428 "Cancel notification";
433 <<
"It is illegal to call GetSendMessage on a method which "
434 "has a Cancel notification";
440 <<
"It is illegal to call GetSendMessageStatus on a method which "
441 "has a Cancel notification";
447 <<
"It is illegal to call GetOriginalSendMessage on a method which "
448 "has a Cancel notification";
454 <<
"It is illegal to call ModifySendMessage on a method which "
455 "has a Cancel notification";
459 ABSL_CHECK(
false) <<
"It is illegal to call GetSendInitialMetadata on a "
460 "method which has a Cancel notification";
466 <<
"It is illegal to call GetSendStatus on a method which "
467 "has a Cancel notification";
472 ABSL_CHECK(
false) <<
"It is illegal to call ModifySendStatus on a method "
473 "which has a Cancel notification";
477 ABSL_CHECK(
false) <<
"It is illegal to call GetSendTrailingMetadata on a "
478 "method which has a Cancel notification";
484 <<
"It is illegal to call GetRecvMessage on a method which "
485 "has a Cancel notification";
491 ABSL_CHECK(
false) <<
"It is illegal to call GetRecvInitialMetadata on a "
492 "method which has a Cancel notification";
498 <<
"It is illegal to call GetRecvStatus on a method which "
499 "has a Cancel notification";
505 ABSL_CHECK(
false) <<
"It is illegal to call GetRecvTrailingMetadata on a "
506 "method which has a Cancel notification";
511 ABSL_CHECK(
false) <<
"It is illegal to call GetInterceptedChannel on a "
512 "method which has a Cancel notification";
513 return std::unique_ptr<ChannelInterface>(
nullptr);
517 ABSL_CHECK(
false) <<
"It is illegal to call FailHijackedRecvMessage on a "
518 "method which has a Cancel notification";
522 ABSL_CHECK(
false) <<
"It is illegal to call FailHijackedSendMessage on a "
523 "method which has a Cancel notification";
529 #endif // GRPCPP_IMPL_INTERCEPTOR_COMMON_H
ByteBuffer * GetSerializedSendMessage() override
Send Message Methods GetSerializedSendMessage and GetSendMessage/ModifySendMessage are the available ...
Definition: interceptor_common.h:84
void Proceed() override
Signal that the interceptor is done intercepting the current batch of the RPC.
Definition: interceptor_common.h:57
void ModifySendStatus(const Status &status) override
Overwrites the status with status.
Definition: interceptor_common.h:114
std::multimap< std::string, std::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:476
void ModifySendMessage(const void *) override
Overwrites the message to be sent with message.
Definition: interceptor_common.h:452
Status GetSendStatus() override
Returns the status to be sent. Valid for PRE_SEND_STATUS interceptions.
Definition: interceptor_common.h:109
virtual void SetHijackingState()=0
bool QueryInterceptionHookPoint(experimental::InterceptionHookPoints type) override
Determine whether the current batch has an interception hook point of type type.
Definition: interceptor_common.h:52
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
void ModifySendMessage(const void *message) override
Overwrites the message to be sent with message.
Definition: interceptor_common.h:98
grpc_status_code
Definition: status.h:28
void SetRecvInitialMetadata(MetadataMap *map)
Definition: interceptor_common.h:175
Status GetSendStatus() override
Returns the status to be sent. Valid for PRE_SEND_STATUS interceptions.
Definition: interceptor_common.h:464
void SetCallOpSetInterface(CallOpSetInterface *ops)
Definition: interceptor_common.h:221
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvInitialMetadata() override
Returns a modifiable multimap of the received initial metadata.
Definition: interceptor_common.h:126
void SetCall(Call *call)
Definition: interceptor_common.h:217
bool InterceptorsListEmpty()
Definition: interceptor_common.h:225
InterceptorBatchMethodsImpl()
Definition: interceptor_common.h:41
void FailHijackedRecvMessage() override
On a hijacked RPC, an interceptor can decide to fail a PRE_RECV_MESSAGE op.
Definition: interceptor_common.h:516
void ClearState()
Definition: interceptor_common.h:203
Straightforward wrapping of the C call object.
Definition: call.h:36
void FailHijackedSendMessage() override
On a hijacked RPC/ to-be hijacked RPC, this can be called to fail a SEND MESSAGE op.
Definition: interceptor_common.h:133
@ PRE_SEND_CANCEL
This is a special hook point available to both clients and servers when TryCancel() is performed.
bool GetSendMessageStatus() override
Checks whether the SEND MESSAGE op succeeded.
Definition: interceptor_common.h:103
std::unique_ptr< ChannelInterface > GetInterceptedChannel() override
Gets an intercepted channel.
Definition: interceptor_common.h:510
Did it work? If it didn't, why?
Definition: status.h:34
void SetSendStatus(grpc_status_code *code, std::string *error_details, std::string *error_message)
Definition: interceptor_common.h:158
void * GetRecvMessage() override
Returns a pointer to the modifiable received message.
Definition: interceptor_common.h:482
bool RunInterceptors(std::function< void(void)> f)
Definition: interceptor_common.h:264
std::multimap< std::string, std::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:120
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:445
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call_op_set_interface.h:36
A sequence of bytes.
Definition: byte_buffer.h:60
void SetReverse()
Definition: interceptor_common.h:210
void SetRecvStatus(Status *status)
Definition: interceptor_common.h:179
void * GetRecvMessage() override
Returns a pointer to the modifiable received message.
Definition: interceptor_common.h:124
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvInitialMetadata() override
Returns a modifiable multimap of the received initial metadata.
Definition: interceptor_common.h:489
void ModifySendStatus(const Status &) override
Overwrites the status with status.
Definition: interceptor_common.h:471
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:65
An InterceptedChannel is available to client Interceptors.
Definition: intercepted_channel.h:35
std::string error_message() const
Return the instance's error message.
Definition: status.h:119
StatusCode
Definition: status_code_enum.h:26
ByteBuffer * GetSerializedSendMessage() override
Send Message Methods GetSerializedSendMessage and GetSendMessage/ModifySendMessage are the available ...
Definition: interceptor_common.h:431
std::multimap< std::string, std::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:105
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:93
void SetRecvMessage(void *message, bool *hijacked_recv_message_failed)
Definition: interceptor_common.h:170
StatusCode error_code() const
Return the instance's error code.
Definition: status.h:117
Status * GetRecvStatus() override
Returns a modifiable view of the received status on PRE_RECV_STATUS and POST_RECV_STATUS interception...
Definition: interceptor_common.h:496
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:425
void FailHijackedRecvMessage() override
On a hijacked RPC, an interceptor can decide to fail a PRE_RECV_MESSAGE op.
Definition: interceptor_common.h:196
experimental::ServerRpcInfo * server_rpc_info() const
Definition: call.h:79
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvTrailingMetadata() override
Returns a modifiable multimap of the received trailing metadata on PRE_RECV_STATUS and POST_RECV_STAT...
Definition: interceptor_common.h:139
virtual void ContinueFillOpsAfterInterception()=0
void SetSendMessage(ByteBuffer *buf, const void **msg, bool *fail_send_message, std::function< Status(const void *)> serializer)
Definition: interceptor_common.h:144
~InterceptorBatchMethodsImpl() override
Definition: interceptor_common.h:50
virtual void ContinueFinalizeResultAfterInterception()=0
Status * GetRecvStatus() override
Returns a modifiable view of the received status on PRE_RECV_STATUS and POST_RECV_STATUS interception...
Definition: interceptor_common.h:131
std::unique_ptr< ChannelInterface > GetInterceptedChannel() override
Gets an intercepted channel.
Definition: interceptor_common.h:185
Class that is passed as an argument to the Intercept method of the application's Interceptor interfac...
Definition: interceptor.h:95
void Proceed() override
Signal that the interceptor is done intercepting the current batch of the RPC.
Definition: interceptor_common.h:420
bool GetSendMessageStatus() override
Checks whether the SEND MESSAGE op succeeded.
Definition: interceptor_common.h:438
bool RunInterceptors()
Definition: interceptor_common.h:240
void FailHijackedSendMessage() override
On a hijacked RPC/ to-be hijacked RPC, this can be called to fail a SEND MESSAGE op.
Definition: interceptor_common.h:521
Definition: interceptor_common.h:412
void SetRecvTrailingMetadata(MetadataMap *map)
Definition: interceptor_common.h:181
void SetSendInitialMetadata(std::multimap< std::string, std::string > *metadata)
Definition: interceptor_common.h:153
::absl::Status Status
Definition: config_protobuf.h:106
bool QueryInterceptionHookPoint(experimental::InterceptionHookPoints type) override
Determine whether the current batch has an interception hook point of type type.
Definition: interceptor_common.h:415
InterceptionHookPoints
An enumeration of different possible points at which the Intercept method of the Interceptor interfac...
Definition: interceptor.h:56
Definition: interceptor_common.h:38
std::string error_details() const
Return the (binary) error details.
Definition: status.h:122
void SetSendTrailingMetadata(std::multimap< std::string, std::string > *metadata)
Definition: interceptor_common.h:165
std::multimap< std::string, std::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:458
void AddInterceptionHookPoint(experimental::InterceptionHookPoints type)
Definition: interceptor_common.h:80
experimental::ClientRpcInfo * client_rpc_info() const
Definition: call.h:75
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvTrailingMetadata() override
Returns a modifiable multimap of the received trailing metadata on PRE_RECV_STATUS and POST_RECV_STAT...
Definition: interceptor_common.h:503