Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 1 | /* |
| 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. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 36 | #include "src/core/lib/channel/channel_stack.h" |
Mark D. Roth | ab950ee | 2016-06-29 14:51:53 -0700 | [diff] [blame] | 37 | #include "src/cpp/common/channel_filter.h" |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 38 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 39 | #include <grpc++/impl/codegen/slice.h> |
| 40 | |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 41 | namespace grpc { |
| 42 | |
Mark D. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 43 | // MetadataBatch |
Mark D. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 44 | |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 45 | grpc_linked_mdelem *MetadataBatch::AddMetadata(grpc_exec_ctx *exec_ctx, |
| 46 | const string &key, |
Mark D. Roth | bd3e318 | 2016-06-29 08:01:55 -0700 | [diff] [blame] | 47 | const string &value) { |
Mark D. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 48 | grpc_linked_mdelem *storage = new grpc_linked_mdelem; |
| 49 | memset(storage, 0, sizeof(grpc_linked_mdelem)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 50 | 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. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 54 | return storage; |
| 55 | } |
| 56 | |
Mark D. Roth | 9fe5ef5 | 2016-06-16 14:26:04 -0700 | [diff] [blame] | 57 | // ChannelData |
Mark D. Roth | 9fe5ef5 | 2016-06-16 14:26:04 -0700 | [diff] [blame] | 58 | |
| 59 | void ChannelData::StartTransportOp(grpc_exec_ctx *exec_ctx, |
| 60 | grpc_channel_element *elem, |
Mark D. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 61 | TransportOp *op) { |
| 62 | grpc_channel_next_op(exec_ctx, elem, op->op()); |
Mark D. Roth | 9fe5ef5 | 2016-06-16 14:26:04 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Mark D. Roth | 0e44a25 | 2016-11-07 11:26:05 -0800 | [diff] [blame] | 65 | void 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. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 70 | // CallData |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 71 | |
Craig Tiller | 7a8232d | 2017-04-03 10:59:42 -0700 | [diff] [blame] | 72 | void CallData::StartTransportStreamOpBatch(grpc_exec_ctx *exec_ctx, |
| 73 | grpc_call_element *elem, |
| 74 | TransportStreamOpBatch *op) { |
Mark D. Roth | 07cd9c9 | 2016-06-27 10:14:38 -0700 | [diff] [blame] | 75 | grpc_call_next_op(exec_ctx, elem, op->op()); |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 78 | void CallData::SetPollsetOrPollsetSet(grpc_exec_ctx *exec_ctx, |
| 79 | grpc_call_element *elem, |
| 80 | grpc_polling_entity *pollent) { |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 81 | grpc_call_stack_ignore_set_pollset_or_pollset_set(exec_ctx, elem, pollent); |
| 82 | } |
| 83 | |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 84 | char *CallData::GetPeer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 85 | return grpc_call_next_get_peer(exec_ctx, elem); |
| 86 | } |
| 87 | |
Mark D. Roth | f9d8e90 | 2016-07-28 09:31:56 -0700 | [diff] [blame] | 88 | // internal code used by RegisterChannelFilter() |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 89 | |
| 90 | namespace internal { |
| 91 | |
Mark D. Roth | f5bbff9 | 2016-06-16 12:56:07 -0700 | [diff] [blame] | 92 | // Note: Implicitly initialized to nullptr due to static lifetime. |
| 93 | std::vector<FilterRecord> *channel_filters; |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 94 | |
| 95 | namespace { |
| 96 | |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 97 | bool MaybeAddFilter(grpc_exec_ctx *exec_ctx, |
| 98 | grpc_channel_stack_builder *builder, void *arg) { |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 99 | const FilterRecord &filter = *(FilterRecord *)arg; |
Mark D. Roth | f5bbff9 | 2016-06-16 12:56:07 -0700 | [diff] [blame] | 100 | if (filter.include_filter) { |
Mark D. Roth | c459ecf | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 101 | const grpc_channel_args *args = |
| 102 | grpc_channel_stack_builder_get_channel_arguments(builder); |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 103 | if (!filter.include_filter(*args)) return true; |
Mark D. Roth | c459ecf | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 104 | } |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 105 | return grpc_channel_stack_builder_prepend_filter(builder, &filter.filter, |
| 106 | nullptr, nullptr); |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace |
| 110 | |
| 111 | void ChannelFilterPluginInit() { |
| 112 | for (size_t i = 0; i < channel_filters->size(); ++i) { |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 113 | FilterRecord &filter = (*channel_filters)[i]; |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 114 | grpc_channel_init_register_stage(filter.stack_type, filter.priority, |
Mark D. Roth | f9c1f7a | 2016-06-16 10:57:28 -0700 | [diff] [blame] | 115 | MaybeAddFilter, (void *)&filter); |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Mark D. Roth | c459ecf | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 119 | void ChannelFilterPluginShutdown() {} |
| 120 | |
Mark D. Roth | ec0bc8b | 2016-06-15 14:02:57 -0700 | [diff] [blame] | 121 | } // namespace internal |
| 122 | |
| 123 | } // namespace grpc |