Go to the documentation of this file.
19 #ifndef GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H
20 #define GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H
40 for (
auto i = static_cast<experimental::InterceptionHookPoints>(0);
42 i = static_cast<experimental::InterceptionHookPoints>(
43 static_cast<size_t>(i) + 1)) {
44 hooks_[static_cast<size_t>(i)] =
false;
52 return hooks_[static_cast<size_t>(type)];
57 return ProceedClient();
70 rpc_info->hijacked_ =
true;
71 rpc_info->hijacked_interceptor_ = current_interceptor_index_;
74 ran_hijacking_interceptor_ =
true;
75 rpc_info->RunInterceptor(
this, current_interceptor_index_);
79 hooks_[static_cast<size_t>(type)] =
true;
84 if (*orig_send_message_ !=
nullptr) {
86 *orig_send_message_ =
nullptr;
93 return *orig_send_message_;
98 *orig_send_message_ = message;
104 return send_initial_metadata_;
108 return Status(static_cast<StatusCode>(*code_), *error_message_,
113 *code_ = static_cast<grpc_status_code>(status.
error_code());
120 return send_trailing_metadata_;
127 return recv_initial_metadata_->
map();
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<grpc::string, grpc::string>* metadata) {
154 send_initial_metadata_ = metadata;
160 error_details_ = error_details;
161 error_message_ = error_message;
165 std::multimap<grpc::string, grpc::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));
198 *hijacked_recv_message_failed_ =
true;
204 ran_hijacking_interceptor_ =
false;
211 ran_hijacking_interceptor_ =
false;
226 if (client_rpc_info !=
nullptr) {
227 if (client_rpc_info->interceptors_.size() == 0) {
235 if (server_rpc_info ==
nullptr ||
236 server_rpc_info->interceptors_.size() == 0) {
250 if (client_rpc_info !=
nullptr) {
251 if (client_rpc_info->interceptors_.size() == 0) {
254 RunClientInterceptors();
260 if (server_rpc_info ==
nullptr ||
261 server_rpc_info->interceptors_.size() == 0) {
264 RunServerInterceptors();
277 if (server_rpc_info ==
nullptr ||
278 server_rpc_info->interceptors_.size() == 0) {
281 callback_ = std::move(f);
282 RunServerInterceptors();
287 void RunClientInterceptors() {
290 current_interceptor_index_ = 0;
292 if (rpc_info->hijacked_) {
293 current_interceptor_index_ = rpc_info->hijacked_interceptor_;
295 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
298 rpc_info->RunInterceptor(
this, current_interceptor_index_);
301 void RunServerInterceptors() {
304 current_interceptor_index_ = 0;
306 current_interceptor_index_ = rpc_info->interceptors_.size() - 1;
308 rpc_info->RunInterceptor(
this, current_interceptor_index_);
311 void ProceedClient() {
313 if (rpc_info->hijacked_ && !reverse_ &&
314 current_interceptor_index_ == rpc_info->hijacked_interceptor_ &&
315 !ran_hijacking_interceptor_) {
319 ran_hijacking_interceptor_ =
true;
320 rpc_info->RunInterceptor(
this, current_interceptor_index_);
324 current_interceptor_index_++;
326 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
327 if (rpc_info->hijacked_ &&
328 current_interceptor_index_ > rpc_info->hijacked_interceptor_) {
332 rpc_info->RunInterceptor(
this, current_interceptor_index_);
340 if (current_interceptor_index_ > 0) {
342 current_interceptor_index_--;
343 rpc_info->RunInterceptor(
this, current_interceptor_index_);
351 void ProceedServer() {
354 current_interceptor_index_++;
355 if (current_interceptor_index_ < rpc_info->interceptors_.size()) {
356 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
362 if (current_interceptor_index_ > 0) {
364 current_interceptor_index_--;
365 return rpc_info->RunInterceptor(
this, current_interceptor_index_);
374 void ClearHookPoints() {
375 for (
auto i = static_cast<experimental::InterceptionHookPoints>(0);
377 i = static_cast<experimental::InterceptionHookPoints>(
378 static_cast<size_t>(i) + 1)) {
379 hooks_[static_cast<size_t>(i)] =
false;
388 size_t current_interceptor_index_ = 0;
389 bool reverse_ =
false;
390 bool ran_hijacking_interceptor_ =
false;
391 Call* call_ =
nullptr;
393 CallOpSetInterface* ops_ =
nullptr;
394 std::function<void(
void)> callback_;
396 ByteBuffer* send_message_ =
nullptr;
397 bool* fail_send_message_ =
nullptr;
398 const void** orig_send_message_ =
nullptr;
399 std::function<
Status(
const void*)> serializer_;
401 std::multimap<grpc::string, grpc::string>* send_initial_metadata_;
407 std::multimap<grpc::string, grpc::string>* send_trailing_metadata_ =
nullptr;
409 void* recv_message_ =
nullptr;
410 bool* hijacked_recv_message_failed_ =
nullptr;
412 MetadataMap* recv_initial_metadata_ =
nullptr;
414 Status* recv_status_ =
nullptr;
416 MetadataMap* recv_trailing_metadata_ =
nullptr;
441 "It is illegal to call Hijack on a method which has a "
442 "Cancel notification");
447 "It is illegal to call GetSendMessage on a method which "
448 "has a Cancel notification");
455 "It is illegal to call GetSendMessageStatus on a method which "
456 "has a Cancel notification");
463 "It is illegal to call GetOriginalSendMessage on a method which "
464 "has a Cancel notification");
471 "It is illegal to call ModifySendMessage on a method which "
472 "has a Cancel notification");
477 "It is illegal to call GetSendInitialMetadata on a "
478 "method which has a Cancel notification");
484 "It is illegal to call GetSendStatus on a method which "
485 "has a Cancel notification");
491 "It is illegal to call ModifySendStatus on a method "
492 "which has a Cancel notification");
499 "It is illegal to call GetSendTrailingMetadata on a "
500 "method which has a Cancel notification");
506 "It is illegal to call GetRecvMessage on a method which "
507 "has a Cancel notification");
514 "It is illegal to call GetRecvInitialMetadata on a "
515 "method which has a Cancel notification");
521 "It is illegal to call GetRecvStatus on a method which "
522 "has a Cancel notification");
529 "It is illegal to call GetRecvTrailingMetadata on a "
530 "method which has a Cancel notification");
536 "It is illegal to call GetInterceptedChannel on a "
537 "method which has a Cancel notification");
538 return std::unique_ptr<ChannelInterface>(
nullptr);
543 "It is illegal to call FailHijackedRecvMessage on a "
544 "method which has a Cancel notification");
549 "It is illegal to call FailHijackedSendMessage on a "
550 "method which has a Cancel notification");
556 #endif // GRPCPP_IMPL_CODEGEN_INTERCEPTOR_COMMON_H
grpc::string error_details() const
Return the (binary) error details.
Definition: status.h:115
ByteBuffer * GetSerializedSendMessage() override
Send Message Methods GetSerializedSendMessage and GetSendMessage/ModifySendMessage are the available ...
Definition: interceptor_common.h:82
void Proceed() override
Signal that the interceptor is done intercepting the current batch of the RPC.
Definition: interceptor_common.h:55
void ModifySendStatus(const Status &status) override
Overwrites the status with status.
Definition: interceptor_common.h:112
void ModifySendMessage(const void *) override
Overwrites the message to be sent with message.
Definition: interceptor_common.h:468
Status GetSendStatus() override
Returns the status to be sent. Valid for PRE_SEND_STATUS interceptions.
Definition: interceptor_common.h:107
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:50
grpc_status_code
Definition: status.h:26
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
void ModifySendMessage(const void *message) override
Overwrites the message to be sent with message.
Definition: interceptor_common.h:96
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:482
std::multimap< grpc::string, grpc::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:496
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:39
void FailHijackedRecvMessage() override
On a hijacked RPC, an interceptor can decide to fail a PRE_RECV_MESSAGE op.
Definition: interceptor_common.h:541
void ClearState()
Definition: interceptor_common.h:202
Straightforward wrapping of the C call object.
Definition: call.h:38
std::multimap< grpc::string, grpc::string > * GetSendTrailingMetadata() override
Returns a modifiable multimap of the trailing metadata to be sent.
Definition: interceptor_common.h:118
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
std::multimap< grpc::string, grpc::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:103
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:101
std::unique_ptr< ChannelInterface > GetInterceptedChannel() override
Gets an intercepted channel.
Definition: interceptor_common.h:534
Did it work? If it didn't, why?
Definition: status.h:31
~InterceptorBatchMethodsImpl()
Definition: interceptor_common.h:48
void * GetRecvMessage() override
Returns a pointer to the modifiable received message.
Definition: interceptor_common.h:504
bool RunInterceptors(std::function< void(void)> f)
Definition: interceptor_common.h:272
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:460
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:34
A sequence of bytes.
Definition: byte_buffer.h:67
void SetReverse()
Definition: interceptor_common.h:209
void SetRecvStatus(Status *status)
Definition: interceptor_common.h:178
void SetSendStatus(grpc_status_code *code, grpc::string *error_details, grpc::string *error_message)
Definition: interceptor_common.h:157
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:511
std::multimap< grpc::string, grpc::string > * GetSendInitialMetadata() override
Returns a modifiable multimap of the initial metadata to be sent.
Definition: interceptor_common.h:475
void SetSendTrailingMetadata(std::multimap< grpc::string, grpc::string > *metadata)
Definition: interceptor_common.h:164
void ModifySendStatus(const Status &) override
Overwrites the status with status.
Definition: interceptor_common.h:489
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:63
An InterceptedChannel is available to client Interceptors.
Definition: intercepted_channel.h:38
ByteBuffer * GetSerializedSendMessage() override
Send Message Methods GetSerializedSendMessage and GetSendMessage/ModifySendMessage are the available ...
Definition: interceptor_common.h:445
const void * GetSendMessage() override
Returns a non-modifiable pointer to the non-serialized form of the message to be sent.
Definition: interceptor_common.h:91
::google::protobuf::util::Status Status
Definition: config_protobuf.h:90
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:110
Status * GetRecvStatus() override
Returns a modifiable view of the received status on PRE_RECV_STATUS and POST_RECV_STATUS interception...
Definition: interceptor_common.h:519
void Hijack() override
Indicate that the interceptor has hijacked the RPC (only valid if the batch contains send_initial_met...
Definition: interceptor_common.h:438
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:81
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
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
grpc::string error_message() const
Return the instance's error message.
Definition: status.h:112
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:93
std::string string
Definition: config.h:35
void Proceed() override
Signal that the interceptor is done intercepting the current batch of the RPC.
Definition: interceptor_common.h:433
bool GetSendMessageStatus() override
Checks whether the SEND MESSAGE op succeeded.
Definition: interceptor_common.h:452
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
bool RunInterceptors()
Definition: interceptor_common.h:247
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:547
void SetSendInitialMetadata(std::multimap< grpc::string, grpc::string > *metadata)
Definition: interceptor_common.h:152
Definition: interceptor_common.h:421
void SetRecvTrailingMetadata(MetadataMap *map)
Definition: interceptor_common.h:180
bool QueryInterceptionHookPoint(experimental::InterceptionHookPoints type) override
Determine whether the current batch has an interception hook point of type type.
Definition: interceptor_common.h:424
InterceptionHookPoints
An enumeration of different possible points at which the Intercept method of the Interceptor interfac...
Definition: interceptor.h:54
Definition: interceptor_common.h:36
void AddInterceptionHookPoint(experimental::InterceptionHookPoints type)
Definition: interceptor_common.h:78
experimental::ClientRpcInfo * client_rpc_info() const
Definition: call.h:77
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:526