blob: 78f9144c19c980998bd41928170a5aa90a8c9ae0 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller7bd5ab12015-02-17 22:29:04 -080039#include "src/core/iomgr/iomgr.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include "src/core/surface/call.h"
41#include "src/core/surface/client.h"
Craig Tiller60fd3612015-03-05 16:24:22 -080042#include "src/core/surface/init.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc/support/alloc.h>
44#include <grpc/support/log.h>
45
Craig Tiller08453372015-04-10 16:05:38 -070046typedef struct registered_call {
47 grpc_mdelem *path;
48 grpc_mdelem *authority;
49 struct registered_call *next;
50} registered_call;
51
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052struct grpc_channel {
53 int is_client;
54 gpr_refcount refs;
Craig Tiller629b0ed2015-04-22 11:14:26 -070055 gpr_uint32 max_message_length;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056 grpc_mdctx *metadata_context;
57 grpc_mdstr *grpc_status_string;
58 grpc_mdstr *grpc_message_string;
klempnerc463f742014-12-19 13:03:35 -080059 grpc_mdstr *path_string;
60 grpc_mdstr *authority_string;
Craig Tiller08453372015-04-10 16:05:38 -070061
62 gpr_mu registered_call_mu;
63 registered_call *registered_calls;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064};
65
Craig Tiller6902ad22015-04-16 08:01:49 -070066#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
Craig Tillerb20111c2015-04-10 23:27:11 +000067#define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
68 (((grpc_channel *)(channel_stack)) - 1)
Craig Tillerda669372015-02-05 10:10:15 -080069#define CHANNEL_FROM_TOP_ELEM(top_elem) \
70 CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
Craig Tiller629b0ed2015-04-22 11:14:26 -070072/* the protobuf library will (by default) start warning at 100megs */
73#define DEFAULT_MAX_MESSAGE_LENGTH (100 * 1024 * 1024)
74
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075grpc_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 Tiller629b0ed2015-04-22 11:14:26 -070078 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 size_t size =
80 sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters);
81 grpc_channel *channel = gpr_malloc(size);
Craig Tiller60fd3612015-03-05 16:24:22 -080082 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 channel->is_client = is_client;
Craig Tillerb20111c2015-04-10 23:27:11 +000084 /* decremented by grpc_channel_destroy, and grpc_client_channel_closed if
85 * is_client */
Craig Tillerda669372015-02-05 10:10:15 -080086 gpr_ref_init(&channel->refs, 1 + is_client);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087 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");
klempnerc463f742014-12-19 13:03:35 -080090 channel->path_string = grpc_mdstr_from_string(mdctx, ":path");
91 channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 grpc_channel_stack_init(filters, num_filters, args, channel->metadata_context,
93 CHANNEL_STACK_FROM_CHANNEL(channel));
Craig Tiller08453372015-04-10 16:05:38 -070094 gpr_mu_init(&channel->registered_call_mu);
95 channel->registered_calls = NULL;
Craig Tiller629b0ed2015-04-22 11:14:26 -070096
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 Nobleb7ebd3b2014-11-26 16:33:03 -0800114 return channel;
115}
116
Craig Tiller08453372015-04-10 16:05:38 -0700117static grpc_call *grpc_channel_create_call_internal(
Craig Tillerb20111c2015-04-10 23:27:11 +0000118 grpc_channel *channel, grpc_completion_queue *cq, grpc_mdelem *path_mdelem,
119 grpc_mdelem *authority_mdelem, gpr_timespec deadline) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700120 grpc_mdelem *send_metadata[2];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
Craig Tiller6902ad22015-04-16 08:01:49 -0700122 GPR_ASSERT(channel->is_client);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123
Craig Tiller6902ad22015-04-16 08:01:49 -0700124 send_metadata[0] = path_mdelem;
125 send_metadata[1] = authority_mdelem;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tiller6902ad22015-04-16 08:01:49 -0700127 return grpc_call_create(channel, cq, NULL, send_metadata,
128 GPR_ARRAY_SIZE(send_metadata), deadline);
129}
klempnerc463f742014-12-19 13:03:35 -0800130
Craig Tiller6902ad22015-04-16 08:01:49 -0700131grpc_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 Nobleb7ebd3b2014-11-26 16:33:03 -0800136}
137
Craig Tiller08453372015-04-10 16:05:38 -0700138grpc_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 Tillerb20111c2015-04-10 23:27:11 +0000143 channel, cq,
Craig Tiller08453372015-04-10 16:05:38 -0700144 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 Tillerb20111c2015-04-10 23:27:11 +0000153void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700154 const char *host) {
155 registered_call *rc = gpr_malloc(sizeof(registered_call));
156 rc->path = grpc_mdelem_from_metadata_strings(
Craig Tillerb20111c2015-04-10 23:27:11 +0000157 channel->metadata_context, grpc_mdstr_ref(channel->path_string),
158 grpc_mdstr_from_string(channel->metadata_context, method));
Craig Tiller08453372015-04-10 16:05:38 -0700159 rc->authority = grpc_mdelem_from_metadata_strings(
Craig Tillerb20111c2015-04-10 23:27:11 +0000160 channel->metadata_context, grpc_mdstr_ref(channel->authority_string),
161 grpc_mdstr_from_string(channel->metadata_context, host));
Craig Tiller08453372015-04-10 16:05:38 -0700162 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 Tillerb20111c2015-04-10 23:27:11 +0000169grpc_call *grpc_channel_create_registered_call(
170 grpc_channel *channel, grpc_completion_queue *completion_queue,
171 void *registered_call_handle, gpr_timespec deadline) {
Craig Tiller08453372015-04-10 16:05:38 -0700172 registered_call *rc = registered_call_handle;
Craig Tillerb20111c2015-04-10 23:27:11 +0000173 return grpc_channel_create_call_internal(
174 channel, completion_queue, grpc_mdelem_ref(rc->path),
175 grpc_mdelem_ref(rc->authority), deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700176}
177
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178void grpc_channel_internal_ref(grpc_channel *channel) {
179 gpr_ref(&channel->refs);
180}
181
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800182static 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 Tiller08453372015-04-10 16:05:38 -0700189 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 Tiller9be83ee2015-02-18 14:16:15 -0800196 grpc_mdctx_unref(channel->metadata_context);
Craig Tiller08453372015-04-10 16:05:38 -0700197 gpr_mu_destroy(&channel->registered_call_mu);
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800198 gpr_free(channel);
199}
200
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201void grpc_channel_internal_unref(grpc_channel *channel) {
202 if (gpr_unref(&channel->refs)) {
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800203 grpc_iomgr_add_callback(destroy_channel, channel);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 }
205}
206
207void grpc_channel_destroy(grpc_channel *channel) {
208 grpc_channel_op op;
209 grpc_channel_element *elem;
210
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211 elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
nnoble0c475f02014-12-05 15:37:39 -0800212
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");
ctillerf962f522014-12-10 15:28:27 -0800217 elem->filter->channel_op(elem, NULL, &op);
nnoble0c475f02014-12-05 15:37:39 -0800218
219 op.type = GRPC_CHANNEL_DISCONNECT;
220 op.dir = GRPC_CALL_DOWN;
ctillerf962f522014-12-10 15:28:27 -0800221 elem->filter->channel_op(elem, NULL, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222
223 grpc_channel_internal_unref(channel);
224}
225
Craig Tillerda669372015-02-05 10:10:15 -0800226void grpc_client_channel_closed(grpc_channel_element *elem) {
227 grpc_channel_internal_unref(CHANNEL_FROM_TOP_ELEM(elem));
228}
229
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
231 return CHANNEL_STACK_FROM_CHANNEL(channel);
232}
233
234grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel) {
235 return channel->metadata_context;
236}
237
238grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) {
239 return channel->grpc_status_string;
240}
241
242grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) {
243 return channel->grpc_message_string;
Craig Tiller190d3602015-02-18 09:23:38 -0800244}
Craig Tillerfbf5be22015-04-22 16:17:09 -0700245
246gpr_uint32 grpc_channel_get_max_message_length(grpc_channel *channel) {
247 return channel->max_message_length;
Craig Tiller09b49d72015-04-22 16:40:23 -0700248}