Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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/surface/channel.h" |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 39 | #include "src/core/iomgr/iomgr.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/surface/call.h" |
| 41 | #include "src/core/surface/client.h" |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 42 | #include "src/core/surface/init.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
| 45 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 46 | typedef struct registered_call { |
| 47 | grpc_mdelem *path; |
| 48 | grpc_mdelem *authority; |
| 49 | struct registered_call *next; |
| 50 | } registered_call; |
| 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | struct grpc_channel { |
| 53 | int is_client; |
| 54 | gpr_refcount refs; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 55 | gpr_uint32 max_message_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | grpc_mdctx *metadata_context; |
| 57 | grpc_mdstr *grpc_status_string; |
| 58 | grpc_mdstr *grpc_message_string; |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 59 | grpc_mdstr *path_string; |
| 60 | grpc_mdstr *authority_string; |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 61 | |
| 62 | gpr_mu registered_call_mu; |
| 63 | registered_call *registered_calls; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 66 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 67 | #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \ |
| 68 | (((grpc_channel *)(channel_stack)) - 1) |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 69 | #define CHANNEL_FROM_TOP_ELEM(top_elem) \ |
| 70 | CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 72 | /* the protobuf library will (by default) start warning at 100megs */ |
| 73 | #define DEFAULT_MAX_MESSAGE_LENGTH (100 * 1024 * 1024) |
| 74 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | grpc_channel *grpc_channel_create_from_filters( |
| 76 | const grpc_channel_filter **filters, size_t num_filters, |
| 77 | const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client) { |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 78 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | size_t size = |
| 80 | sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters); |
| 81 | grpc_channel *channel = gpr_malloc(size); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 82 | GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | channel->is_client = is_client; |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 84 | /* decremented by grpc_channel_destroy, and grpc_client_channel_closed if |
| 85 | * is_client */ |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 86 | gpr_ref_init(&channel->refs, 1 + is_client); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | channel->metadata_context = mdctx; |
| 88 | channel->grpc_status_string = grpc_mdstr_from_string(mdctx, "grpc-status"); |
| 89 | channel->grpc_message_string = grpc_mdstr_from_string(mdctx, "grpc-message"); |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 90 | channel->path_string = grpc_mdstr_from_string(mdctx, ":path"); |
| 91 | channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | grpc_channel_stack_init(filters, num_filters, args, channel->metadata_context, |
| 93 | CHANNEL_STACK_FROM_CHANNEL(channel)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 94 | gpr_mu_init(&channel->registered_call_mu); |
| 95 | channel->registered_calls = NULL; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 96 | |
| 97 | channel->max_message_length = DEFAULT_MAX_MESSAGE_LENGTH; |
| 98 | if (args) { |
| 99 | for (i = 0; i < args->num_args; i++) { |
| 100 | if (0 == strcmp(args->args[i].key, GRPC_ARG_MAX_MESSAGE_LENGTH)) { |
| 101 | if (args->args[i].type != GRPC_ARG_INTEGER) { |
| 102 | gpr_log(GPR_ERROR, "%s ignored: it must be an integer", |
| 103 | GRPC_ARG_MAX_MESSAGE_LENGTH); |
| 104 | } else if (args->args[i].value.integer < 0) { |
| 105 | gpr_log(GPR_ERROR, "%s ignored: it must be >= 0", |
| 106 | GRPC_ARG_MAX_MESSAGE_LENGTH); |
| 107 | } else { |
| 108 | channel->max_message_length = args->args[i].value.integer; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | return channel; |
| 115 | } |
| 116 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 117 | static grpc_call *grpc_channel_create_call_internal( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 118 | grpc_channel *channel, grpc_completion_queue *cq, grpc_mdelem *path_mdelem, |
| 119 | grpc_mdelem *authority_mdelem, gpr_timespec deadline) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 120 | grpc_mdelem *send_metadata[2]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 122 | GPR_ASSERT(channel->is_client); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 124 | send_metadata[0] = path_mdelem; |
| 125 | send_metadata[1] = authority_mdelem; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 127 | return grpc_call_create(channel, cq, NULL, send_metadata, |
| 128 | GPR_ARRAY_SIZE(send_metadata), deadline); |
| 129 | } |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 130 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 131 | grpc_call *grpc_channel_create_call_old(grpc_channel *channel, |
| 132 | const char *method, const char *host, |
| 133 | gpr_timespec absolute_deadline) { |
| 134 | return grpc_channel_create_call(channel, NULL, method, host, |
| 135 | absolute_deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 138 | grpc_call *grpc_channel_create_call(grpc_channel *channel, |
| 139 | grpc_completion_queue *cq, |
| 140 | const char *method, const char *host, |
| 141 | gpr_timespec deadline) { |
| 142 | return grpc_channel_create_call_internal( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 143 | channel, cq, |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 144 | grpc_mdelem_from_metadata_strings( |
| 145 | channel->metadata_context, grpc_mdstr_ref(channel->path_string), |
| 146 | grpc_mdstr_from_string(channel->metadata_context, method)), |
| 147 | grpc_mdelem_from_metadata_strings( |
| 148 | channel->metadata_context, grpc_mdstr_ref(channel->authority_string), |
| 149 | grpc_mdstr_from_string(channel->metadata_context, host)), |
| 150 | deadline); |
| 151 | } |
| 152 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 153 | void *grpc_channel_register_call(grpc_channel *channel, const char *method, |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 154 | const char *host) { |
| 155 | registered_call *rc = gpr_malloc(sizeof(registered_call)); |
| 156 | rc->path = grpc_mdelem_from_metadata_strings( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 157 | channel->metadata_context, grpc_mdstr_ref(channel->path_string), |
| 158 | grpc_mdstr_from_string(channel->metadata_context, method)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 159 | rc->authority = grpc_mdelem_from_metadata_strings( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 160 | channel->metadata_context, grpc_mdstr_ref(channel->authority_string), |
| 161 | grpc_mdstr_from_string(channel->metadata_context, host)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 162 | gpr_mu_lock(&channel->registered_call_mu); |
| 163 | rc->next = channel->registered_calls; |
| 164 | channel->registered_calls = rc; |
| 165 | gpr_mu_unlock(&channel->registered_call_mu); |
| 166 | return rc; |
| 167 | } |
| 168 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 169 | grpc_call *grpc_channel_create_registered_call( |
| 170 | grpc_channel *channel, grpc_completion_queue *completion_queue, |
| 171 | void *registered_call_handle, gpr_timespec deadline) { |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 172 | registered_call *rc = registered_call_handle; |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 173 | return grpc_channel_create_call_internal( |
| 174 | channel, completion_queue, grpc_mdelem_ref(rc->path), |
| 175 | grpc_mdelem_ref(rc->authority), deadline); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | void grpc_channel_internal_ref(grpc_channel *channel) { |
| 179 | gpr_ref(&channel->refs); |
| 180 | } |
| 181 | |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 182 | static void destroy_channel(void *p, int ok) { |
| 183 | grpc_channel *channel = p; |
| 184 | grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CHANNEL(channel)); |
| 185 | grpc_mdstr_unref(channel->grpc_status_string); |
| 186 | grpc_mdstr_unref(channel->grpc_message_string); |
| 187 | grpc_mdstr_unref(channel->path_string); |
| 188 | grpc_mdstr_unref(channel->authority_string); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 189 | while (channel->registered_calls) { |
| 190 | registered_call *rc = channel->registered_calls; |
| 191 | channel->registered_calls = rc->next; |
| 192 | grpc_mdelem_unref(rc->path); |
| 193 | grpc_mdelem_unref(rc->authority); |
| 194 | gpr_free(rc); |
| 195 | } |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 196 | grpc_mdctx_unref(channel->metadata_context); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 197 | gpr_mu_destroy(&channel->registered_call_mu); |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 198 | gpr_free(channel); |
| 199 | } |
| 200 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 201 | void grpc_channel_internal_unref(grpc_channel *channel) { |
| 202 | if (gpr_unref(&channel->refs)) { |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 203 | grpc_iomgr_add_callback(destroy_channel, channel); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | void grpc_channel_destroy(grpc_channel *channel) { |
| 208 | grpc_channel_op op; |
| 209 | grpc_channel_element *elem; |
| 210 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 211 | elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 212 | |
| 213 | op.type = GRPC_CHANNEL_GOAWAY; |
| 214 | op.dir = GRPC_CALL_DOWN; |
| 215 | op.data.goaway.status = GRPC_STATUS_OK; |
| 216 | op.data.goaway.message = gpr_slice_from_copied_string("Client disconnect"); |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 217 | elem->filter->channel_op(elem, NULL, &op); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 218 | |
| 219 | op.type = GRPC_CHANNEL_DISCONNECT; |
| 220 | op.dir = GRPC_CALL_DOWN; |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 221 | elem->filter->channel_op(elem, NULL, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | |
| 223 | grpc_channel_internal_unref(channel); |
| 224 | } |
| 225 | |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 226 | void grpc_client_channel_closed(grpc_channel_element *elem) { |
| 227 | grpc_channel_internal_unref(CHANNEL_FROM_TOP_ELEM(elem)); |
| 228 | } |
| 229 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) { |
| 231 | return CHANNEL_STACK_FROM_CHANNEL(channel); |
| 232 | } |
| 233 | |
| 234 | grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel) { |
| 235 | return channel->metadata_context; |
| 236 | } |
| 237 | |
| 238 | grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) { |
| 239 | return channel->grpc_status_string; |
| 240 | } |
| 241 | |
| 242 | grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) { |
| 243 | return channel->grpc_message_string; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 244 | } |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 245 | |
| 246 | gpr_uint32 grpc_channel_get_max_message_length(grpc_channel *channel) { |
| 247 | return channel->max_message_length; |
Craig Tiller | 09b49d7 | 2015-04-22 16:40:23 -0700 | [diff] [blame] | 248 | } |