aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/channel/channel_stack_builder.h
blob: fdff2a2b6dbee2161dae1b6bf8ae341dbae4e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 *
 * Copyright 2016 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
#define GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H

#include <stdbool.h>

#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack.h"

#ifdef __cplusplus
extern "C" {
#endif

/// grpc_channel_stack_builder offers a programmatic interface to selected
/// and order channel filters
typedef struct grpc_channel_stack_builder grpc_channel_stack_builder;
typedef struct grpc_channel_stack_builder_iterator
    grpc_channel_stack_builder_iterator;

/// Create a new channel stack builder
grpc_channel_stack_builder *grpc_channel_stack_builder_create(void);

/// Assign a name to the channel stack: \a name must be statically allocated
void grpc_channel_stack_builder_set_name(grpc_channel_stack_builder *builder,
                                         const char *name);

/// Set the target uri
void grpc_channel_stack_builder_set_target(grpc_channel_stack_builder *b,
                                           const char *target);

const char *grpc_channel_stack_builder_get_target(
    grpc_channel_stack_builder *b);

/// Attach \a transport to the builder (does not take ownership)
void grpc_channel_stack_builder_set_transport(
    grpc_channel_stack_builder *builder, grpc_transport *transport);

/// Fetch attached transport
grpc_transport *grpc_channel_stack_builder_get_transport(
    grpc_channel_stack_builder *builder);

/// Set channel arguments: copies args
void grpc_channel_stack_builder_set_channel_arguments(
    grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
    const grpc_channel_args *args);

/// Return a borrowed pointer to the channel arguments
const grpc_channel_args *grpc_channel_stack_builder_get_channel_arguments(
    grpc_channel_stack_builder *builder);

/// Begin iterating over already defined filters in the builder at the beginning
grpc_channel_stack_builder_iterator *
grpc_channel_stack_builder_create_iterator_at_first(
    grpc_channel_stack_builder *builder);

/// Begin iterating over already defined filters in the builder at the end
grpc_channel_stack_builder_iterator *
grpc_channel_stack_builder_create_iterator_at_last(
    grpc_channel_stack_builder *builder);

/// Is an iterator at the first element?
bool grpc_channel_stack_builder_iterator_is_first(
    grpc_channel_stack_builder_iterator *iterator);

/// Is an iterator at the end?
bool grpc_channel_stack_builder_iterator_is_end(
    grpc_channel_stack_builder_iterator *iterator);

/// What is the name of the filter at this iterator position?
const char *grpc_channel_stack_builder_iterator_filter_name(
    grpc_channel_stack_builder_iterator *iterator);

/// Move an iterator to the next item
bool grpc_channel_stack_builder_move_next(
    grpc_channel_stack_builder_iterator *iterator);

/// Move an iterator to the previous item
bool grpc_channel_stack_builder_move_prev(
    grpc_channel_stack_builder_iterator *iterator);

/// Return an iterator at \a filter_name, or at the end of the list if not
/// found.
grpc_channel_stack_builder_iterator *grpc_channel_stack_builder_iterator_find(
    grpc_channel_stack_builder *builder, const char *filter_name);

typedef void (*grpc_post_filter_create_init_func)(
    grpc_channel_stack *channel_stack, grpc_channel_element *elem, void *arg);

/// Add \a filter to the stack, after \a iterator.
/// Call \a post_init_func(..., \a user_data) once the channel stack is
/// created.
bool grpc_channel_stack_builder_add_filter_after(
    grpc_channel_stack_builder_iterator *iterator,
    const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func,
    void *user_data) GRPC_MUST_USE_RESULT;

/// Add \a filter to the stack, before \a iterator.
/// Call \a post_init_func(..., \a user_data) once the channel stack is
/// created.
bool grpc_channel_stack_builder_add_filter_before(
    grpc_channel_stack_builder_iterator *iterator,
    const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func,
    void *user_data) GRPC_MUST_USE_RESULT;

/// Add \a filter to the beginning of the filter list.
/// Call \a post_init_func(..., \a user_data) once the channel stack is
/// created.
bool grpc_channel_stack_builder_prepend_filter(
    grpc_channel_stack_builder *builder, const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func,
    void *user_data) GRPC_MUST_USE_RESULT;

/// Add \a filter to the end of the filter list.
/// Call \a post_init_func(..., \a user_data) once the channel stack is
/// created.
bool grpc_channel_stack_builder_append_filter(
    grpc_channel_stack_builder *builder, const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func,
    void *user_data) GRPC_MUST_USE_RESULT;

/// Remove any filter whose name is \a filter_name from \a builder. Returns true
/// if \a filter_name was not found.
bool grpc_channel_stack_builder_remove_filter(
    grpc_channel_stack_builder *builder, const char *filter_name);

/// Terminate iteration and destroy \a iterator
void grpc_channel_stack_builder_iterator_destroy(
    grpc_channel_stack_builder_iterator *iterator);

/// Destroy the builder, return the freshly minted channel stack in \a result.
/// Allocates \a prefix_bytes bytes before the channel stack
/// Returns the base pointer of the allocated block
/// \a initial_refs, \a destroy, \a destroy_arg are as per
/// grpc_channel_stack_init
grpc_error *grpc_channel_stack_builder_finish(
    grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
    size_t prefix_bytes, int initial_refs, grpc_iomgr_cb_func destroy,
    void *destroy_arg, void **result);

/// Destroy the builder without creating a channel stack
void grpc_channel_stack_builder_destroy(grpc_exec_ctx *exec_ctx,
                                        grpc_channel_stack_builder *builder);

extern grpc_tracer_flag grpc_trace_channel_stack_builder;

#ifdef __cplusplus
}
#endif

#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H */