blob: a7b3c2c0dac85933345005249eb893aec561513a [file] [log] [blame]
Mark D. Rothec0bc8b2016-06-15 14:02:57 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Mark D. Roth07cd9c92016-06-27 10:14:38 -070034#include <string.h>
35
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070036#include "src/core/lib/channel/channel_stack.h"
Mark D. Rothab950ee2016-06-29 14:51:53 -070037#include "src/cpp/common/channel_filter.h"
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070038
Craig Tiller7c70b6c2017-01-23 07:48:42 -080039#include <grpc++/impl/codegen/slice.h>
40
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070041namespace grpc {
42
Mark D. Roth07cd9c92016-06-27 10:14:38 -070043// MetadataBatch
Mark D. Roth07cd9c92016-06-27 10:14:38 -070044
Craig Tillerc5866662016-11-16 15:25:00 -080045grpc_linked_mdelem *MetadataBatch::AddMetadata(grpc_exec_ctx *exec_ctx,
46 const string &key,
Mark D. Rothbd3e3182016-06-29 08:01:55 -070047 const string &value) {
Mark D. Roth07cd9c92016-06-27 10:14:38 -070048 grpc_linked_mdelem *storage = new grpc_linked_mdelem;
49 memset(storage, 0, sizeof(grpc_linked_mdelem));
Craig Tiller7c70b6c2017-01-23 07:48:42 -080050 storage->md = grpc_mdelem_from_slices(exec_ctx, SliceFromCopiedString(key),
51 SliceFromCopiedString(value));
52 GRPC_LOG_IF_ERROR("MetadataBatch::AddMetadata",
53 grpc_metadata_batch_link_head(exec_ctx, batch_, storage));
Mark D. Roth07cd9c92016-06-27 10:14:38 -070054 return storage;
55}
56
Mark D. Roth9fe5ef52016-06-16 14:26:04 -070057// ChannelData
Mark D. Roth9fe5ef52016-06-16 14:26:04 -070058
59void ChannelData::StartTransportOp(grpc_exec_ctx *exec_ctx,
60 grpc_channel_element *elem,
Mark D. Roth07cd9c92016-06-27 10:14:38 -070061 TransportOp *op) {
62 grpc_channel_next_op(exec_ctx, elem, op->op());
Mark D. Roth9fe5ef52016-06-16 14:26:04 -070063}
64
Mark D. Roth0e44a252016-11-07 11:26:05 -080065void ChannelData::GetInfo(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
66 const grpc_channel_info *channel_info) {
67 grpc_channel_next_get_info(exec_ctx, elem, channel_info);
68}
69
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070070// CallData
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070071
Craig Tiller7a8232d2017-04-03 10:59:42 -070072void CallData::StartTransportStreamOpBatch(grpc_exec_ctx *exec_ctx,
73 grpc_call_element *elem,
74 TransportStreamOpBatch *op) {
Mark D. Roth07cd9c92016-06-27 10:14:38 -070075 grpc_call_next_op(exec_ctx, elem, op->op());
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070076}
77
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -070078void CallData::SetPollsetOrPollsetSet(grpc_exec_ctx *exec_ctx,
79 grpc_call_element *elem,
80 grpc_polling_entity *pollent) {
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070081 grpc_call_stack_ignore_set_pollset_or_pollset_set(exec_ctx, elem, pollent);
82}
83
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -070084char *CallData::GetPeer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070085 return grpc_call_next_get_peer(exec_ctx, elem);
86}
87
Mark D. Rothf9d8e902016-07-28 09:31:56 -070088// internal code used by RegisterChannelFilter()
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070089
90namespace internal {
91
Mark D. Rothf5bbff92016-06-16 12:56:07 -070092// Note: Implicitly initialized to nullptr due to static lifetime.
93std::vector<FilterRecord> *channel_filters;
Mark D. Rothec0bc8b2016-06-15 14:02:57 -070094
95namespace {
96
Craig Tillerc5866662016-11-16 15:25:00 -080097bool MaybeAddFilter(grpc_exec_ctx *exec_ctx,
98 grpc_channel_stack_builder *builder, void *arg) {
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -070099 const FilterRecord &filter = *(FilterRecord *)arg;
Mark D. Rothf5bbff92016-06-16 12:56:07 -0700100 if (filter.include_filter) {
Mark D. Rothc459ecf2016-06-16 09:17:49 -0700101 const grpc_channel_args *args =
102 grpc_channel_stack_builder_get_channel_arguments(builder);
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -0700103 if (!filter.include_filter(*args)) return true;
Mark D. Rothc459ecf2016-06-16 09:17:49 -0700104 }
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -0700105 return grpc_channel_stack_builder_prepend_filter(builder, &filter.filter,
106 nullptr, nullptr);
Mark D. Rothec0bc8b2016-06-15 14:02:57 -0700107}
108
109} // namespace
110
111void ChannelFilterPluginInit() {
112 for (size_t i = 0; i < channel_filters->size(); ++i) {
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -0700113 FilterRecord &filter = (*channel_filters)[i];
Mark D. Rothec0bc8b2016-06-15 14:02:57 -0700114 grpc_channel_init_register_stage(filter.stack_type, filter.priority,
Mark D. Rothf9c1f7a2016-06-16 10:57:28 -0700115 MaybeAddFilter, (void *)&filter);
Mark D. Rothec0bc8b2016-06-15 14:02:57 -0700116 }
117}
118
Mark D. Rothc459ecf2016-06-16 09:17:49 -0700119void ChannelFilterPluginShutdown() {}
120
Mark D. Rothec0bc8b2016-06-15 14:02:57 -0700121} // namespace internal
122
123} // namespace grpc