Go to the documentation of this file.
19 #ifndef GRPCPP_IMPL_INTERCEPTOR_COMMON_H
20 #define GRPCPP_IMPL_INTERCEPTOR_COMMON_H
32 #include "absl/log/absl_check.h"
44 static_cast<size_t>(i) + 1)) {
45 hooks_[
static_cast<size_t>(i)] =
false;
53 return hooks_[
static_cast<size_t>(type)];
58 return ProceedClient();
66 ABSL_CHECK(!reverse_ && ops_ !=
nullptr &&
69 ABSL_CHECK(!ran_hijacking_interceptor_);
71 rpc_info->hijacked_ =
true;
72 rpc_info->hijacked_interceptor_ = current_interceptor_index_;
75 ran_hijacking_interceptor_ =
true;
76 rpc_info->RunInterceptor(
this, current_interceptor_index_);
80 hooks_[
static_cast<size_t>(type)] =
true;
84 ABSL_CHECK_NE(orig_send_message_,
nullptr);
85 if (*orig_send_message_ !=
nullptr) {
86 ABSL_CHECK(serializer_(*orig_send_message_).ok());
87 *orig_send_message_ =
nullptr;
93 ABSL_CHECK_NE(orig_send_message_,
nullptr);
94 return *orig_send_message_;
98 ABSL_CHECK_NE(orig_send_message_,
nullptr);
99 *orig_send_message_ = message;
105 return send_initial_metadata_;
120 return send_trailing_metadata_;
127 return recv_initial_metadata_->
map();
133 ABSL_CHECK(hooks_[
static_cast<size_t>(
135 *fail_send_message_ =
true;
140 return recv_trailing_metadata_->
map();
144 bool* fail_send_message,
145 std::function<
Status(
const void*)> serializer) {
147 orig_send_message_ = msg;
148 fail_send_message_ = fail_send_message;
149 serializer_ = serializer;
153 std::multimap<std::string, std::string>* metadata) {
154 send_initial_metadata_ = metadata;
158 std::string* error_message) {
160 error_details_ = error_details;
161 error_message_ = error_message;
165 std::multimap<std::string, std::string>* metadata) {
166 send_trailing_metadata_ = metadata;
170 recv_message_ = message;
171 hijacked_recv_message_failed_ = hijacked_recv_message_failed;
175 recv_initial_metadata_ = map;
181 recv_trailing_metadata_ = map;
186 if (info ==
nullptr) {
187 return std::unique_ptr<ChannelInterface>(
nullptr);
192 info->channel(), current_interceptor_index_ + 1));
196 ABSL_CHECK(hooks_[
static_cast<size_t>(
198 *hijacked_recv_message_failed_ =
true;
204 ran_hijacking_interceptor_ =
false;
211 ran_hijacking_interceptor_ =
false;
226 if (client_rpc_info !=
nullptr) {
227 return client_rpc_info->interceptors_.empty();
231 return server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty();
242 if (client_rpc_info !=
nullptr) {
243 if (client_rpc_info->interceptors_.empty()) {
246 RunClientInterceptors();
252 if (server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty()) {
255 RunServerInterceptors();
265 ABSL_CHECK_EQ(reverse_,
true);
268 if (server_rpc_info ==
nullptr || server_rpc_info->interceptors_.empty()) {
271 callback_ = std::move(f);
272 RunServerInterceptors();
277 void RunClientInterceptors() {
280 current_interceptor_index_ = 0;
282 if (rpc_info->hijacked_) {
283 current_interceptor_index_ = rpc_info->hijacked_interceptor_;
285 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
288 rpc_info->RunInterceptor(
this, current_interceptor_index_);
291 void RunServerInterceptors() {
294 current_interceptor_index_ = 0;
296 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
298 rpc_info->RunInterceptor(
this, current_interceptor_index_);
301 void ProceedClient() {
303 if (rpc_info->hijacked_ && !reverse_ &&
304 current_interceptor_index_ == rpc_info->hijacked_interceptor_ &&
305 !ran_hijacking_interceptor_) {
309 ran_hijacking_interceptor_ =
true;
310 rpc_info->RunInterceptor(
this, current_interceptor_index_);
314 current_interceptor_index_++;
316 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
317 if (rpc_info->hijacked_ &&
318 current_interceptor_index_ > rpc_info->hijacked_interceptor_) {
322 rpc_info->RunInterceptor(
this, current_interceptor_index_);
330 if (current_interceptor_index_ > 0) {
332 current_interceptor_index_--;
333 rpc_info->RunInterceptor(
this, current_interceptor_index_);
341 void ProceedServer() {
344 current_interceptor_index_++;
345 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
346 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
352 if (current_interceptor_index_ > 0) {
354 current_interceptor_index_--;
355 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
360 ABSL_CHECK(callback_);
364 void ClearHookPoints() {
368 static_cast<size_t>(i) + 1)) {
369 hooks_[
static_cast<size_t>(i)] =
false;
378 size_t current_interceptor_index_ = 0;
379 bool reverse_ =
false;
380 bool ran_hijacking_interceptor_ =
false;
381 Call* call_ =
nullptr;
383 CallOpSetInterface* ops_ =
nullptr;
384 std::function<void(
void)> callback_;
386 ByteBuffer* send_message_ =
nullptr;
387 bool* fail_send_message_ =
nullptr;
388 const void** orig_send_message_ =
nullptr;
389 std::function<
Status(
const void*)> serializer_;
391 std::multimap<std::string, std::string>* send_initial_metadata_;
394 std::string* error_details_ =
nullptr;
395 std::string* error_message_ =
nullptr;
397 std::multimap<std::string, std::string>* send_trailing_metadata_ =
nullptr;
399 void* recv_message_ =
nullptr;
400 bool* hijacked_recv_message_failed_ =
nullptr;
402 MetadataMap* recv_initial_metadata_ =
nullptr;
404 Status* recv_status_ =
nullptr;
406 MetadataMap* recv_trailing_metadata_ =
nullptr;
426 ABSL_CHECK(
false) <<
"It is illegal to call Hijack on a method which has a "
427 "Cancel notification";
432 <<
"It is illegal to call GetSendMessage on a method which "
433 "has a Cancel notification";
439 <<
"It is illegal to call GetSendMessageStatus on a method which "
440 "has a Cancel notification";
446 <<
"It is illegal to call GetOriginalSendMessage on a method which "
447 "has a Cancel notification";
453 <<
"It is illegal to call ModifySendMessage on a method which "
454 "has a Cancel notification";
458 ABSL_CHECK(
false) <<
"It is illegal to call GetSendInitialMetadata on a "
459 "method which has a Cancel notification";
465 <<
"It is illegal to call GetSendStatus on a method which "
466 "has a Cancel notification";
471 ABSL_CHECK(
false) <<
"It is illegal to call ModifySendStatus on a method "
472 "which has a Cancel notification";
476 ABSL_CHECK(
false) <<
"It is illegal to call GetSendTrailingMetadata on a "
477 "method which has a Cancel notification";
483 <<
"It is illegal to call GetRecvMessage on a method which "
484 "has a Cancel notification";
490 ABSL_CHECK(
false) <<
"It is illegal to call GetRecvInitialMetadata on a "
491 "method which has a Cancel notification";
497 <<
"It is illegal to call GetRecvStatus on a method which "
498 "has a Cancel notification";
504 ABSL_CHECK(
false) <<
"It is illegal to call GetRecvTrailingMetadata on a "
505 "method which has a Cancel notification";
510 ABSL_CHECK(
false) <<
"It is illegal to call GetInterceptedChannel on a "
511 "method which has a Cancel notification";
512 return std::unique_ptr<ChannelInterface>(
nullptr);
516 ABSL_CHECK(
false) <<
"It is illegal to call FailHijackedRecvMessage on a "
517 "method which has a Cancel notification";
521 ABSL_CHECK(
false) <<
"It is illegal to call FailHijackedSendMessage on a "
522 "method which has a Cancel notification";
528 #endif // GRPCPP_IMPL_INTERCEPTOR_COMMON_H
ByteBuffer * GetSerializedSendMessage() override
Send Message Methods GetSerializedSendMessage and GetSendMessage/ModifySendMessage are the available ...
Definition: interceptor_common.h:83
void Proceed() override
Signal that the interceptor is done intercepting the current batch of the RPC.
Definition: interceptor_common.h:56
void ModifySendStatus(const Status &status) override
Overwrites the status with status.
Definition: interceptor_common.h:113
std::multimap< std::string, std::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:475
void ModifySendMessage(const void *) override
Overwrites the message to be sent with message.
Definition: interceptor_common.h:451
Status GetSendStatus() override
Returns the status to be sent. Valid for PRE_SEND_STATUS interceptions.
Definition: interceptor_common.h:108
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:51
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:97
grpc_status_code
Definition: status.h:28
void SetRecvInitialMetadata(MetadataMap *map)
Definition: interceptor_common.h:174
Status GetSendStatus() override
Returns the status to be sent. Valid for PRE_SEND_STATUS interceptions.
Definition: interceptor_common.h:463
void SetCallOpSetInterface(CallOpSetInterface *ops)
Definition: interceptor_common.h:220
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvInitialMetadata() override
Returns a modifiable multimap of the received initial metadata.
Definition: interceptor_common.h:125
void SetCall(Call *call)
Definition: interceptor_common.h:216
bool InterceptorsListEmpty()
Definition: interceptor_common.h:224
InterceptorBatchMethodsImpl()
Definition: interceptor_common.h:40
void FailHijackedRecvMessage() override
On a hijacked RPC, an interceptor can decide to fail a PRE_RECV_MESSAGE op.
Definition: interceptor_common.h:515
void ClearState()
Definition: interceptor_common.h:202
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:132
@ 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:102
std::unique_ptr< ChannelInterface > GetInterceptedChannel() override
Gets an intercepted channel.
Definition: interceptor_common.h:509
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:157
void * GetRecvMessage() override
Returns a pointer to the modifiable received message.
Definition: interceptor_common.h:481
bool RunInterceptors(std::function< void(void)> f)
Definition: interceptor_common.h:263
std::multimap< std::string, std::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:119
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:444
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:59
void SetReverse()
Definition: interceptor_common.h:209
void SetRecvStatus(Status *status)
Definition: interceptor_common.h:178
void * GetRecvMessage() override
Returns a pointer to the modifiable received message.
Definition: interceptor_common.h:123
std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvInitialMetadata() override
Returns a modifiable multimap of the received initial metadata.
Definition: interceptor_common.h:488
void ModifySendStatus(const Status &) override
Overwrites the status with status.
Definition: interceptor_common.h:470
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:64
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:430
std::multimap< std::string, std::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:104
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:92
void SetRecvMessage(void *message, bool *hijacked_recv_message_failed)
Definition: interceptor_common.h:169
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:495
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:424
void FailHijackedRecvMessage() override
On a hijacked RPC, an interceptor can decide to fail a PRE_RECV_MESSAGE op.
Definition: interceptor_common.h:195
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:138
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:143
~InterceptorBatchMethodsImpl() override
Definition: interceptor_common.h:49
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:130
std::unique_ptr< ChannelInterface > GetInterceptedChannel() override
Gets an intercepted channel.
Definition: interceptor_common.h:184
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:419
bool GetSendMessageStatus() override
Checks whether the SEND MESSAGE op succeeded.
Definition: interceptor_common.h:437
bool RunInterceptors()
Definition: interceptor_common.h:239
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:520
Definition: interceptor_common.h:411
void SetRecvTrailingMetadata(MetadataMap *map)
Definition: interceptor_common.h:180
void SetSendInitialMetadata(std::multimap< std::string, std::string > *metadata)
Definition: interceptor_common.h:152
::absl::Status Status
Definition: config_protobuf.h:107
bool QueryInterceptionHookPoint(experimental::InterceptionHookPoints type) override
Determine whether the current batch has an interception hook point of type type.
Definition: interceptor_common.h:414
InterceptionHookPoints
An enumeration of different possible points at which the Intercept method of the Interceptor interfac...
Definition: interceptor.h:56
Definition: interceptor_common.h:37
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:164
std::multimap< std::string, std::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:457
void AddInterceptionHookPoint(experimental::InterceptionHookPoints type)
Definition: interceptor_common.h:79
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:502