Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 <grpc/grpc.h> |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 40 | #include <grpc/support/slice.h> |
| 41 | #include <grpc/support/slice_buffer.h> |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 42 | |
Craig Tiller | 4a6dad3 | 2016-04-01 13:51:14 -0700 | [diff] [blame] | 43 | #include "src/core/ext/client_config/client_channel.h" |
| 44 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | d0bcc43 | 2016-03-31 14:30:51 -0700 | [diff] [blame] | 45 | #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 46 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/channel/compress_filter.h" |
| 48 | #include "src/core/lib/channel/http_client_filter.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 49 | #include "src/core/lib/iomgr/tcp_client.h" |
| 50 | #include "src/core/lib/surface/api_trace.h" |
| 51 | #include "src/core/lib/surface/channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | typedef struct { |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 54 | grpc_connector base; |
| 55 | gpr_refcount refs; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 56 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 57 | grpc_closure *notify; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 58 | grpc_connect_in_args args; |
| 59 | grpc_connect_out_args *result; |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 60 | grpc_closure initial_string_sent; |
| 61 | gpr_slice_buffer initial_string_buffer; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 62 | |
| 63 | grpc_endpoint *tcp; |
| 64 | |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 65 | grpc_closure connected; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 66 | } connector; |
| 67 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | static void connector_ref(grpc_connector *con) { |
| 69 | connector *c = (connector *)con; |
| 70 | gpr_ref(&c->refs); |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 73 | static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) { |
| 74 | connector *c = (connector *)con; |
| 75 | if (gpr_unref(&c->refs)) { |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 76 | /* c->initial_string_buffer does not need to be destroyed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | gpr_free(c); |
| 78 | } |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 79 | } |
| 80 | |
yang-g | f8843fb | 2015-11-21 12:33:22 -0800 | [diff] [blame] | 81 | static void on_initial_connect_string_sent(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 82 | bool success) { |
yang-g | f8843fb | 2015-11-21 12:33:22 -0800 | [diff] [blame] | 83 | connector_unref(exec_ctx, arg); |
| 84 | } |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 85 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 86 | static void connected(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 87 | connector *c = arg; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 88 | grpc_closure *notify; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 89 | grpc_endpoint *tcp = c->tcp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | if (tcp != NULL) { |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 91 | if (!GPR_SLICE_IS_EMPTY(c->args.initial_connect_string)) { |
yang-g | f8843fb | 2015-11-21 12:33:22 -0800 | [diff] [blame] | 92 | grpc_closure_init(&c->initial_string_sent, on_initial_connect_string_sent, |
| 93 | c); |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 94 | gpr_slice_buffer_init(&c->initial_string_buffer); |
| 95 | gpr_slice_buffer_add(&c->initial_string_buffer, |
| 96 | c->args.initial_connect_string); |
yang-g | f8843fb | 2015-11-21 12:33:22 -0800 | [diff] [blame] | 97 | connector_ref(arg); |
yang-g | 074c6fc | 2015-11-05 23:14:07 -0800 | [diff] [blame] | 98 | grpc_endpoint_write(exec_ctx, tcp, &c->initial_string_buffer, |
| 99 | &c->initial_string_sent); |
| 100 | } |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 101 | c->result->transport = |
| 102 | grpc_create_chttp2_transport(exec_ctx, c->args.channel_args, tcp, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL, |
| 104 | 0); |
| 105 | GPR_ASSERT(c->result->transport); |
Julien Boeuf | 5b19403 | 2015-12-17 16:00:51 -0800 | [diff] [blame] | 106 | c->result->channel_args = c->args.channel_args; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | } else { |
| 108 | memset(c->result, 0, sizeof(*c->result)); |
| 109 | } |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 110 | notify = c->notify; |
| 111 | c->notify = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | notify->cb(exec_ctx, notify->cb_arg, 1); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {} |
Craig Tiller | 131f6ed | 2015-09-15 08:20:20 -0700 | [diff] [blame] | 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con, |
| 118 | const grpc_connect_in_args *args, |
| 119 | grpc_connect_out_args *result, |
| 120 | grpc_closure *notify) { |
| 121 | connector *c = (connector *)con; |
| 122 | GPR_ASSERT(c->notify == NULL); |
| 123 | GPR_ASSERT(notify->cb); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 124 | c->notify = notify; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 125 | c->args = *args; |
| 126 | c->result = result; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 127 | c->tcp = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | grpc_closure_init(&c->connected, connected, c); |
| 129 | grpc_tcp_client_connect(exec_ctx, &c->connected, &c->tcp, |
| 130 | args->interested_parties, args->addr, args->addr_len, |
| 131 | args->deadline); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 134 | static const grpc_connector_vtable connector_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | connector_ref, connector_unref, connector_shutdown, connector_connect}; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | typedef struct { |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 138 | grpc_client_channel_factory base; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 139 | gpr_refcount refs; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 140 | grpc_channel_args *merge_args; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 141 | grpc_channel *master; |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 142 | } client_channel_factory; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 143 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 144 | static void client_channel_factory_ref( |
| 145 | grpc_client_channel_factory *cc_factory) { |
| 146 | client_channel_factory *f = (client_channel_factory *)cc_factory; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | gpr_ref(&f->refs); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 150 | static void client_channel_factory_unref( |
| 151 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory) { |
| 152 | client_channel_factory *f = (client_channel_factory *)cc_factory; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 153 | if (gpr_unref(&f->refs)) { |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 154 | if (f->master != NULL) { |
| 155 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, |
| 156 | "client_channel_factory"); |
| 157 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | grpc_channel_args_destroy(f->merge_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | gpr_free(f); |
| 160 | } |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 161 | } |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 162 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 163 | static grpc_subchannel *client_channel_factory_create_subchannel( |
| 164 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | grpc_subchannel_args *args) { |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 166 | client_channel_factory *f = (client_channel_factory *)cc_factory; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | connector *c = gpr_malloc(sizeof(*c)); |
| 168 | grpc_channel_args *final_args = |
| 169 | grpc_channel_args_merge(args->args, f->merge_args); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 170 | grpc_subchannel *s; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | memset(c, 0, sizeof(*c)); |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 172 | c->base.vtable = &connector_vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | gpr_ref_init(&c->refs, 1); |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 174 | args->args = final_args; |
Craig Tiller | 8cdba66 | 2016-01-22 20:01:55 -0800 | [diff] [blame] | 175 | s = grpc_subchannel_create(exec_ctx, &c->base, args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | grpc_connector_unref(exec_ctx, &c->base); |
| 177 | grpc_channel_args_destroy(final_args); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 178 | return s; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 181 | static grpc_channel *client_channel_factory_create_channel( |
| 182 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory, |
David Garcia Quintas | 0b868c7 | 2016-04-01 14:28:22 -0700 | [diff] [blame] | 183 | const char *target, grpc_client_channel_type type, |
| 184 | grpc_channel_args *args) { |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 185 | client_channel_factory *f = (client_channel_factory *)cc_factory; |
| 186 | grpc_channel_args *final_args = grpc_channel_args_merge(args, f->merge_args); |
| 187 | grpc_channel *channel = grpc_channel_create(exec_ctx, target, final_args, |
| 188 | GRPC_CLIENT_CHANNEL, NULL); |
| 189 | grpc_channel_args_destroy(final_args); |
| 190 | grpc_resolver *resolver = grpc_resolver_create(target, &f->base); |
| 191 | if (!resolver) { |
| 192 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, |
| 193 | "client_channel_factory_create_channel"); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | grpc_client_channel_set_resolver( |
| 198 | exec_ctx, grpc_channel_get_channel_stack(channel), resolver); |
| 199 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "create_channel"); |
| 200 | |
| 201 | return channel; |
| 202 | } |
| 203 | |
| 204 | static const grpc_client_channel_factory_vtable client_channel_factory_vtable = |
| 205 | {client_channel_factory_ref, client_channel_factory_unref, |
| 206 | client_channel_factory_create_subchannel, |
| 207 | client_channel_factory_create_channel}; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 208 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | /* Create a client channel: |
| 210 | Asynchronously: - resolve target |
| 211 | - connect to it (trying alternatives as presented) |
| 212 | - perform handshakes */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | grpc_channel *grpc_insecure_channel_create(const char *target, |
| 214 | const grpc_channel_args *args, |
| 215 | void *reserved) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 216 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 217 | GRPC_API_TRACE( |
| 218 | "grpc_insecure_channel_create(target=%p, args=%p, reserved=%p)", 3, |
| 219 | (target, args, reserved)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | GPR_ASSERT(!reserved); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 222 | client_channel_factory *f = gpr_malloc(sizeof(*f)); |
| 223 | memset(f, 0, sizeof(*f)); |
| 224 | f->base.vtable = &client_channel_factory_vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 225 | gpr_ref_init(&f->refs, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | f->merge_args = grpc_channel_args_copy(args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | |
David Garcia Quintas | 0b868c7 | 2016-04-01 14:28:22 -0700 | [diff] [blame] | 228 | grpc_channel *channel = client_channel_factory_create_channel( |
| 229 | &exec_ctx, &f->base, target, GRPC_CLIENT_CHANNEL_TYPE_REGULAR, NULL); |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 230 | if (channel != NULL) { |
| 231 | f->master = channel; |
| 232 | GRPC_CHANNEL_INTERNAL_REF(f->master, "grpc_insecure_channel_create"); |
| 233 | } |
| 234 | grpc_client_channel_factory_unref(&exec_ctx, &f->base); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 235 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 237 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 238 | return channel; /* may be NULL */ |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 239 | } |