// // // Copyright 2016 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // #ifndef GRPC_SRC_CPP_COMMON_CHANNEL_FILTER_H #define GRPC_SRC_CPP_COMMON_CHANNEL_FILTER_H #include <stddef.h> #include <functional> #include <new> #include <string> #include <utility> #include "absl/status/status.h" #include "absl/types/optional.h" #include <grpc/grpc.h> #include <grpcpp/support/config.h> #include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channel_fwd.h" #include "src/core/lib/channel/channel_stack.h" #include "src/core/lib/channel/context.h" #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/polling_entity.h" #include "src/core/lib/slice/slice_buffer.h" #include "src/core/lib/surface/channel_stack_type.h" #include "src/core/lib/transport/metadata_batch.h" #include "src/core/lib/transport/transport.h" /// An interface to define filters. /// /// To define a filter, implement a subclass of each of \c CallData and /// \c ChannelData. Then register the filter using something like this: /// \code{.cpp} /// RegisterChannelFilter<MyChannelDataSubclass, MyCallDataSubclass>( /// "name-of-filter", GRPC_SERVER_CHANNEL, INT_MAX, nullptr); /// \endcode namespace grpc { /// A C++ wrapper for the \c grpc_metadata_batch struct. class MetadataBatch { … }; /// A C++ wrapper for the \c grpc_transport_op struct. class TransportOp { … }; /// A C++ wrapper for the \c grpc_transport_stream_op_batch struct. class TransportStreamOpBatch { … }; /// Represents channel data. class ChannelData { … }; /// Represents call data. class CallData { … }; namespace internal { // Defines static members for passing to C core. // Members of this class correspond to the members of the C // grpc_channel_filter struct. template <typename ChannelDataType, typename CallDataType> class ChannelFilter final { … }; void RegisterChannelFilter( grpc_channel_stack_type stack_type, int priority, std::function<bool(const grpc_core::ChannelArgs&)> include_filter, const grpc_channel_filter* filter); } // namespace internal /// Registers a new filter. /// Must be called by only one thread at a time. /// The \a include_filter argument specifies a function that will be called /// to determine at run-time whether or not to add the filter. If the /// value is nullptr, the filter will be added unconditionally. /// If the channel stack type is GRPC_CLIENT_SUBCHANNEL, the caller should /// ensure that subchannels with different filter lists will always have /// different channel args. This requires setting a channel arg in case the /// registration function relies on some condition other than channel args to /// decide whether to add a filter or not. template <typename ChannelDataType, typename CallDataType> void RegisterChannelFilter( const char* name, grpc_channel_stack_type stack_type, int priority, std::function<bool(const grpc_core::ChannelArgs&)> include_filter) { … } } // namespace grpc #endif // GRPC_SRC_CPP_COMMON_CHANNEL_FILTER_H