Craig Tiller | af69180 | 2015-06-23 14:57:07 -0700 | [diff] [blame] | 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/client_config/subchannel.h" |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 35 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | #include "src/core/channel/channel_args.h" |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 41 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 42 | #include "src/core/channel/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | |
| 44 | typedef struct { |
| 45 | gpr_refcount refs; |
| 46 | grpc_subchannel *subchannel; |
| 47 | } connection; |
| 48 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 49 | typedef struct waiting_for_connect { |
| 50 | struct waiting_for_connect *next; |
| 51 | grpc_iomgr_closure *notify; |
| 52 | grpc_transport_stream_op *initial_op; |
| 53 | grpc_subchannel_call **target; |
| 54 | } waiting_for_connect; |
| 55 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 56 | struct grpc_subchannel { |
| 57 | gpr_refcount refs; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 58 | grpc_connector *connector; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 59 | |
| 60 | /** non-transport related channel filters */ |
| 61 | const grpc_channel_filter **filters; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 62 | size_t num_filters; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 63 | /** channel arguments */ |
| 64 | grpc_channel_args *args; |
| 65 | /** address to connect to */ |
| 66 | struct sockaddr *addr; |
| 67 | size_t addr_len; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 68 | /** metadata context */ |
| 69 | grpc_mdctx *mdctx; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 70 | |
| 71 | /** set during connection */ |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 72 | grpc_connect_out_args connecting_result; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 73 | |
| 74 | /** callback for connection finishing */ |
| 75 | grpc_iomgr_closure connected; |
| 76 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 77 | /** pollset_set tracking who's interested in a connection |
| 78 | being setup */ |
| 79 | grpc_pollset_set pollset_set; |
| 80 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 81 | /** mutex protecting remaining elements */ |
| 82 | gpr_mu mu; |
| 83 | |
| 84 | /** active connection */ |
| 85 | connection *active; |
| 86 | /** are we connecting */ |
| 87 | int connecting; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 88 | /** things waiting for a connection */ |
| 89 | waiting_for_connect *waiting; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 90 | /** connectivity state tracking */ |
| 91 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct grpc_subchannel_call { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 95 | connection *connection; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 96 | gpr_refcount refs; |
| 97 | }; |
| 98 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 99 | #define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1)) |
| 100 | #define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1)) |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 101 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 102 | static grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 103 | static void connectivity_state_changed_locked(grpc_subchannel *c); |
| 104 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c); |
| 105 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 106 | static void subchannel_connected(void *subchannel, int iomgr_success); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 107 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 108 | /* |
| 109 | * grpc_subchannel implementation |
| 110 | */ |
| 111 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 112 | void grpc_subchannel_ref(grpc_subchannel *c) { gpr_ref(&c->refs); } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 113 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 114 | void grpc_subchannel_unref(grpc_subchannel *c) { |
| 115 | if (gpr_unref(&c->refs)) { |
| 116 | gpr_free(c->filters); |
| 117 | grpc_channel_args_destroy(c->args); |
| 118 | gpr_free(c->addr); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 119 | grpc_mdctx_unref(c->mdctx); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 120 | grpc_pollset_set_destroy(&c->pollset_set); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 121 | grpc_connectivity_state_destroy(&c->state_tracker); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 122 | gpr_free(c); |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 126 | void grpc_subchannel_add_interested_party(grpc_subchannel *c, |
| 127 | grpc_pollset *pollset) { |
| 128 | grpc_pollset_set_add_pollset(&c->pollset_set, pollset); |
| 129 | } |
| 130 | |
| 131 | void grpc_subchannel_del_interested_party(grpc_subchannel *c, |
| 132 | grpc_pollset *pollset) { |
| 133 | grpc_pollset_set_del_pollset(&c->pollset_set, pollset); |
| 134 | } |
| 135 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 136 | grpc_subchannel *grpc_subchannel_create(grpc_connector *connector, |
| 137 | grpc_subchannel_args *args) { |
| 138 | grpc_subchannel *c = gpr_malloc(sizeof(*c)); |
| 139 | memset(c, 0, sizeof(*c)); |
| 140 | gpr_ref_init(&c->refs, 1); |
| 141 | c->connector = connector; |
| 142 | grpc_connector_ref(c->connector); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 143 | c->num_filters = args->filter_count; |
| 144 | c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 145 | memcpy(c->filters, args->filters, |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 146 | sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 147 | c->addr = gpr_malloc(args->addr_len); |
| 148 | memcpy(c->addr, args->addr, args->addr_len); |
| 149 | c->addr_len = args->addr_len; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 150 | c->args = grpc_channel_args_copy(args->args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 151 | c->mdctx = args->mdctx; |
| 152 | grpc_mdctx_ref(c->mdctx); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 153 | grpc_pollset_set_init(&c->pollset_set); |
| 154 | grpc_iomgr_closure_init(&c->connected, subchannel_connected, c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 155 | grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 156 | gpr_mu_init(&c->mu); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 157 | return c; |
| 158 | } |
| 159 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 160 | static void start_connect(grpc_subchannel *c) { |
| 161 | grpc_connect_in_args args; |
| 162 | |
| 163 | args.interested_parties = &c->pollset_set; |
| 164 | args.addr = c->addr; |
| 165 | args.addr_len = c->addr_len; |
| 166 | args.deadline = compute_connect_deadline(c); |
| 167 | args.channel_args = c->args; |
| 168 | args.metadata_context = c->mdctx; |
| 169 | |
| 170 | grpc_connector_connect(c->connector, &args, &c->connecting_result, &c->connected); |
| 171 | } |
| 172 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 173 | void grpc_subchannel_create_call(grpc_subchannel *c, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 174 | grpc_transport_stream_op *initial_op, |
| 175 | grpc_subchannel_call **target, |
| 176 | grpc_iomgr_closure *notify) { |
| 177 | connection *con; |
| 178 | gpr_mu_lock(&c->mu); |
| 179 | if (c->active != NULL) { |
| 180 | con = c->active; |
| 181 | gpr_ref(&con->refs); |
| 182 | gpr_mu_unlock(&c->mu); |
| 183 | |
| 184 | *target = create_call(con, initial_op); |
| 185 | notify->cb(notify->cb_arg, 1); |
| 186 | } else { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 187 | waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c)); |
| 188 | w4c->next = c->waiting; |
| 189 | w4c->notify = notify; |
| 190 | w4c->initial_op = initial_op; |
| 191 | w4c->target = target; |
| 192 | c->waiting = w4c; |
| 193 | grpc_subchannel_add_interested_party(c, initial_op->bind_pollset); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 194 | if (!c->connecting) { |
| 195 | c->connecting = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 196 | connectivity_state_changed_locked(c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 197 | grpc_subchannel_ref(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 198 | gpr_mu_unlock(&c->mu); |
| 199 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 200 | start_connect(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 201 | } else { |
| 202 | gpr_mu_unlock(&c->mu); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 207 | grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) { |
| 208 | grpc_connectivity_state state; |
| 209 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 210 | state = grpc_connectivity_state_check(&c->state_tracker); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 211 | gpr_mu_unlock(&c->mu); |
| 212 | return state; |
| 213 | } |
| 214 | |
| 215 | void grpc_subchannel_notify_on_state_change(grpc_subchannel *c, |
| 216 | grpc_connectivity_state *state, |
| 217 | grpc_iomgr_closure *notify) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 218 | int do_connect = 0; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 219 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 220 | if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state, notify)) { |
| 221 | do_connect = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 222 | c->connecting = 1; |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 223 | grpc_subchannel_ref(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 224 | grpc_connectivity_state_set(&c->state_tracker, compute_connectivity_locked(c)); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 225 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 226 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 227 | if (do_connect) { |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 228 | start_connect(c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 232 | void grpc_subchannel_process_transport_op(grpc_subchannel *c, grpc_transport_op *op) { |
| 233 | abort(); |
| 234 | } |
| 235 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 236 | static void publish_transport(grpc_subchannel *c) { |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 237 | size_t channel_stack_size; |
| 238 | connection *con; |
| 239 | grpc_channel_stack *stk; |
| 240 | size_t num_filters; |
| 241 | const grpc_channel_filter **filters; |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 242 | waiting_for_connect *w4c; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 243 | |
| 244 | num_filters = c->num_filters + c->connecting_result.num_filters + 1; |
| 245 | filters = gpr_malloc(sizeof(*filters) * num_filters); |
| 246 | memcpy(filters, c->filters, sizeof(*filters) * c->num_filters); |
| 247 | memcpy(filters + c->num_filters, c->connecting_result.filters, sizeof(*filters) * c->connecting_result.num_filters); |
| 248 | filters[num_filters - 1] = &grpc_connected_channel_filter; |
| 249 | |
| 250 | channel_stack_size = grpc_channel_stack_size(filters, num_filters); |
| 251 | con = gpr_malloc(sizeof(connection) + channel_stack_size); |
| 252 | stk = (grpc_channel_stack *)(con + 1); |
| 253 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 254 | gpr_ref_init(&con->refs, 1); |
| 255 | con->subchannel = c; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 256 | grpc_channel_stack_init(filters, num_filters, c->args, c->mdctx, stk); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 257 | grpc_connected_channel_bind_transport(stk, c->connecting_result.transport); |
| 258 | memset(&c->connecting_result, 0, sizeof(c->connecting_result)); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 259 | |
| 260 | gpr_mu_lock(&c->mu); |
| 261 | GPR_ASSERT(c->active == NULL); |
| 262 | c->active = con; |
| 263 | c->connecting = 0; |
| 264 | connectivity_state_changed_locked(c); |
| 265 | while ((w4c = c->waiting)) { |
| 266 | abort(); /* not implemented */ |
| 267 | } |
| 268 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 269 | |
| 270 | gpr_free(filters); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void subchannel_connected(void *arg, int iomgr_success) { |
| 274 | grpc_subchannel *c = arg; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 275 | if (c->connecting_result.transport) { |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 276 | publish_transport(c); |
| 277 | } else { |
| 278 | grpc_subchannel_unref(c); |
| 279 | /* TODO(ctiller): retry after sleeping */ |
| 280 | abort(); |
| 281 | } |
| 282 | } |
| 283 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 284 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c) { |
| 285 | return gpr_time_add(gpr_now(), gpr_time_from_seconds(60)); |
| 286 | } |
| 287 | |
| 288 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) { |
| 289 | if (c->connecting) { |
| 290 | return GRPC_CHANNEL_CONNECTING; |
| 291 | } |
| 292 | if (c->active) { |
| 293 | return GRPC_CHANNEL_READY; |
| 294 | } |
| 295 | return GRPC_CHANNEL_IDLE; |
| 296 | } |
| 297 | |
| 298 | static void connectivity_state_changed_locked(grpc_subchannel *c) { |
| 299 | grpc_connectivity_state current = compute_connectivity_locked(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame^] | 300 | grpc_connectivity_state_set(&c->state_tracker, current); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 303 | /* |
| 304 | * grpc_subchannel_call implementation |
| 305 | */ |
| 306 | |
| 307 | void grpc_subchannel_call_ref(grpc_subchannel_call *call) { |
| 308 | gpr_ref(&call->refs); |
| 309 | } |
| 310 | |
| 311 | void grpc_subchannel_call_unref(grpc_subchannel_call *call) { |
| 312 | if (gpr_unref(&call->refs)) { |
| 313 | grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(call)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 314 | if (gpr_unref(&call->connection->refs)) { |
| 315 | gpr_free(call->connection); |
| 316 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 317 | gpr_free(call); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void grpc_subchannel_call_process_op(grpc_subchannel_call *call, |
| 322 | grpc_transport_stream_op *op) { |
| 323 | grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 324 | grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0); |
| 325 | top_elem->filter->start_transport_stream_op(top_elem, op); |
| 326 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 327 | |
| 328 | grpc_subchannel_call *create_call(connection *con, grpc_transport_stream_op *initial_op) { |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 329 | grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con); |
| 330 | grpc_subchannel_call *call = gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size); |
| 331 | grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 332 | call->connection = con; |
| 333 | gpr_ref_init(&call->refs, 1); |
| 334 | grpc_call_stack_init(chanstk, NULL, initial_op, callstk); |
| 335 | return call; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 336 | } |