OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, 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 |
| 34 #include "src/core/channel/connected_channel.h" |
| 35 |
| 36 #include <stdarg.h> |
| 37 #include <stdio.h> |
| 38 #include <string.h> |
| 39 |
| 40 #include "src/core/support/string.h" |
| 41 #include "src/core/transport/transport.h" |
| 42 #include "src/core/profiling/timers.h" |
| 43 #include <grpc/byte_buffer.h> |
| 44 #include <grpc/support/alloc.h> |
| 45 #include <grpc/support/log.h> |
| 46 #include <grpc/support/slice_buffer.h> |
| 47 |
| 48 #define MAX_BUFFER_LENGTH 8192 |
| 49 |
| 50 typedef struct connected_channel_channel_data { |
| 51 grpc_transport *transport; |
| 52 } channel_data; |
| 53 |
| 54 typedef struct connected_channel_call_data { void *unused; } call_data; |
| 55 |
| 56 /* We perform a small hack to locate transport data alongside the connected |
| 57 channel data in call allocations, to allow everything to be pulled in minimal |
| 58 cache line requests */ |
| 59 #define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld) + 1)) |
| 60 #define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \ |
| 61 (((call_data *)(transport_stream)) - 1) |
| 62 |
| 63 /* Intercept a call operation and either push it directly up or translate it |
| 64 into transport stream operations */ |
| 65 static void con_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 66 grpc_call_element *elem, |
| 67 grpc_transport_stream_op *op) { |
| 68 call_data *calld = elem->call_data; |
| 69 channel_data *chand = elem->channel_data; |
| 70 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 71 GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 72 |
| 73 grpc_transport_perform_stream_op(exec_ctx, chand->transport, |
| 74 TRANSPORT_STREAM_FROM_CALL_DATA(calld), op); |
| 75 } |
| 76 |
| 77 static void con_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 78 grpc_channel_element *elem, |
| 79 grpc_transport_op *op) { |
| 80 channel_data *chand = elem->channel_data; |
| 81 grpc_transport_perform_op(exec_ctx, chand->transport, op); |
| 82 } |
| 83 |
| 84 /* Constructor for call_data */ |
| 85 static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 86 grpc_call_element_args *args) { |
| 87 call_data *calld = elem->call_data; |
| 88 channel_data *chand = elem->channel_data; |
| 89 int r; |
| 90 |
| 91 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 92 r = grpc_transport_init_stream( |
| 93 exec_ctx, chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA(calld), |
| 94 &args->call_stack->refcount, args->server_transport_data); |
| 95 GPR_ASSERT(r == 0); |
| 96 } |
| 97 |
| 98 static void set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 99 grpc_pollset *pollset) { |
| 100 call_data *calld = elem->call_data; |
| 101 channel_data *chand = elem->channel_data; |
| 102 grpc_transport_set_pollset(exec_ctx, chand->transport, |
| 103 TRANSPORT_STREAM_FROM_CALL_DATA(calld), pollset); |
| 104 } |
| 105 |
| 106 /* Destructor for call_data */ |
| 107 static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 108 grpc_call_element *elem) { |
| 109 call_data *calld = elem->call_data; |
| 110 channel_data *chand = elem->channel_data; |
| 111 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 112 grpc_transport_destroy_stream(exec_ctx, chand->transport, |
| 113 TRANSPORT_STREAM_FROM_CALL_DATA(calld)); |
| 114 } |
| 115 |
| 116 /* Constructor for channel_data */ |
| 117 static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 118 grpc_channel_element *elem, |
| 119 grpc_channel_element_args *args) { |
| 120 channel_data *cd = (channel_data *)elem->channel_data; |
| 121 GPR_ASSERT(args->is_last); |
| 122 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 123 cd->transport = NULL; |
| 124 } |
| 125 |
| 126 /* Destructor for channel_data */ |
| 127 static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 128 grpc_channel_element *elem) { |
| 129 channel_data *cd = (channel_data *)elem->channel_data; |
| 130 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 131 grpc_transport_destroy(exec_ctx, cd->transport); |
| 132 } |
| 133 |
| 134 static char *con_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 135 channel_data *chand = elem->channel_data; |
| 136 return grpc_transport_get_peer(exec_ctx, chand->transport); |
| 137 } |
| 138 |
| 139 const grpc_channel_filter grpc_connected_channel_filter = { |
| 140 con_start_transport_stream_op, con_start_transport_op, sizeof(call_data), |
| 141 init_call_elem, set_pollset, destroy_call_elem, sizeof(channel_data), |
| 142 init_channel_elem, destroy_channel_elem, con_get_peer, "connected", |
| 143 }; |
| 144 |
| 145 void grpc_connected_channel_bind_transport(grpc_channel_stack *channel_stack, |
| 146 grpc_transport *transport) { |
| 147 /* Assumes that the connected channel filter is always the last filter |
| 148 in a channel stack */ |
| 149 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 150 channel_data *cd = (channel_data *)elem->channel_data; |
| 151 GPR_ASSERT(elem->filter == &grpc_connected_channel_filter); |
| 152 GPR_ASSERT(cd->transport == NULL); |
| 153 cd->transport = transport; |
| 154 |
| 155 /* HACK(ctiller): increase call stack size for the channel to make space |
| 156 for channel data. We need a cleaner (but performant) way to do this, |
| 157 and I'm not sure what that is yet. |
| 158 This is only "safe" because call stacks place no additional data after |
| 159 the last call element, and the last call element MUST be the connected |
| 160 channel. */ |
| 161 channel_stack->call_stack_size += grpc_transport_stream_size(transport); |
| 162 } |
| 163 |
| 164 grpc_stream *grpc_connected_channel_get_stream(grpc_call_element *elem) { |
| 165 call_data *calld = elem->call_data; |
| 166 return TRANSPORT_STREAM_FROM_CALL_DATA(calld); |
| 167 } |
OLD | NEW |