blob: 29b042e7c1a491ee46913f8a99ee7de1edbbfe09 [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;
55 grpc_mdctx *metadata_context;
56 grpc_mdstr *grpc_status_string;
57 grpc_mdstr *grpc_message_string;
klempnerc463f742014-12-19 13:03:35 -080058 grpc_mdstr *path_string;
59 grpc_mdstr *authority_string;
Craig Tiller08453372015-04-10 16:05:38 -070060
61 gpr_mu registered_call_mu;
62 registered_call *registered_calls;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063};
64
Craig Tiller6902ad22015-04-16 08:01:49 -070065#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
Craig Tillerb20111c2015-04-10 23:27:11 +000066#define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
67 (((grpc_channel *)(channel_stack)) - 1)
Craig Tillerda669372015-02-05 10:10:15 -080068#define CHANNEL_FROM_TOP_ELEM(top_elem) \
69 CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070
71grpc_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 Tiller60fd3612015-03-05 16:24:22 -080077 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 channel->is_client = is_client;
Craig Tillerb20111c2015-04-10 23:27:11 +000079 /* decremented by grpc_channel_destroy, and grpc_client_channel_closed if
80 * is_client */
Craig Tillerda669372015-02-05 10:10:15 -080081 gpr_ref_init(&channel->refs, 1 + is_client);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082 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");
klempnerc463f742014-12-19 13:03:35 -080085 channel->path_string = grpc_mdstr_from_string(mdctx, ":path");
86 channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087 grpc_channel_stack_init(filters, num_filters, args, channel->metadata_context,
88 CHANNEL_STACK_FROM_CHANNEL(channel));
Craig Tiller08453372015-04-10 16:05:38 -070089 gpr_mu_init(&channel->registered_call_mu);
90 channel->registered_calls = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 return channel;
92}
93
Craig Tiller08453372015-04-10 16:05:38 -070094static grpc_call *grpc_channel_create_call_internal(
Craig Tillerb20111c2015-04-10 23:27:11 +000095 grpc_channel *channel, grpc_completion_queue *cq, grpc_mdelem *path_mdelem,
96 grpc_mdelem *authority_mdelem, gpr_timespec deadline) {
Craig Tiller6902ad22015-04-16 08:01:49 -070097 grpc_mdelem *send_metadata[2];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098
Craig Tiller6902ad22015-04-16 08:01:49 -070099 GPR_ASSERT(channel->is_client);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100
Craig Tiller6902ad22015-04-16 08:01:49 -0700101 send_metadata[0] = path_mdelem;
102 send_metadata[1] = authority_mdelem;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103
Craig Tiller6902ad22015-04-16 08:01:49 -0700104 return grpc_call_create(channel, cq, NULL, send_metadata,
105 GPR_ARRAY_SIZE(send_metadata), deadline);
106}
klempnerc463f742014-12-19 13:03:35 -0800107
Craig Tiller6902ad22015-04-16 08:01:49 -0700108grpc_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 Nobleb7ebd3b2014-11-26 16:33:03 -0800113}
114
Craig Tiller08453372015-04-10 16:05:38 -0700115grpc_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 Tillerb20111c2015-04-10 23:27:11 +0000120 channel, cq,
Craig Tiller08453372015-04-10 16:05:38 -0700121 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 Tillerb20111c2015-04-10 23:27:11 +0000130void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700131 const char *host) {
132 registered_call *rc = gpr_malloc(sizeof(registered_call));
133 rc->path = grpc_mdelem_from_metadata_strings(
Craig Tillerb20111c2015-04-10 23:27:11 +0000134 channel->metadata_context, grpc_mdstr_ref(channel->path_string),
135 grpc_mdstr_from_string(channel->metadata_context, method));
Craig Tiller08453372015-04-10 16:05:38 -0700136 rc->authority = grpc_mdelem_from_metadata_strings(
Craig Tillerb20111c2015-04-10 23:27:11 +0000137 channel->metadata_context, grpc_mdstr_ref(channel->authority_string),
138 grpc_mdstr_from_string(channel->metadata_context, host));
Craig Tiller08453372015-04-10 16:05:38 -0700139 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 Tillerb20111c2015-04-10 23:27:11 +0000146grpc_call *grpc_channel_create_registered_call(
147 grpc_channel *channel, grpc_completion_queue *completion_queue,
148 void *registered_call_handle, gpr_timespec deadline) {
Craig Tiller08453372015-04-10 16:05:38 -0700149 registered_call *rc = registered_call_handle;
Craig Tillerb20111c2015-04-10 23:27:11 +0000150 return grpc_channel_create_call_internal(
151 channel, completion_queue, grpc_mdelem_ref(rc->path),
152 grpc_mdelem_ref(rc->authority), deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700153}
154
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155void grpc_channel_internal_ref(grpc_channel *channel) {
156 gpr_ref(&channel->refs);
157}
158
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800159static 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 Tiller08453372015-04-10 16:05:38 -0700166 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 Tiller9be83ee2015-02-18 14:16:15 -0800173 grpc_mdctx_unref(channel->metadata_context);
Craig Tiller08453372015-04-10 16:05:38 -0700174 gpr_mu_destroy(&channel->registered_call_mu);
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800175 gpr_free(channel);
176}
177
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178void grpc_channel_internal_unref(grpc_channel *channel) {
179 if (gpr_unref(&channel->refs)) {
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800180 grpc_iomgr_add_callback(destroy_channel, channel);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181 }
182}
183
184void grpc_channel_destroy(grpc_channel *channel) {
185 grpc_channel_op op;
186 grpc_channel_element *elem;
187
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
nnoble0c475f02014-12-05 15:37:39 -0800189
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");
ctillerf962f522014-12-10 15:28:27 -0800194 elem->filter->channel_op(elem, NULL, &op);
nnoble0c475f02014-12-05 15:37:39 -0800195
196 op.type = GRPC_CHANNEL_DISCONNECT;
197 op.dir = GRPC_CALL_DOWN;
ctillerf962f522014-12-10 15:28:27 -0800198 elem->filter->channel_op(elem, NULL, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199
200 grpc_channel_internal_unref(channel);
201}
202
Craig Tillerda669372015-02-05 10:10:15 -0800203void grpc_client_channel_closed(grpc_channel_element *elem) {
204 grpc_channel_internal_unref(CHANNEL_FROM_TOP_ELEM(elem));
205}
206
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
208 return CHANNEL_STACK_FROM_CHANNEL(channel);
209}
210
211grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel) {
212 return channel->metadata_context;
213}
214
215grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) {
216 return channel->grpc_status_string;
217}
218
219grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) {
220 return channel->grpc_message_string;
Craig Tiller190d3602015-02-18 09:23:38 -0800221}