static void s_process_received_data()

in source/secure_tunneling.c [301:332]


static void s_process_received_data(struct aws_secure_tunnel *secure_tunnel) {
    struct aws_byte_buf *received_data = &secure_tunnel->received_data;
    struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(received_data);

    uint16_t data_length = 0;
    struct aws_byte_cursor tmp_cursor =
        cursor; /* If there are at least two bytes for the data_length, but not enough      */
                /* data for a complete secure tunnel frame, we don't want to move `cursor`. */
    while (aws_byte_cursor_read_be16(&tmp_cursor, &data_length) && tmp_cursor.len >= data_length) {
        cursor = tmp_cursor;

        struct aws_byte_cursor st_frame = {.len = data_length, .ptr = cursor.ptr};
        aws_byte_cursor_advance(&cursor, data_length);
        tmp_cursor = cursor;

        struct aws_iot_st_msg st_msg;
        aws_iot_st_msg_deserialize_from_cursor(&st_msg, &st_frame, secure_tunnel->alloc);
        s_process_iot_st_msg(secure_tunnel, &st_msg);

        if (st_msg.type == DATA) {
            aws_byte_buf_clean_up(&st_msg.payload);
        }
    }

    if (cursor.ptr != received_data->buffer) {
        /* TODO: Consider better data structure that doesn't require moving bytes */

        /* Move unprocessed data to the beginning */
        received_data->len = 0;
        aws_byte_buf_append(received_data, &cursor);
    }
}