3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
20 #define GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
22 #include <grpc/support/port_platform.h>
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/channel/channel_stack.h"
29 /// grpc_channel_stack_builder offers a programmatic interface to selected
30 /// and order channel filters
31 typedef struct grpc_channel_stack_builder grpc_channel_stack_builder;
32 typedef struct grpc_channel_stack_builder_iterator
33 grpc_channel_stack_builder_iterator;
35 /// Create a new channel stack builder
36 grpc_channel_stack_builder* grpc_channel_stack_builder_create(void);
38 /// Assign a name to the channel stack: \a name must be statically allocated
39 void grpc_channel_stack_builder_set_name(grpc_channel_stack_builder* builder,
42 /// Set the target uri
43 void grpc_channel_stack_builder_set_target(grpc_channel_stack_builder* b,
46 const char* grpc_channel_stack_builder_get_target(
47 grpc_channel_stack_builder* b);
49 /// Attach \a transport to the builder (does not take ownership)
50 void grpc_channel_stack_builder_set_transport(
51 grpc_channel_stack_builder* builder, grpc_transport* transport);
53 /// Fetch attached transport
54 grpc_transport* grpc_channel_stack_builder_get_transport(
55 grpc_channel_stack_builder* builder);
57 /// Attach \a resource_user to the builder (does not take ownership)
58 void grpc_channel_stack_builder_set_resource_user(
59 grpc_channel_stack_builder* builder, grpc_resource_user* resource_user);
61 /// Fetch attached resource user
62 grpc_resource_user* grpc_channel_stack_builder_get_resource_user(
63 grpc_channel_stack_builder* builder);
65 /// Set channel arguments: copies args
66 void grpc_channel_stack_builder_set_channel_arguments(
67 grpc_channel_stack_builder* builder, const grpc_channel_args* args);
69 /// Return a borrowed pointer to the channel arguments
70 const grpc_channel_args* grpc_channel_stack_builder_get_channel_arguments(
71 grpc_channel_stack_builder* builder);
73 /// Begin iterating over already defined filters in the builder at the beginning
74 grpc_channel_stack_builder_iterator*
75 grpc_channel_stack_builder_create_iterator_at_first(
76 grpc_channel_stack_builder* builder);
78 /// Begin iterating over already defined filters in the builder at the end
79 grpc_channel_stack_builder_iterator*
80 grpc_channel_stack_builder_create_iterator_at_last(
81 grpc_channel_stack_builder* builder);
83 /// Is an iterator at the first element?
84 bool grpc_channel_stack_builder_iterator_is_first(
85 grpc_channel_stack_builder_iterator* iterator);
87 /// Is an iterator at the end?
88 bool grpc_channel_stack_builder_iterator_is_end(
89 grpc_channel_stack_builder_iterator* iterator);
91 /// What is the name of the filter at this iterator position?
92 const char* grpc_channel_stack_builder_iterator_filter_name(
93 grpc_channel_stack_builder_iterator* iterator);
95 /// Move an iterator to the next item
96 bool grpc_channel_stack_builder_move_next(
97 grpc_channel_stack_builder_iterator* iterator);
99 /// Move an iterator to the previous item
100 bool grpc_channel_stack_builder_move_prev(
101 grpc_channel_stack_builder_iterator* iterator);
103 /// Return an iterator at \a filter_name, or at the end of the list if not
105 grpc_channel_stack_builder_iterator* grpc_channel_stack_builder_iterator_find(
106 grpc_channel_stack_builder* builder, const char* filter_name);
108 typedef void (*grpc_post_filter_create_init_func)(
109 grpc_channel_stack* channel_stack, grpc_channel_element* elem, void* arg);
111 /// Add \a filter to the stack, after \a iterator.
112 /// Call \a post_init_func(..., \a user_data) once the channel stack is
114 bool grpc_channel_stack_builder_add_filter_after(
115 grpc_channel_stack_builder_iterator* iterator,
116 const grpc_channel_filter* filter,
117 grpc_post_filter_create_init_func post_init_func,
118 void* user_data) GRPC_MUST_USE_RESULT;
120 /// Add \a filter to the stack, before \a iterator.
121 /// Call \a post_init_func(..., \a user_data) once the channel stack is
123 bool grpc_channel_stack_builder_add_filter_before(
124 grpc_channel_stack_builder_iterator* iterator,
125 const grpc_channel_filter* filter,
126 grpc_post_filter_create_init_func post_init_func,
127 void* user_data) GRPC_MUST_USE_RESULT;
129 /// Add \a filter to the beginning of the filter list.
130 /// Call \a post_init_func(..., \a user_data) once the channel stack is
132 bool grpc_channel_stack_builder_prepend_filter(
133 grpc_channel_stack_builder* builder, const grpc_channel_filter* filter,
134 grpc_post_filter_create_init_func post_init_func,
135 void* user_data) GRPC_MUST_USE_RESULT;
137 /// Add \a filter to the end of the filter list.
138 /// Call \a post_init_func(..., \a user_data) once the channel stack is
140 bool grpc_channel_stack_builder_append_filter(
141 grpc_channel_stack_builder* builder, const grpc_channel_filter* filter,
142 grpc_post_filter_create_init_func post_init_func,
143 void* user_data) GRPC_MUST_USE_RESULT;
145 /// Remove any filter whose name is \a filter_name from \a builder. Returns true
146 /// if \a filter_name was not found.
147 bool grpc_channel_stack_builder_remove_filter(
148 grpc_channel_stack_builder* builder, const char* filter_name);
150 /// Terminate iteration and destroy \a iterator
151 void grpc_channel_stack_builder_iterator_destroy(
152 grpc_channel_stack_builder_iterator* iterator);
154 /// Destroy the builder, return the freshly minted channel stack in \a result.
155 /// Allocates \a prefix_bytes bytes before the channel stack
156 /// Returns the base pointer of the allocated block
157 /// \a initial_refs, \a destroy, \a destroy_arg are as per
158 /// grpc_channel_stack_init
159 grpc_error_handle grpc_channel_stack_builder_finish(
160 grpc_channel_stack_builder* builder, size_t prefix_bytes, int initial_refs,
161 grpc_iomgr_cb_func destroy, void* destroy_arg, void** result);
163 /// Destroy the builder without creating a channel stack
164 void grpc_channel_stack_builder_destroy(grpc_channel_stack_builder* builder);
166 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H */