1 /* 2 * 3 * Copyright 2016 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H 20 #define GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <stdbool.h> 25 26 #include "src/core/lib/channel/channel_args.h" 27 #include "src/core/lib/channel/channel_stack.h" 28 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; 34 35 /// Create a new channel stack builder 36 grpc_channel_stack_builder* grpc_channel_stack_builder_create(void); 37 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, 40 const char* name); 41 42 /// Set the target uri 43 void grpc_channel_stack_builder_set_target(grpc_channel_stack_builder* b, 44 const char* target); 45 46 const char* grpc_channel_stack_builder_get_target( 47 grpc_channel_stack_builder* b); 48 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); 52 53 /// Fetch attached transport 54 grpc_transport* grpc_channel_stack_builder_get_transport( 55 grpc_channel_stack_builder* builder); 56 57 /// Set channel arguments: copies args 58 void grpc_channel_stack_builder_set_channel_arguments( 59 grpc_channel_stack_builder* builder, const grpc_channel_args* args); 60 61 /// Return a borrowed pointer to the channel arguments 62 const grpc_channel_args* grpc_channel_stack_builder_get_channel_arguments( 63 grpc_channel_stack_builder* builder); 64 65 /// Begin iterating over already defined filters in the builder at the beginning 66 grpc_channel_stack_builder_iterator* 67 grpc_channel_stack_builder_create_iterator_at_first( 68 grpc_channel_stack_builder* builder); 69 70 /// Begin iterating over already defined filters in the builder at the end 71 grpc_channel_stack_builder_iterator* 72 grpc_channel_stack_builder_create_iterator_at_last( 73 grpc_channel_stack_builder* builder); 74 75 /// Is an iterator at the first element? 76 bool grpc_channel_stack_builder_iterator_is_first( 77 grpc_channel_stack_builder_iterator* iterator); 78 79 /// Is an iterator at the end? 80 bool grpc_channel_stack_builder_iterator_is_end( 81 grpc_channel_stack_builder_iterator* iterator); 82 83 /// What is the name of the filter at this iterator position? 84 const char* grpc_channel_stack_builder_iterator_filter_name( 85 grpc_channel_stack_builder_iterator* iterator); 86 87 /// Move an iterator to the next item 88 bool grpc_channel_stack_builder_move_next( 89 grpc_channel_stack_builder_iterator* iterator); 90 91 /// Move an iterator to the previous item 92 bool grpc_channel_stack_builder_move_prev( 93 grpc_channel_stack_builder_iterator* iterator); 94 95 /// Return an iterator at \a filter_name, or at the end of the list if not 96 /// found. 97 grpc_channel_stack_builder_iterator* grpc_channel_stack_builder_iterator_find( 98 grpc_channel_stack_builder* builder, const char* filter_name); 99 100 typedef void (*grpc_post_filter_create_init_func)( 101 grpc_channel_stack* channel_stack, grpc_channel_element* elem, void* arg); 102 103 /// Add \a filter to the stack, after \a iterator. 104 /// Call \a post_init_func(..., \a user_data) once the channel stack is 105 /// created. 106 bool grpc_channel_stack_builder_add_filter_after( 107 grpc_channel_stack_builder_iterator* iterator, 108 const grpc_channel_filter* filter, 109 grpc_post_filter_create_init_func post_init_func, 110 void* user_data) GRPC_MUST_USE_RESULT; 111 112 /// Add \a filter to the stack, before \a iterator. 113 /// Call \a post_init_func(..., \a user_data) once the channel stack is 114 /// created. 115 bool grpc_channel_stack_builder_add_filter_before( 116 grpc_channel_stack_builder_iterator* iterator, 117 const grpc_channel_filter* filter, 118 grpc_post_filter_create_init_func post_init_func, 119 void* user_data) GRPC_MUST_USE_RESULT; 120 121 /// Add \a filter to the beginning of the filter list. 122 /// Call \a post_init_func(..., \a user_data) once the channel stack is 123 /// created. 124 bool grpc_channel_stack_builder_prepend_filter( 125 grpc_channel_stack_builder* builder, const grpc_channel_filter* filter, 126 grpc_post_filter_create_init_func post_init_func, 127 void* user_data) GRPC_MUST_USE_RESULT; 128 129 /// Add \a filter to the end of the filter list. 130 /// Call \a post_init_func(..., \a user_data) once the channel stack is 131 /// created. 132 bool grpc_channel_stack_builder_append_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; 136 137 /// Remove any filter whose name is \a filter_name from \a builder. Returns true 138 /// if \a filter_name was not found. 139 bool grpc_channel_stack_builder_remove_filter( 140 grpc_channel_stack_builder* builder, const char* filter_name); 141 142 /// Terminate iteration and destroy \a iterator 143 void grpc_channel_stack_builder_iterator_destroy( 144 grpc_channel_stack_builder_iterator* iterator); 145 146 /// Destroy the builder, return the freshly minted channel stack in \a result. 147 /// Allocates \a prefix_bytes bytes before the channel stack 148 /// Returns the base pointer of the allocated block 149 /// \a initial_refs, \a destroy, \a destroy_arg are as per 150 /// grpc_channel_stack_init 151 grpc_error* grpc_channel_stack_builder_finish( 152 grpc_channel_stack_builder* builder, size_t prefix_bytes, int initial_refs, 153 grpc_iomgr_cb_func destroy, void* destroy_arg, void** result); 154 155 /// Destroy the builder without creating a channel stack 156 void grpc_channel_stack_builder_destroy(grpc_channel_stack_builder* builder); 157 158 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H */ 159