| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "read_write_test_handler.h" |
| 7 |
|
| 8 |
#include <aws/common/atomics.h> |
| 9 |
#include <aws/common/condition_variable.h> |
| 10 |
#include <aws/common/mutex.h> |
| 11 |
#include <aws/common/task_scheduler.h> |
| 12 |
#include <aws/io/channel.h> |
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
# pragma warning(disable : 4204) |
| 16 |
# pragma warning(disable : 4267) |
| 17 |
#endif |
| 18 |
|
| 19 |
struct rw_test_handler_impl { |
| 20 |
struct aws_atomic_var shutdown_called; |
| 21 |
bool increment_read_window_called; |
| 22 |
struct aws_atomic_var *destroy_called; |
| 23 |
struct aws_condition_variable *destroy_condition_variable; |
| 24 |
rw_handler_driver_fn *on_read; |
| 25 |
rw_handler_driver_fn *on_write; |
| 26 |
bool event_loop_driven; |
| 27 |
size_t window; |
| 28 |
struct aws_condition_variable condition_variable; |
| 29 |
struct aws_mutex mutex; |
| 30 |
struct aws_atomic_var shutdown_error; |
| 31 |
void *ctx; |
| 32 |
}; |
| 33 |
|
| 34 |
static int s_rw_handler_process_read( |
| 35 |
struct aws_channel_handler *handler, |
| 36 |
struct aws_channel_slot *slot, |
| 37 |
struct aws_io_message *message) { |
| 38 |
|
| 39 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 40 |
|
| 41 |
struct aws_byte_buf next_data = handler_impl->on_read(handler, slot, &message->message_data, handler_impl->ctx); |
| 42 |
aws_mem_release(message->allocator, message); |
| 43 |
|
| 44 |
if (slot->adj_right) { |
| 45 |
|
| 46 |
struct aws_io_message *msg = |
| 47 |
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, next_data.len); |
| 48 |
|
| 49 |
struct aws_byte_cursor next_data_cursor = aws_byte_cursor_from_buf(&next_data); |
| 50 |
aws_byte_buf_append(&msg->message_data, &next_data_cursor); |
| 51 |
|
| 52 |
return aws_channel_slot_send_message(slot, msg, AWS_CHANNEL_DIR_READ); |
| 53 |
} |
| 54 |
|
| 55 |
return AWS_OP_SUCCESS; |
| 56 |
} |
| 57 |
|
| 58 |
static int s_rw_handler_process_write_message( |
| 59 |
struct aws_channel_handler *handler, |
| 60 |
struct aws_channel_slot *slot, |
| 61 |
struct aws_io_message *message) { |
| 62 |
|
| 63 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 64 |
|
| 65 |
struct aws_byte_buf next_data = handler_impl->on_write(handler, slot, &message->message_data, handler_impl->ctx); |
| 66 |
aws_mem_release(message->allocator, message); |
| 67 |
|
| 68 |
if (slot->adj_left) { |
| 69 |
struct aws_io_message *msg = |
| 70 |
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, next_data.len); |
| 71 |
|
| 72 |
struct aws_byte_cursor next_data_cursor = aws_byte_cursor_from_buf(&next_data); |
| 73 |
aws_byte_buf_append(&msg->message_data, &next_data_cursor); |
| 74 |
return aws_channel_slot_send_message(slot, msg, AWS_CHANNEL_DIR_WRITE); |
| 75 |
} |
| 76 |
|
| 77 |
return AWS_OP_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
static int s_rw_handler_increment_read_window( |
| 81 |
struct aws_channel_handler *handler, |
| 82 |
struct aws_channel_slot *slot, |
| 83 |
size_t size) { |
| 84 |
|
| 85 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 86 |
handler_impl->increment_read_window_called = true; |
| 87 |
aws_channel_slot_increment_read_window(slot, size); |
| 88 |
return AWS_OP_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static int s_rw_handler_shutdown( |
| 92 |
struct aws_channel_handler *handler, |
| 93 |
struct aws_channel_slot *slot, |
| 94 |
enum aws_channel_direction dir, |
| 95 |
int error_code, |
| 96 |
bool abort_immediately) { |
| 97 |
|
| 98 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 99 |
aws_atomic_store_int(&handler_impl->shutdown_called, true); |
| 100 |
aws_atomic_store_int(&handler_impl->shutdown_error, error_code); |
| 101 |
aws_condition_variable_notify_one(&handler_impl->condition_variable); |
| 102 |
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, abort_immediately); |
| 103 |
} |
| 104 |
|
| 105 |
static size_t s_rw_handler_message_overhead(struct aws_channel_handler *handler) { |
| 106 |
(void)handler; |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
static size_t s_rw_handler_get_current_window_size(struct aws_channel_handler *handler) { |
| 111 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 112 |
return handler_impl->window; |
| 113 |
} |
| 114 |
|
| 115 |
static void s_rw_handler_destroy(struct aws_channel_handler *handler) { |
| 116 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 117 |
|
| 118 |
if (handler_impl->destroy_called) { |
| 119 |
aws_atomic_store_int(handler_impl->destroy_called, 1); |
| 120 |
aws_condition_variable_notify_one(handler_impl->destroy_condition_variable); |
| 121 |
} |
| 122 |
|
| 123 |
aws_mem_release(handler->alloc, handler_impl); |
| 124 |
aws_mem_release(handler->alloc, handler); |
| 125 |
} |
| 126 |
|
| 127 |
struct aws_channel_handler_vtable s_rw_test_vtable = { |
| 128 |
.shutdown = s_rw_handler_shutdown, |
| 129 |
.increment_read_window = s_rw_handler_increment_read_window, |
| 130 |
.initial_window_size = s_rw_handler_get_current_window_size, |
| 131 |
.process_read_message = s_rw_handler_process_read, |
| 132 |
.process_write_message = s_rw_handler_process_write_message, |
| 133 |
.destroy = s_rw_handler_destroy, |
| 134 |
.message_overhead = s_rw_handler_message_overhead, |
| 135 |
}; |
| 136 |
|
| 137 |
struct aws_channel_handler *rw_handler_new( |
| 138 |
struct aws_allocator *allocator, |
| 139 |
rw_handler_driver_fn *on_read, |
| 140 |
rw_handler_driver_fn *on_write, |
| 141 |
bool event_loop_driven, |
| 142 |
size_t window, |
| 143 |
void *ctx) { |
| 144 |
|
| 145 |
struct aws_channel_handler *handler = aws_mem_acquire(allocator, sizeof(struct aws_channel_handler)); |
| 146 |
handler->alloc = allocator; |
| 147 |
handler->vtable = &s_rw_test_vtable; |
| 148 |
|
| 149 |
struct rw_test_handler_impl *handler_impl = aws_mem_acquire(allocator, sizeof(struct rw_test_handler_impl)); |
| 150 |
AWS_ZERO_STRUCT(*handler_impl); |
| 151 |
handler_impl->on_read = on_read; |
| 152 |
handler_impl->on_write = on_write; |
| 153 |
handler_impl->ctx = ctx; |
| 154 |
handler_impl->event_loop_driven = event_loop_driven; |
| 155 |
handler_impl->window = window; |
| 156 |
handler_impl->condition_variable = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT; |
| 157 |
handler_impl->mutex = (struct aws_mutex)AWS_MUTEX_INIT; |
| 158 |
|
| 159 |
handler->impl = handler_impl; |
| 160 |
|
| 161 |
return handler; |
| 162 |
} |
| 163 |
|
| 164 |
void rw_handler_enable_wait_on_destroy( |
| 165 |
struct aws_channel_handler *handler, |
| 166 |
struct aws_atomic_var *destroy_called, |
| 167 |
struct aws_condition_variable *condition_variable) { |
| 168 |
|
| 169 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 170 |
handler_impl->destroy_called = destroy_called; |
| 171 |
handler_impl->destroy_condition_variable = condition_variable; |
| 172 |
} |
| 173 |
|
| 174 |
void rw_handler_trigger_read(struct aws_channel_handler *handler, struct aws_channel_slot *slot) { |
| 175 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 176 |
|
| 177 |
struct aws_byte_buf next_data = handler_impl->on_read(handler, slot, NULL, handler_impl->ctx); |
| 178 |
|
| 179 |
struct aws_io_message *msg = |
| 180 |
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, next_data.len); |
| 181 |
|
| 182 |
struct aws_byte_cursor next_data_cursor = aws_byte_cursor_from_buf(&next_data); |
| 183 |
aws_byte_buf_append(&msg->message_data, &next_data_cursor); |
| 184 |
|
| 185 |
aws_channel_slot_send_message(slot, msg, AWS_CHANNEL_DIR_READ); |
| 186 |
} |
| 187 |
|
| 188 |
struct rw_handler_write_task_args { |
| 189 |
struct aws_channel_handler *handler; |
| 190 |
struct aws_channel_slot *slot; |
| 191 |
struct aws_byte_buf *buffer; |
| 192 |
struct aws_channel_task task; |
| 193 |
aws_channel_on_message_write_completed_fn *on_completion; |
| 194 |
void *user_data; |
| 195 |
}; |
| 196 |
|
| 197 |
static void s_rw_handler_write_now( |
| 198 |
struct aws_channel_slot *slot, |
| 199 |
struct aws_byte_buf *buffer, |
| 200 |
aws_channel_on_message_write_completed_fn *on_completion, |
| 201 |
void *user_data) { |
| 202 |
size_t remaining = buffer->len; |
| 203 |
struct aws_byte_cursor write_cursor = aws_byte_cursor_from_buf(buffer); |
| 204 |
|
| 205 |
while (remaining > 0) { |
| 206 |
size_t chunk_size = remaining; |
| 207 |
struct aws_io_message *msg = |
| 208 |
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, chunk_size); |
| 209 |
|
| 210 |
chunk_size = aws_min_size(chunk_size, msg->message_data.capacity - msg->message_data.len); |
| 211 |
|
| 212 |
msg->on_completion = on_completion; |
| 213 |
msg->user_data = user_data; |
| 214 |
|
| 215 |
struct aws_byte_cursor chunk_cursor = aws_byte_cursor_advance(&write_cursor, chunk_size); |
| 216 |
AWS_FATAL_ASSERT(aws_byte_buf_append(&msg->message_data, &chunk_cursor) == AWS_OP_SUCCESS); |
| 217 |
|
| 218 |
AWS_FATAL_ASSERT(aws_channel_slot_send_message(slot, msg, AWS_CHANNEL_DIR_WRITE) == AWS_OP_SUCCESS); |
| 219 |
|
| 220 |
remaining -= chunk_size; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
static void s_rw_handler_write_task(struct aws_channel_task *task, void *arg, enum aws_task_status task_status) { |
| 225 |
(void)task; |
| 226 |
(void)task_status; |
| 227 |
struct rw_handler_write_task_args *write_task_args = arg; |
| 228 |
s_rw_handler_write_now( |
| 229 |
write_task_args->slot, write_task_args->buffer, write_task_args->on_completion, write_task_args->user_data); |
| 230 |
aws_mem_release(write_task_args->handler->alloc, write_task_args); |
| 231 |
} |
| 232 |
|
| 233 |
void rw_handler_write(struct aws_channel_handler *handler, struct aws_channel_slot *slot, struct aws_byte_buf *buffer) { |
| 234 |
rw_handler_write_with_callback(handler, slot, buffer, NULL , NULL ); |
| 235 |
} |
| 236 |
|
| 237 |
void rw_handler_write_with_callback( |
| 238 |
struct aws_channel_handler *handler, |
| 239 |
struct aws_channel_slot *slot, |
| 240 |
struct aws_byte_buf *buffer, |
| 241 |
aws_channel_on_message_write_completed_fn *on_completion, |
| 242 |
void *user_data) { |
| 243 |
|
| 244 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 245 |
|
| 246 |
if (!handler_impl->event_loop_driven || aws_channel_thread_is_callers_thread(slot->channel)) { |
| 247 |
s_rw_handler_write_now(slot, buffer, on_completion, user_data); |
| 248 |
} else { |
| 249 |
struct rw_handler_write_task_args *write_task_args = |
| 250 |
aws_mem_acquire(handler->alloc, sizeof(struct rw_handler_write_task_args)); |
| 251 |
write_task_args->handler = handler; |
| 252 |
write_task_args->buffer = buffer; |
| 253 |
write_task_args->slot = slot; |
| 254 |
write_task_args->on_completion = on_completion; |
| 255 |
write_task_args->user_data = user_data; |
| 256 |
aws_channel_task_init(&write_task_args->task, s_rw_handler_write_task, write_task_args, "rw_handler_write"); |
| 257 |
|
| 258 |
aws_channel_schedule_task_now(slot->channel, &write_task_args->task); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
struct increment_read_window_task_args { |
| 263 |
size_t window_update; |
| 264 |
struct aws_channel_handler *handler; |
| 265 |
struct aws_channel_slot *slot; |
| 266 |
struct aws_channel_task task; |
| 267 |
}; |
| 268 |
|
| 269 |
static void s_increment_read_window_task(struct aws_channel_task *task, void *arg, enum aws_task_status task_status) { |
| 270 |
(void)task; |
| 271 |
(void)task_status; |
| 272 |
struct increment_read_window_task_args *increment_read_window_task_args = arg; |
| 273 |
struct rw_test_handler_impl *handler_impl = increment_read_window_task_args->handler->impl; |
| 274 |
|
| 275 |
handler_impl->window += increment_read_window_task_args->window_update; |
| 276 |
aws_channel_slot_increment_read_window( |
| 277 |
increment_read_window_task_args->slot, increment_read_window_task_args->window_update); |
| 278 |
|
| 279 |
aws_mem_release(increment_read_window_task_args->handler->alloc, increment_read_window_task_args); |
| 280 |
} |
| 281 |
|
| 282 |
void rw_handler_trigger_increment_read_window( |
| 283 |
struct aws_channel_handler *handler, |
| 284 |
struct aws_channel_slot *slot, |
| 285 |
size_t window_update) { |
| 286 |
|
| 287 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 288 |
|
| 289 |
if (!handler_impl->event_loop_driven || aws_channel_thread_is_callers_thread(slot->channel)) { |
| 290 |
handler_impl->window += window_update; |
| 291 |
aws_channel_slot_increment_read_window(slot, window_update); |
| 292 |
} else { |
| 293 |
struct increment_read_window_task_args *increment_read_window_task_args = |
| 294 |
aws_mem_acquire(handler->alloc, sizeof(struct increment_read_window_task_args)); |
| 295 |
increment_read_window_task_args->handler = handler; |
| 296 |
increment_read_window_task_args->window_update = window_update; |
| 297 |
increment_read_window_task_args->slot = slot; |
| 298 |
aws_channel_task_init( |
| 299 |
&increment_read_window_task_args->task, |
| 300 |
s_increment_read_window_task, |
| 301 |
increment_read_window_task_args, |
| 302 |
"increment_read_window_task"); |
| 303 |
|
| 304 |
aws_channel_schedule_task_now(slot->channel, &increment_read_window_task_args->task); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
bool rw_handler_shutdown_called(struct aws_channel_handler *handler) { |
| 309 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 310 |
return aws_atomic_load_int(&handler_impl->shutdown_called); |
| 311 |
} |
| 312 |
|
| 313 |
bool rw_handler_increment_read_window_called(struct aws_channel_handler *handler) { |
| 314 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 315 |
return handler_impl->increment_read_window_called; |
| 316 |
} |
| 317 |
|
| 318 |
int rw_handler_last_error_code(struct aws_channel_handler *handler) { |
| 319 |
struct rw_test_handler_impl *handler_impl = handler->impl; |
| 320 |
return aws_atomic_load_int(&handler_impl->shutdown_error); |
| 321 |
} |