blob: 037f0c0ab04ec2df894abdff045dcca0cefb5518 [file] [log] [blame]
Craig Tilleraf691802015-06-23 14:57:07 -07001/*
2 *
3 * Copyright 2015, 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 "src/core/client_config/subchannel.h"
Craig Tiller2595ab72015-06-25 15:26:00 -070035
Craig Tillerf7afa1f2015-06-26 09:02:20 -070036#include <string.h>
37
Craig Tiller2595ab72015-06-25 15:26:00 -070038#include <grpc/support/alloc.h>
39
40struct grpc_subchannel {
41 gpr_refcount refs;
Craig Tiller91624662015-06-25 16:31:02 -070042 grpc_connector *connector;
Craig Tillerf7afa1f2015-06-26 09:02:20 -070043
44 /** non-transport related channel filters */
45 const grpc_channel_filter **filters;
46 size_t filter_count;
47 /** channel arguments */
48 grpc_channel_args *args;
49 /** address to connect to */
50 struct sockaddr *addr;
51 size_t addr_len;
Craig Tiller2595ab72015-06-25 15:26:00 -070052};
53
54struct grpc_subchannel_call {
55 grpc_subchannel *subchannel;
56 gpr_refcount refs;
57};
58
59#define SUBCHANNEL_CALL_TO_CALL_STACK(call) (((grpc_call_stack *)(call)) + 1)
60
61/*
62 * grpc_subchannel implementation
63 */
64
65void grpc_subchannel_ref(grpc_subchannel *channel) { gpr_ref(&channel->refs); }
66
67void grpc_subchannel_unref(grpc_subchannel *channel) {
68 if (gpr_unref(&channel->refs)) {
69 gpr_free(channel);
70 }
71}
72
Craig Tillerf7afa1f2015-06-26 09:02:20 -070073grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
74 grpc_subchannel_args *args) {
75 grpc_subchannel *c = gpr_malloc(sizeof(*c));
76 memset(c, 0, sizeof(*c));
77 gpr_ref_init(&c->refs, 1);
78 c->connector = connector;
79 grpc_connector_ref(c->connector);
80 c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * args->filter_count);
81 memcpy(c->filters, args->filters,
82 sizeof(grpc_channel_filter *) * args->filter_count);
83 c->filter_count = args->filter_count;
84 c->addr = gpr_malloc(args->addr_len);
85 memcpy(c->addr, args->addr, args->addr_len);
86 c->addr_len = args->addr_len;
87 return c;
88}
89
Craig Tiller2595ab72015-06-25 15:26:00 -070090/*
91 * grpc_subchannel_call implementation
92 */
93
94void grpc_subchannel_call_ref(grpc_subchannel_call *call) {
95 gpr_ref(&call->refs);
96}
97
98void grpc_subchannel_call_unref(grpc_subchannel_call *call) {
99 if (gpr_unref(&call->refs)) {
100 grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(call));
101 grpc_subchannel_unref(call->subchannel);
102 gpr_free(call);
103 }
104}
105
106void grpc_subchannel_call_process_op(grpc_subchannel_call *call,
107 grpc_transport_stream_op *op) {
108 grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
109 grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
110 top_elem->filter->start_transport_stream_op(top_elem, op);
111}