Home | History | Annotate | Download | only in common
      1 /*
      2  *
      3  * Copyright 2016 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 #include <string.h>
     20 
     21 #include "src/core/lib/channel/channel_stack.h"
     22 #include "src/cpp/common/channel_filter.h"
     23 
     24 #include <grpcpp/impl/codegen/slice.h>
     25 
     26 namespace grpc {
     27 
     28 // MetadataBatch
     29 
     30 grpc_linked_mdelem* MetadataBatch::AddMetadata(const string& key,
     31                                                const string& value) {
     32   grpc_linked_mdelem* storage = new grpc_linked_mdelem;
     33   memset(storage, 0, sizeof(grpc_linked_mdelem));
     34   storage->md = grpc_mdelem_from_slices(SliceFromCopiedString(key),
     35                                         SliceFromCopiedString(value));
     36   GRPC_LOG_IF_ERROR("MetadataBatch::AddMetadata",
     37                     grpc_metadata_batch_link_head(batch_, storage));
     38   return storage;
     39 }
     40 
     41 // ChannelData
     42 
     43 void ChannelData::StartTransportOp(grpc_channel_element* elem,
     44                                    TransportOp* op) {
     45   grpc_channel_next_op(elem, op->op());
     46 }
     47 
     48 void ChannelData::GetInfo(grpc_channel_element* elem,
     49                           const grpc_channel_info* channel_info) {
     50   grpc_channel_next_get_info(elem, channel_info);
     51 }
     52 
     53 // CallData
     54 
     55 void CallData::StartTransportStreamOpBatch(grpc_call_element* elem,
     56                                            TransportStreamOpBatch* op) {
     57   grpc_call_next_op(elem, op->op());
     58 }
     59 
     60 void CallData::SetPollsetOrPollsetSet(grpc_call_element* elem,
     61                                       grpc_polling_entity* pollent) {
     62   grpc_call_stack_ignore_set_pollset_or_pollset_set(elem, pollent);
     63 }
     64 
     65 // internal code used by RegisterChannelFilter()
     66 
     67 namespace internal {
     68 
     69 // Note: Implicitly initialized to nullptr due to static lifetime.
     70 std::vector<FilterRecord>* channel_filters;
     71 
     72 namespace {
     73 
     74 bool MaybeAddFilter(grpc_channel_stack_builder* builder, void* arg) {
     75   const FilterRecord& filter = *static_cast<FilterRecord*>(arg);
     76   if (filter.include_filter) {
     77     const grpc_channel_args* args =
     78         grpc_channel_stack_builder_get_channel_arguments(builder);
     79     if (!filter.include_filter(*args)) return true;
     80   }
     81   return grpc_channel_stack_builder_prepend_filter(builder, &filter.filter,
     82                                                    nullptr, nullptr);
     83 }
     84 
     85 }  // namespace
     86 
     87 void ChannelFilterPluginInit() {
     88   for (size_t i = 0; i < channel_filters->size(); ++i) {
     89     FilterRecord& filter = (*channel_filters)[i];
     90     grpc_channel_init_register_stage(filter.stack_type, filter.priority,
     91                                      MaybeAddFilter, (void*)&filter);
     92   }
     93 }
     94 
     95 void ChannelFilterPluginShutdown() {}
     96 
     97 }  // namespace internal
     98 
     99 }  // namespace grpc
    100