Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: third_party/grpc/src/core/surface/channel_create.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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 <grpc/grpc.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/slice.h>
41 #include <grpc/support/slice_buffer.h>
42
43 #include "src/core/census/grpc_filter.h"
44 #include "src/core/channel/channel_args.h"
45 #include "src/core/channel/client_channel.h"
46 #include "src/core/channel/compress_filter.h"
47 #include "src/core/channel/http_client_filter.h"
48 #include "src/core/client_config/resolver_registry.h"
49 #include "src/core/iomgr/tcp_client.h"
50 #include "src/core/surface/api_trace.h"
51 #include "src/core/surface/channel.h"
52 #include "src/core/transport/chttp2_transport.h"
53
54 typedef struct {
55 grpc_connector base;
56 gpr_refcount refs;
57
58 grpc_closure *notify;
59 grpc_connect_in_args args;
60 grpc_connect_out_args *result;
61 grpc_closure initial_string_sent;
62 gpr_slice_buffer initial_string_buffer;
63
64 grpc_endpoint *tcp;
65
66 grpc_closure connected;
67 } connector;
68
69 static void connector_ref(grpc_connector *con) {
70 connector *c = (connector *)con;
71 gpr_ref(&c->refs);
72 }
73
74 static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) {
75 connector *c = (connector *)con;
76 if (gpr_unref(&c->refs)) {
77 /* c->initial_string_buffer does not need to be destroyed */
78 gpr_free(c);
79 }
80 }
81
82 static void on_initial_connect_string_sent(grpc_exec_ctx *exec_ctx, void *arg,
83 bool success) {
84 connector_unref(exec_ctx, arg);
85 }
86
87 static void connected(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
88 connector *c = arg;
89 grpc_closure *notify;
90 grpc_endpoint *tcp = c->tcp;
91 if (tcp != NULL) {
92 if (!GPR_SLICE_IS_EMPTY(c->args.initial_connect_string)) {
93 grpc_closure_init(&c->initial_string_sent, on_initial_connect_string_sent,
94 c);
95 gpr_slice_buffer_init(&c->initial_string_buffer);
96 gpr_slice_buffer_add(&c->initial_string_buffer,
97 c->args.initial_connect_string);
98 connector_ref(arg);
99 grpc_endpoint_write(exec_ctx, tcp, &c->initial_string_buffer,
100 &c->initial_string_sent);
101 }
102 c->result->transport =
103 grpc_create_chttp2_transport(exec_ctx, c->args.channel_args, tcp, 1);
104 grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL,
105 0);
106 GPR_ASSERT(c->result->transport);
107 c->result->channel_args = c->args.channel_args;
108 c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *));
109 c->result->filters[0] = &grpc_http_client_filter;
110 c->result->num_filters = 1;
111 } else {
112 memset(c->result, 0, sizeof(*c->result));
113 }
114 notify = c->notify;
115 c->notify = NULL;
116 notify->cb(exec_ctx, notify->cb_arg, 1);
117 }
118
119 static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {}
120
121 static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con,
122 const grpc_connect_in_args *args,
123 grpc_connect_out_args *result,
124 grpc_closure *notify) {
125 connector *c = (connector *)con;
126 GPR_ASSERT(c->notify == NULL);
127 GPR_ASSERT(notify->cb);
128 c->notify = notify;
129 c->args = *args;
130 c->result = result;
131 c->tcp = NULL;
132 grpc_closure_init(&c->connected, connected, c);
133 grpc_tcp_client_connect(exec_ctx, &c->connected, &c->tcp,
134 args->interested_parties, args->addr, args->addr_len,
135 args->deadline);
136 }
137
138 static const grpc_connector_vtable connector_vtable = {
139 connector_ref, connector_unref, connector_shutdown, connector_connect};
140
141 typedef struct {
142 grpc_subchannel_factory base;
143 gpr_refcount refs;
144 grpc_channel_args *merge_args;
145 grpc_channel *master;
146 } subchannel_factory;
147
148 static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
149 subchannel_factory *f = (subchannel_factory *)scf;
150 gpr_ref(&f->refs);
151 }
152
153 static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
154 grpc_subchannel_factory *scf) {
155 subchannel_factory *f = (subchannel_factory *)scf;
156 if (gpr_unref(&f->refs)) {
157 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, "subchannel_factory");
158 grpc_channel_args_destroy(f->merge_args);
159 gpr_free(f);
160 }
161 }
162
163 static grpc_subchannel *subchannel_factory_create_subchannel(
164 grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf,
165 grpc_subchannel_args *args) {
166 subchannel_factory *f = (subchannel_factory *)scf;
167 connector *c = gpr_malloc(sizeof(*c));
168 grpc_channel_args *final_args =
169 grpc_channel_args_merge(args->args, f->merge_args);
170 grpc_subchannel *s;
171 memset(c, 0, sizeof(*c));
172 c->base.vtable = &connector_vtable;
173 gpr_ref_init(&c->refs, 1);
174 args->args = final_args;
175 s = grpc_subchannel_create(exec_ctx, &c->base, args);
176 grpc_connector_unref(exec_ctx, &c->base);
177 grpc_channel_args_destroy(final_args);
178 return s;
179 }
180
181 static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
182 subchannel_factory_ref, subchannel_factory_unref,
183 subchannel_factory_create_subchannel};
184
185 /* Create a client channel:
186 Asynchronously: - resolve target
187 - connect to it (trying alternatives as presented)
188 - perform handshakes */
189 grpc_channel *grpc_insecure_channel_create(const char *target,
190 const grpc_channel_args *args,
191 void *reserved) {
192 grpc_channel *channel = NULL;
193 #define MAX_FILTERS 3
194 const grpc_channel_filter *filters[MAX_FILTERS];
195 grpc_resolver *resolver;
196 subchannel_factory *f;
197 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
198 size_t n = 0;
199 GRPC_API_TRACE(
200 "grpc_insecure_channel_create(target=%p, args=%p, reserved=%p)", 3,
201 (target, args, reserved));
202 GPR_ASSERT(!reserved);
203 if (grpc_channel_args_is_census_enabled(args)) {
204 filters[n++] = &grpc_client_census_filter;
205 }
206 filters[n++] = &grpc_compress_filter;
207 filters[n++] = &grpc_client_channel_filter;
208 GPR_ASSERT(n <= MAX_FILTERS);
209
210 channel =
211 grpc_channel_create_from_filters(&exec_ctx, target, filters, n, args, 1);
212
213 f = gpr_malloc(sizeof(*f));
214 f->base.vtable = &subchannel_factory_vtable;
215 gpr_ref_init(&f->refs, 1);
216 f->merge_args = grpc_channel_args_copy(args);
217 f->master = channel;
218 GRPC_CHANNEL_INTERNAL_REF(f->master, "subchannel_factory");
219 resolver = grpc_resolver_create(target, &f->base);
220 if (!resolver) {
221 GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, f->master, "subchannel_factory");
222 grpc_subchannel_factory_unref(&exec_ctx, &f->base);
223 grpc_exec_ctx_finish(&exec_ctx);
224 return NULL;
225 }
226
227 grpc_client_channel_set_resolver(
228 &exec_ctx, grpc_channel_get_channel_stack(channel), resolver);
229 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "create");
230 grpc_subchannel_factory_unref(&exec_ctx, &f->base);
231
232 grpc_exec_ctx_finish(&exec_ctx);
233
234 return channel;
235 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/surface/channel_connectivity.c ('k') | third_party/grpc/src/core/surface/channel_ping.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698