| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <aws/event-stream/event_stream.h> |
| 17 |
#include <aws/event-stream/event_stream_channel_handler.h> |
| 18 |
|
| 19 |
#include <aws/checksums/crc.h> |
| 20 |
|
| 21 |
#include <aws/io/channel.h> |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
|
| 25 |
static const size_t s_default_payload_size = 1024; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static const size_t s_message_overhead_size = AWS_EVENT_STREAM_PRELUDE_LENGTH + AWS_EVENT_STREAM_TRAILER_LENGTH; |
| 35 |
|
| 36 |
struct aws_event_stream_channel_handler { |
| 37 |
struct aws_channel_handler handler; |
| 38 |
struct aws_byte_buf message_buf; |
| 39 |
uint32_t running_crc; |
| 40 |
uint32_t current_message_len; |
| 41 |
aws_event_stream_channel_handler_on_message_received_fn *on_message_received; |
| 42 |
void *user_data; |
| 43 |
size_t initial_window_size; |
| 44 |
bool manual_window_management; |
| 45 |
}; |
| 46 |
|
| 47 |
static int s_process_read_message( |
| 48 |
struct aws_channel_handler *handler, |
| 49 |
struct aws_channel_slot *slot, |
| 50 |
struct aws_io_message *message) { |
| 51 |
|
| 52 |
AWS_LOGF_TRACE( |
| 53 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 54 |
"id=%p: received message of size %zu", |
| 55 |
(void *)handler, |
| 56 |
message->message_data.len); |
| 57 |
struct aws_event_stream_channel_handler *event_stream_handler = handler->impl; |
| 58 |
|
| 59 |
struct aws_byte_cursor message_cursor = aws_byte_cursor_from_buf(&message->message_data); |
| 60 |
|
| 61 |
int error_code = AWS_ERROR_SUCCESS; |
| 62 |
while (message_cursor.len) { |
| 63 |
AWS_LOGF_TRACE( |
| 64 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 65 |
"id=%p: processing chunk of size %zu", |
| 66 |
(void *)handler, |
| 67 |
message_cursor.len); |
| 68 |
|
| 69 |
|
| 70 |
if (event_stream_handler->message_buf.len < AWS_EVENT_STREAM_PRELUDE_LENGTH) { |
| 71 |
size_t remaining_prelude = AWS_EVENT_STREAM_PRELUDE_LENGTH - event_stream_handler->message_buf.len; |
| 72 |
size_t to_copy = aws_min_size(message_cursor.len, remaining_prelude); |
| 73 |
AWS_LOGF_TRACE( |
| 74 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 75 |
"id=%p: processing prelude, %zu bytes of an expected 12.", |
| 76 |
(void *)handler, |
| 77 |
to_copy); |
| 78 |
|
| 79 |
if (!aws_byte_buf_write(&event_stream_handler->message_buf, message_cursor.ptr, to_copy)) { |
| 80 |
error_code = aws_last_error(); |
| 81 |
AWS_LOGF_ERROR( |
| 82 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 83 |
"id=%p: writing to prelude buffer failed with error %s", |
| 84 |
(void *)handler, |
| 85 |
aws_error_debug_str(error_code)); |
| 86 |
goto finished; |
| 87 |
} |
| 88 |
|
| 89 |
aws_byte_cursor_advance(&message_cursor, to_copy); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (event_stream_handler->message_buf.len == AWS_EVENT_STREAM_PRELUDE_LENGTH) { |
| 95 |
AWS_LOGF_TRACE(AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "id=%p: processing prelude buffer", (void *)handler); |
| 96 |
|
| 97 |
struct aws_byte_cursor prelude_cursor = aws_byte_cursor_from_buf(&event_stream_handler->message_buf); |
| 98 |
|
| 99 |
event_stream_handler->running_crc = |
| 100 |
aws_checksums_crc32(prelude_cursor.ptr, sizeof(uint32_t) + sizeof(uint32_t), 0); |
| 101 |
AWS_LOGF_DEBUG( |
| 102 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 103 |
"id=%p: calculated prelude CRC of %" PRIu32, |
| 104 |
(void *)handler, |
| 105 |
event_stream_handler->running_crc); |
| 106 |
|
| 107 |
aws_byte_cursor_read_be32(&prelude_cursor, &event_stream_handler->current_message_len); |
| 108 |
|
| 109 |
AWS_LOGF_DEBUG( |
| 110 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 111 |
"id=%p: read total message length of %" PRIu32, |
| 112 |
(void *)handler, |
| 113 |
event_stream_handler->current_message_len); |
| 114 |
|
| 115 |
if (event_stream_handler->current_message_len > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE) { |
| 116 |
AWS_LOGF_ERROR( |
| 117 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 118 |
"id=%p: message length of %" PRIu32 " exceeds the max size of %zu", |
| 119 |
(void *)handler, |
| 120 |
event_stream_handler->current_message_len, |
| 121 |
(size_t)AWS_EVENT_STREAM_MAX_MESSAGE_SIZE); |
| 122 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 123 |
error_code = aws_last_error(); |
| 124 |
goto finished; |
| 125 |
} |
| 126 |
|
| 127 |
if (event_stream_handler->current_message_len < |
| 128 |
AWS_EVENT_STREAM_PRELUDE_LENGTH + AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 129 |
AWS_LOGF_ERROR( |
| 130 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 131 |
"id=%p: message length of %" PRIu32 " is invalid and too small", |
| 132 |
(void *)handler, |
| 133 |
event_stream_handler->current_message_len); |
| 134 |
|
| 135 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 136 |
error_code = aws_last_error(); |
| 137 |
goto finished; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
aws_byte_cursor_advance(&prelude_cursor, sizeof(uint32_t)); |
| 142 |
|
| 143 |
uint32_t prelude_crc = 0; |
| 144 |
aws_byte_cursor_read_be32(&prelude_cursor, &prelude_crc); |
| 145 |
AWS_LOGF_DEBUG( |
| 146 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 147 |
"id=%p: read prelude CRC of %" PRIu32, |
| 148 |
(void *)handler, |
| 149 |
prelude_crc); |
| 150 |
|
| 151 |
|
| 152 |
if (event_stream_handler->running_crc != prelude_crc) { |
| 153 |
AWS_LOGF_ERROR( |
| 154 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 155 |
"id=%p: prelude CRC mismatch. calculated %" PRIu32 " but the crc for the message was %" PRIu32, |
| 156 |
(void *)handler, |
| 157 |
event_stream_handler->running_crc, |
| 158 |
prelude_crc); |
| 159 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE); |
| 160 |
error_code = aws_last_error(); |
| 161 |
goto finished; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if (event_stream_handler->message_buf.len < event_stream_handler->current_message_len) { |
| 167 |
AWS_LOGF_TRACE( |
| 168 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "id=%p: processing remaining message buffer", (void *)handler); |
| 169 |
size_t remaining = event_stream_handler->current_message_len - event_stream_handler->message_buf.len; |
| 170 |
size_t to_copy = aws_min_size(message_cursor.len, remaining); |
| 171 |
AWS_LOGF_TRACE( |
| 172 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 173 |
"id=%p: of the remaining %zu, processing %zu from the " |
| 174 |
"current message.", |
| 175 |
(void *)handler, |
| 176 |
remaining, |
| 177 |
to_copy); |
| 178 |
|
| 179 |
struct aws_byte_cursor to_append = aws_byte_cursor_advance(&message_cursor, to_copy); |
| 180 |
if (aws_byte_buf_append_dynamic(&event_stream_handler->message_buf, &to_append)) { |
| 181 |
error_code = aws_last_error(); |
| 182 |
AWS_LOGF_ERROR( |
| 183 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 184 |
"id=%p: Appending to the message buffer failed with error %s.", |
| 185 |
(void *)handler, |
| 186 |
aws_error_debug_str(error_code)); |
| 187 |
|
| 188 |
goto finished; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if (event_stream_handler->message_buf.len == event_stream_handler->current_message_len) { |
| 195 |
AWS_LOGF_TRACE( |
| 196 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 197 |
"id=%p: An entire message has been read. Parsing the message now.", |
| 198 |
(void *)handler); |
| 199 |
struct aws_event_stream_message received_message; |
| 200 |
AWS_ZERO_STRUCT(received_message); |
| 201 |
|
| 202 |
if (aws_event_stream_message_from_buffer( |
| 203 |
&received_message, event_stream_handler->handler.alloc, &event_stream_handler->message_buf)) { |
| 204 |
error_code = aws_last_error(); |
| 205 |
AWS_LOGF_ERROR( |
| 206 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 207 |
"id=%p: Parsing the message failed with error %s.", |
| 208 |
(void *)handler, |
| 209 |
aws_error_debug_str(error_code)); |
| 210 |
goto finished; |
| 211 |
} |
| 212 |
|
| 213 |
size_t message_size = event_stream_handler->message_buf.len; |
| 214 |
AWS_LOGF_TRACE( |
| 215 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "id=%p: Invoking on_message_received callback.", (void *)handler); |
| 216 |
event_stream_handler->on_message_received( |
| 217 |
&received_message, AWS_ERROR_SUCCESS, event_stream_handler->user_data); |
| 218 |
aws_event_stream_message_clean_up(&received_message); |
| 219 |
event_stream_handler->current_message_len = 0; |
| 220 |
event_stream_handler->running_crc = 0; |
| 221 |
aws_byte_buf_reset(&event_stream_handler->message_buf, true); |
| 222 |
|
| 223 |
if (!event_stream_handler->manual_window_management) { |
| 224 |
aws_channel_slot_increment_read_window(slot, message_size); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
finished: |
| 230 |
if (error_code) { |
| 231 |
event_stream_handler->on_message_received(NULL, error_code, event_stream_handler->user_data); |
| 232 |
aws_channel_shutdown(slot->channel, error_code); |
| 233 |
} |
| 234 |
aws_mem_release(message->allocator, message); |
| 235 |
return AWS_OP_SUCCESS; |
| 236 |
} |
| 237 |
|
| 238 |
struct message_write_data { |
| 239 |
struct aws_allocator *allocator; |
| 240 |
struct aws_channel_task task; |
| 241 |
struct aws_event_stream_channel_handler *handler; |
| 242 |
struct aws_event_stream_message *message; |
| 243 |
aws_event_stream_channel_handler_on_message_written_fn *on_message_written; |
| 244 |
void *user_data; |
| 245 |
}; |
| 246 |
|
| 247 |
static void s_on_message_write_completed_fn( |
| 248 |
struct aws_channel *channel, |
| 249 |
struct aws_io_message *message, |
| 250 |
int err_code, |
| 251 |
void *user_data) { |
| 252 |
(void)channel; |
| 253 |
(void)message; |
| 254 |
|
| 255 |
struct message_write_data *message_data = user_data; |
| 256 |
AWS_LOGF_TRACE( |
| 257 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 258 |
"channel=%p: Message write completed. Invoking " |
| 259 |
"on_message_written callback.", |
| 260 |
(void *)channel); |
| 261 |
message_data->on_message_written(message_data->message, err_code, message_data->user_data); |
| 262 |
aws_mem_release(message_data->allocator, message_data); |
| 263 |
} |
| 264 |
|
| 265 |
static void s_write_handler_message(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 266 |
(void)task; |
| 267 |
|
| 268 |
struct message_write_data *message_data = arg; |
| 269 |
|
| 270 |
AWS_LOGF_TRACE(AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "static: Write message task invoked."); |
| 271 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 272 |
struct aws_event_stream_message *message = message_data->message; |
| 273 |
struct aws_event_stream_channel_handler *handler = message_data->handler; |
| 274 |
|
| 275 |
struct aws_byte_cursor message_cur = aws_byte_cursor_from_array( |
| 276 |
aws_event_stream_message_buffer(message), aws_event_stream_message_total_length(message)); |
| 277 |
|
| 278 |
while (message_cur.len) { |
| 279 |
AWS_LOGF_TRACE( |
| 280 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 281 |
"id=%p: writing message chunk of size %zu.", |
| 282 |
(void *)&handler->handler, |
| 283 |
message_cur.len); |
| 284 |
|
| 285 |
|
| 286 |
struct aws_io_message *io_message = aws_channel_acquire_message_from_pool( |
| 287 |
handler->handler.slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, message_cur.len); |
| 288 |
|
| 289 |
if (!io_message) { |
| 290 |
int error_code = aws_last_error(); |
| 291 |
AWS_LOGF_ERROR( |
| 292 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 293 |
"id=%p: Error occurred while acquiring io message %s.", |
| 294 |
(void *)&handler->handler, |
| 295 |
aws_error_debug_str(error_code)); |
| 296 |
|
| 297 |
message_data->on_message_written(message, error_code, message_data->user_data); |
| 298 |
aws_mem_release(message_data->allocator, message_data); |
| 299 |
aws_channel_shutdown(handler->handler.slot->channel, error_code); |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
aws_byte_buf_write_to_capacity(&io_message->message_data, &message_cur); |
| 304 |
|
| 305 |
|
| 306 |
if (message_cur.len == 0) { |
| 307 |
AWS_LOGF_TRACE( |
| 308 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 309 |
"id=%p: Message completely written to all io buffers.", |
| 310 |
(void *)&handler->handler); |
| 311 |
io_message->on_completion = s_on_message_write_completed_fn; |
| 312 |
io_message->user_data = message_data; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if (aws_channel_slot_send_message(handler->handler.slot, io_message, AWS_CHANNEL_DIR_WRITE)) { |
| 318 |
aws_mem_release(io_message->allocator, io_message); |
| 319 |
int error_code = aws_last_error(); |
| 320 |
AWS_LOGF_ERROR( |
| 321 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 322 |
"id=%p: Error occurred while sending message to channel %s.", |
| 323 |
(void *)&handler->handler, |
| 324 |
aws_error_debug_str(error_code)); |
| 325 |
message_data->on_message_written(message, error_code, message_data->user_data); |
| 326 |
aws_mem_release(message_data->allocator, message_data); |
| 327 |
aws_channel_shutdown(handler->handler.slot->channel, error_code); |
| 328 |
break; |
| 329 |
} |
| 330 |
AWS_LOGF_TRACE( |
| 331 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "id=%p: Message sent to channel", (void *)&handler->handler); |
| 332 |
} |
| 333 |
} else { |
| 334 |
AWS_LOGF_WARN(AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "static: Channel was shutdown. Message not sent"); |
| 335 |
message_data->on_message_written( |
| 336 |
message_data->message, AWS_ERROR_IO_OPERATION_CANCELLED, message_data->user_data); |
| 337 |
aws_mem_release(message_data->allocator, message_data); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
int aws_event_stream_channel_handler_write_message( |
| 342 |
struct aws_channel_handler *channel_handler, |
| 343 |
struct aws_event_stream_message *message, |
| 344 |
aws_event_stream_channel_handler_on_message_written_fn *on_message_written, |
| 345 |
void *user_data) { |
| 346 |
AWS_PRECONDITION(channel_handler); |
| 347 |
AWS_PRECONDITION(message); |
| 348 |
AWS_PRECONDITION(on_message_written); |
| 349 |
|
| 350 |
struct aws_event_stream_channel_handler *handler = channel_handler->impl; |
| 351 |
|
| 352 |
struct message_write_data *write_data = |
| 353 |
aws_mem_calloc(handler->handler.alloc, 1, sizeof(struct message_write_data)); |
| 354 |
|
| 355 |
if (!write_data) { |
| 356 |
AWS_LOGF_ERROR( |
| 357 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 358 |
"id=%p: Error occurred while allocating callback data %s.", |
| 359 |
(void *)channel_handler, |
| 360 |
aws_error_debug_str(aws_last_error())); |
| 361 |
aws_channel_shutdown(channel_handler->slot->channel, aws_last_error()); |
| 362 |
return AWS_OP_ERR; |
| 363 |
} |
| 364 |
|
| 365 |
write_data->handler = handler; |
| 366 |
write_data->user_data = user_data; |
| 367 |
write_data->message = message; |
| 368 |
write_data->on_message_written = on_message_written; |
| 369 |
write_data->allocator = handler->handler.alloc; |
| 370 |
|
| 371 |
AWS_LOGF_TRACE( |
| 372 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "id=%p: Scheduling message write task", (void *)channel_handler); |
| 373 |
aws_channel_task_init( |
| 374 |
&write_data->task, s_write_handler_message, write_data, "aws_event_stream_channel_handler_write_message"); |
| 375 |
aws_channel_schedule_task_now_serialized(handler->handler.slot->channel, &write_data->task); |
| 376 |
|
| 377 |
return AWS_OP_SUCCESS; |
| 378 |
} |
| 379 |
|
| 380 |
void *aws_event_stream_channel_handler_get_user_data(struct aws_channel_handler *channel_handler) { |
| 381 |
struct aws_event_stream_channel_handler *handler = channel_handler->impl; |
| 382 |
return handler->user_data; |
| 383 |
} |
| 384 |
|
| 385 |
struct window_update_data { |
| 386 |
struct aws_allocator *allocator; |
| 387 |
struct aws_channel_task task; |
| 388 |
struct aws_event_stream_channel_handler *handler; |
| 389 |
size_t window_update_size; |
| 390 |
}; |
| 391 |
|
| 392 |
static void s_update_window_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 393 |
(void)task; |
| 394 |
struct window_update_data *update_data = arg; |
| 395 |
|
| 396 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 397 |
AWS_LOGF_DEBUG( |
| 398 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 399 |
"static: updating window. increment of %zu", |
| 400 |
update_data->window_update_size); |
| 401 |
aws_channel_slot_increment_read_window(update_data->handler->handler.slot, update_data->window_update_size); |
| 402 |
} |
| 403 |
|
| 404 |
aws_mem_release(update_data->allocator, update_data); |
| 405 |
} |
| 406 |
|
| 407 |
void aws_event_stream_channel_handler_increment_read_window( |
| 408 |
struct aws_channel_handler *channel_handler, |
| 409 |
size_t window_update_size) { |
| 410 |
AWS_PRECONDITION(channel_handler); |
| 411 |
|
| 412 |
struct aws_event_stream_channel_handler *handler = channel_handler->impl; |
| 413 |
|
| 414 |
if (!handler->manual_window_management) { |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
AWS_LOGF_DEBUG( |
| 419 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 420 |
"id=%p: A user requested window update and manual window management is specified. Updating size of %zu", |
| 421 |
(void *)channel_handler, |
| 422 |
window_update_size); |
| 423 |
|
| 424 |
if (aws_channel_thread_is_callers_thread(handler->handler.slot->channel)) { |
| 425 |
if (aws_channel_slot_increment_read_window(handler->handler.slot, window_update_size)) { |
| 426 |
aws_channel_shutdown(handler->handler.slot->channel, aws_last_error()); |
| 427 |
return; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
struct window_update_data *update_data = |
| 432 |
aws_mem_calloc(handler->handler.alloc, 1, sizeof(struct window_update_data)); |
| 433 |
|
| 434 |
if (!update_data) { |
| 435 |
AWS_LOGF_ERROR( |
| 436 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 437 |
"id=%p: Error occurred while allocating update window data %s.", |
| 438 |
(void *)channel_handler, |
| 439 |
aws_error_debug_str(aws_last_error())); |
| 440 |
aws_channel_shutdown(handler->handler.slot->channel, aws_last_error()); |
| 441 |
return; |
| 442 |
} |
| 443 |
|
| 444 |
update_data->allocator = handler->handler.alloc; |
| 445 |
update_data->handler = handler; |
| 446 |
update_data->window_update_size = window_update_size; |
| 447 |
|
| 448 |
aws_channel_task_init( |
| 449 |
&update_data->task, |
| 450 |
s_update_window_task, |
| 451 |
update_data, |
| 452 |
"aws_event_stream_channel_handler_increment_read_window"); |
| 453 |
aws_channel_schedule_task_now(handler->handler.slot->channel, &update_data->task); |
| 454 |
} |
| 455 |
|
| 456 |
static int s_process_write_message( |
| 457 |
struct aws_channel_handler *handler, |
| 458 |
struct aws_channel_slot *slot, |
| 459 |
struct aws_io_message *message) { |
| 460 |
(void)handler; |
| 461 |
(void)slot; |
| 462 |
(void)message; |
| 463 |
AWS_FATAL_ASSERT(!"The event-stream-channel-handler is not designed to be a mid-channel handler."); |
| 464 |
return aws_raise_error(AWS_ERROR_UNIMPLEMENTED); |
| 465 |
} |
| 466 |
|
| 467 |
static int s_increment_read_window(struct aws_channel_handler *handler, struct aws_channel_slot *slot, size_t size) { |
| 468 |
(void)handler; |
| 469 |
return aws_channel_slot_increment_read_window(slot, size); |
| 470 |
} |
| 471 |
|
| 472 |
static size_t s_initial_window_size(struct aws_channel_handler *handler) { |
| 473 |
struct aws_event_stream_channel_handler *message_handler = handler->impl; |
| 474 |
return message_handler->initial_window_size; |
| 475 |
} |
| 476 |
|
| 477 |
static size_t s_message_overhead(struct aws_channel_handler *handler) { |
| 478 |
(void)handler; |
| 479 |
return s_message_overhead_size; |
| 480 |
} |
| 481 |
|
| 482 |
static void s_destroy(struct aws_channel_handler *handler) { |
| 483 |
AWS_LOGF_DEBUG( |
| 484 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 485 |
"id=%p: destroying event-stream message channel handler.", |
| 486 |
(void *)handler); |
| 487 |
struct aws_event_stream_channel_handler *event_stream_handler = handler->impl; |
| 488 |
aws_byte_buf_clean_up(&event_stream_handler->message_buf); |
| 489 |
aws_mem_release(handler->alloc, event_stream_handler); |
| 490 |
} |
| 491 |
|
| 492 |
static int s_shutdown( |
| 493 |
struct aws_channel_handler *handler, |
| 494 |
struct aws_channel_slot *slot, |
| 495 |
enum aws_channel_direction dir, |
| 496 |
int error_code, |
| 497 |
bool free_scarce_resources_immediately) { |
| 498 |
(void)handler; |
| 499 |
AWS_LOGF_DEBUG( |
| 500 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 501 |
"id=%p: shutdown called on event-stream channel handler with error %s.", |
| 502 |
(void *)handler, |
| 503 |
aws_error_debug_str(error_code)); |
| 504 |
|
| 505 |
return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, free_scarce_resources_immediately); |
| 506 |
} |
| 507 |
|
| 508 |
static struct aws_channel_handler_vtable vtable = { |
| 509 |
.destroy = s_destroy, |
| 510 |
.increment_read_window = s_increment_read_window, |
| 511 |
.initial_window_size = s_initial_window_size, |
| 512 |
.process_read_message = s_process_read_message, |
| 513 |
.process_write_message = s_process_write_message, |
| 514 |
.message_overhead = s_message_overhead, |
| 515 |
.shutdown = s_shutdown, |
| 516 |
}; |
| 517 |
|
| 518 |
struct aws_channel_handler *aws_event_stream_channel_handler_new( |
| 519 |
struct aws_allocator *allocator, |
| 520 |
const struct aws_event_stream_channel_handler_options *handler_options) { |
| 521 |
AWS_PRECONDITION(allocator); |
| 522 |
AWS_PRECONDITION(handler_options); |
| 523 |
AWS_PRECONDITION(handler_options->on_message_received); |
| 524 |
|
| 525 |
AWS_LOGF_INFO(AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, "static: creating new event-stream message channel handler."); |
| 526 |
|
| 527 |
struct aws_event_stream_channel_handler *event_stream_handler = |
| 528 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_event_stream_channel_handler)); |
| 529 |
|
| 530 |
if (!event_stream_handler) { |
| 531 |
AWS_LOGF_ERROR( |
| 532 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 533 |
"static: Error occurred while allocating handler %s.", |
| 534 |
aws_error_debug_str(aws_last_error())); |
| 535 |
return NULL; |
| 536 |
} |
| 537 |
|
| 538 |
AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_CLIENT, "static: new handler is %p", (void *)&event_stream_handler->handler); |
| 539 |
|
| 540 |
if (aws_byte_buf_init( |
| 541 |
&event_stream_handler->message_buf, allocator, s_default_payload_size + s_message_overhead_size)) { |
| 542 |
AWS_LOGF_ERROR( |
| 543 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 544 |
"id=%p: Error occurred while allocating scratch buffer %s.", |
| 545 |
(void *)&event_stream_handler->handler, |
| 546 |
aws_error_debug_str(aws_last_error())); |
| 547 |
aws_mem_release(allocator, event_stream_handler); |
| 548 |
return NULL; |
| 549 |
} |
| 550 |
|
| 551 |
event_stream_handler->on_message_received = handler_options->on_message_received; |
| 552 |
event_stream_handler->user_data = handler_options->user_data; |
| 553 |
event_stream_handler->initial_window_size = |
| 554 |
handler_options->initial_window_size > 0 ? handler_options->initial_window_size : SIZE_MAX; |
| 555 |
event_stream_handler->manual_window_management = handler_options->manual_window_management; |
| 556 |
event_stream_handler->handler.vtable = &vtable; |
| 557 |
event_stream_handler->handler.alloc = allocator; |
| 558 |
event_stream_handler->handler.impl = event_stream_handler; |
| 559 |
|
| 560 |
return &event_stream_handler->handler; |
| 561 |
} |