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; |
| 55 | grpc_mdctx *metadata_context; |
| 56 | grpc_mdstr *grpc_status_string; |
| 57 | grpc_mdstr *grpc_message_string; |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 58 | grpc_mdstr *path_string; |
| 59 | grpc_mdstr *authority_string; |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 60 | |
| 61 | gpr_mu registered_call_mu; |
| 62 | registered_call *registered_calls; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 65 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 66 | #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \ |
| 67 | (((grpc_channel *)(channel_stack)) - 1) |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 68 | #define CHANNEL_FROM_TOP_ELEM(top_elem) \ |
| 69 | CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | |
| 71 | grpc_channel *grpc_channel_create_from_filters( |
| 72 | const grpc_channel_filter **filters, size_t num_filters, |
| 73 | const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client) { |
| 74 | size_t size = |
| 75 | sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters); |
| 76 | grpc_channel *channel = gpr_malloc(size); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 77 | GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | channel->is_client = is_client; |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 79 | /* decremented by grpc_channel_destroy, and grpc_client_channel_closed if |
| 80 | * is_client */ |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 81 | gpr_ref_init(&channel->refs, 1 + is_client); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | channel->metadata_context = mdctx; |
| 83 | channel->grpc_status_string = grpc_mdstr_from_string(mdctx, "grpc-status"); |
| 84 | channel->grpc_message_string = grpc_mdstr_from_string(mdctx, "grpc-message"); |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 85 | channel->path_string = grpc_mdstr_from_string(mdctx, ":path"); |
| 86 | channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | grpc_channel_stack_init(filters, num_filters, args, channel->metadata_context, |
| 88 | CHANNEL_STACK_FROM_CHANNEL(channel)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 89 | gpr_mu_init(&channel->registered_call_mu); |
| 90 | channel->registered_calls = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | return channel; |
| 92 | } |
| 93 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 94 | static grpc_call *grpc_channel_create_call_internal( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 95 | grpc_channel *channel, grpc_completion_queue *cq, grpc_mdelem *path_mdelem, |
| 96 | grpc_mdelem *authority_mdelem, gpr_timespec deadline) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 97 | grpc_mdelem *send_metadata[2]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 99 | GPR_ASSERT(channel->is_client); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 101 | send_metadata[0] = path_mdelem; |
| 102 | send_metadata[1] = authority_mdelem; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 104 | return grpc_call_create(channel, cq, NULL, send_metadata, |
| 105 | GPR_ARRAY_SIZE(send_metadata), deadline); |
| 106 | } |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 107 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 108 | grpc_call *grpc_channel_create_call_old(grpc_channel *channel, |
| 109 | const char *method, const char *host, |
| 110 | gpr_timespec absolute_deadline) { |
| 111 | return grpc_channel_create_call(channel, NULL, method, host, |
| 112 | absolute_deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 115 | grpc_call *grpc_channel_create_call(grpc_channel *channel, |
| 116 | grpc_completion_queue *cq, |
| 117 | const char *method, const char *host, |
| 118 | gpr_timespec deadline) { |
| 119 | return grpc_channel_create_call_internal( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 120 | channel, cq, |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 121 | grpc_mdelem_from_metadata_strings( |
| 122 | channel->metadata_context, grpc_mdstr_ref(channel->path_string), |
| 123 | grpc_mdstr_from_string(channel->metadata_context, method)), |
| 124 | grpc_mdelem_from_metadata_strings( |
| 125 | channel->metadata_context, grpc_mdstr_ref(channel->authority_string), |
| 126 | grpc_mdstr_from_string(channel->metadata_context, host)), |
| 127 | deadline); |
| 128 | } |
| 129 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 130 | void *grpc_channel_register_call(grpc_channel *channel, const char *method, |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 131 | const char *host) { |
| 132 | registered_call *rc = gpr_malloc(sizeof(registered_call)); |
| 133 | rc->path = grpc_mdelem_from_metadata_strings( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 134 | channel->metadata_context, grpc_mdstr_ref(channel->path_string), |
| 135 | grpc_mdstr_from_string(channel->metadata_context, method)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 136 | rc->authority = grpc_mdelem_from_metadata_strings( |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 137 | channel->metadata_context, grpc_mdstr_ref(channel->authority_string), |
| 138 | grpc_mdstr_from_string(channel->metadata_context, host)); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 139 | gpr_mu_lock(&channel->registered_call_mu); |
| 140 | rc->next = channel->registered_calls; |
| 141 | channel->registered_calls = rc; |
| 142 | gpr_mu_unlock(&channel->registered_call_mu); |
| 143 | return rc; |
| 144 | } |
| 145 | |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 146 | grpc_call *grpc_channel_create_registered_call( |
| 147 | grpc_channel *channel, grpc_completion_queue *completion_queue, |
| 148 | void *registered_call_handle, gpr_timespec deadline) { |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 149 | registered_call *rc = registered_call_handle; |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 150 | return grpc_channel_create_call_internal( |
| 151 | channel, completion_queue, grpc_mdelem_ref(rc->path), |
| 152 | grpc_mdelem_ref(rc->authority), deadline); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | void grpc_channel_internal_ref(grpc_channel *channel) { |
| 156 | gpr_ref(&channel->refs); |
| 157 | } |
| 158 | |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 159 | static void destroy_channel(void *p, int ok) { |
| 160 | grpc_channel *channel = p; |
| 161 | grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CHANNEL(channel)); |
| 162 | grpc_mdstr_unref(channel->grpc_status_string); |
| 163 | grpc_mdstr_unref(channel->grpc_message_string); |
| 164 | grpc_mdstr_unref(channel->path_string); |
| 165 | grpc_mdstr_unref(channel->authority_string); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 166 | while (channel->registered_calls) { |
| 167 | registered_call *rc = channel->registered_calls; |
| 168 | channel->registered_calls = rc->next; |
| 169 | grpc_mdelem_unref(rc->path); |
| 170 | grpc_mdelem_unref(rc->authority); |
| 171 | gpr_free(rc); |
| 172 | } |
Craig Tiller | 9be83ee | 2015-02-18 14:16:15 -0800 | [diff] [blame] | 173 | grpc_mdctx_unref(channel->metadata_context); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 174 | gpr_mu_destroy(&channel->registered_call_mu); |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 175 | gpr_free(channel); |
| 176 | } |
| 177 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | void grpc_channel_internal_unref(grpc_channel *channel) { |
| 179 | if (gpr_unref(&channel->refs)) { |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 180 | grpc_iomgr_add_callback(destroy_channel, channel); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void grpc_channel_destroy(grpc_channel *channel) { |
| 185 | grpc_channel_op op; |
| 186 | grpc_channel_element *elem; |
| 187 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 189 | |
| 190 | op.type = GRPC_CHANNEL_GOAWAY; |
| 191 | op.dir = GRPC_CALL_DOWN; |
| 192 | op.data.goaway.status = GRPC_STATUS_OK; |
| 193 | op.data.goaway.message = gpr_slice_from_copied_string("Client disconnect"); |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 194 | elem->filter->channel_op(elem, NULL, &op); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 195 | |
| 196 | op.type = GRPC_CHANNEL_DISCONNECT; |
| 197 | op.dir = GRPC_CALL_DOWN; |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 198 | elem->filter->channel_op(elem, NULL, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | |
| 200 | grpc_channel_internal_unref(channel); |
| 201 | } |
| 202 | |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 203 | void grpc_client_channel_closed(grpc_channel_element *elem) { |
| 204 | grpc_channel_internal_unref(CHANNEL_FROM_TOP_ELEM(elem)); |
| 205 | } |
| 206 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) { |
| 208 | return CHANNEL_STACK_FROM_CHANNEL(channel); |
| 209 | } |
| 210 | |
| 211 | grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel) { |
| 212 | return channel->metadata_context; |
| 213 | } |
| 214 | |
| 215 | grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) { |
| 216 | return channel->grpc_status_string; |
| 217 | } |
| 218 | |
| 219 | grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) { |
| 220 | return channel->grpc_message_string; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 221 | } |