| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/async_stream.h> |
| 7 |
|
| 8 |
#include <aws/common/byte_buf.h> |
| 9 |
#include <aws/io/future.h> |
| 10 |
#include <aws/io/stream.h> |
| 11 |
|
| 12 |
void aws_async_input_stream_init_base( |
| 13 |
struct aws_async_input_stream *stream, |
| 14 |
struct aws_allocator *alloc, |
| 15 |
const struct aws_async_input_stream_vtable *vtable, |
| 16 |
void *impl) { |
| 17 |
|
| 18 |
AWS_PRECONDITION(stream); |
| 19 |
AWS_PRECONDITION(alloc); |
| 20 |
AWS_PRECONDITION(vtable); |
| 21 |
AWS_PRECONDITION(vtable->read); |
| 22 |
AWS_PRECONDITION(vtable->destroy); |
| 23 |
|
| 24 |
AWS_ZERO_STRUCT(*stream); |
| 25 |
stream->alloc = alloc; |
| 26 |
stream->vtable = vtable; |
| 27 |
stream->impl = impl; |
| 28 |
aws_ref_count_init(&stream->ref_count, stream, (aws_simple_completion_callback *)vtable->destroy); |
| 29 |
} |
| 30 |
|
| 31 |
struct aws_async_input_stream *aws_async_input_stream_acquire(struct aws_async_input_stream *stream) { |
| 32 |
if (stream != NULL) { |
| 33 |
aws_ref_count_acquire(&stream->ref_count); |
| 34 |
} |
| 35 |
return stream; |
| 36 |
} |
| 37 |
|
| 38 |
struct aws_async_input_stream *aws_async_input_stream_release(struct aws_async_input_stream *stream) { |
| 39 |
if (stream) { |
| 40 |
aws_ref_count_release(&stream->ref_count); |
| 41 |
} |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
struct aws_future_bool *aws_async_input_stream_read(struct aws_async_input_stream *stream, struct aws_byte_buf *dest) { |
| 46 |
AWS_PRECONDITION(stream); |
| 47 |
AWS_PRECONDITION(dest); |
| 48 |
|
| 49 |
|
| 50 |
if (dest->len == dest->capacity) { |
| 51 |
struct aws_future_bool *future = aws_future_bool_new(stream->alloc); |
| 52 |
aws_future_bool_set_error(future, AWS_ERROR_SHORT_BUFFER); |
| 53 |
return future; |
| 54 |
} |
| 55 |
|
| 56 |
struct aws_future_bool *future = stream->vtable->read(stream, dest); |
| 57 |
AWS_POSTCONDITION(future != NULL); |
| 58 |
return future; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
struct aws_async_input_stream_fill_job { |
| 63 |
struct aws_allocator *alloc; |
| 64 |
struct aws_async_input_stream *stream; |
| 65 |
struct aws_byte_buf *dest; |
| 66 |
|
| 67 |
struct aws_future_bool *read_step_future; |
| 68 |
|
| 69 |
struct aws_future_bool *on_complete_future; |
| 70 |
}; |
| 71 |
|
| 72 |
static void s_async_stream_fill_job_complete( |
| 73 |
struct aws_async_input_stream_fill_job *fill_job, |
| 74 |
bool eof, |
| 75 |
int error_code) { |
| 76 |
|
| 77 |
if (error_code) { |
| 78 |
aws_future_bool_set_error(fill_job->on_complete_future, error_code); |
| 79 |
} else { |
| 80 |
aws_future_bool_set_result(fill_job->on_complete_future, eof); |
| 81 |
} |
| 82 |
aws_future_bool_release(fill_job->on_complete_future); |
| 83 |
aws_async_input_stream_release(fill_job->stream); |
| 84 |
aws_mem_release(fill_job->alloc, fill_job); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static void s_async_stream_fill_job_loop(void *user_data) { |
| 93 |
struct aws_async_input_stream_fill_job *fill_job = user_data; |
| 94 |
|
| 95 |
while (true) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if (fill_job->read_step_future) { |
| 101 |
if (aws_future_bool_register_callback_if_not_done( |
| 102 |
fill_job->read_step_future, s_async_stream_fill_job_loop, fill_job)) { |
| 103 |
|
| 104 |
|
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int error_code = aws_future_bool_get_error(fill_job->read_step_future); |
| 110 |
bool eof = error_code ? false : aws_future_bool_get_result(fill_job->read_step_future); |
| 111 |
bool reached_capacity = fill_job->dest->len == fill_job->dest->capacity; |
| 112 |
fill_job->read_step_future = aws_future_bool_release(fill_job->read_step_future); |
| 113 |
|
| 114 |
if (error_code || eof || reached_capacity) { |
| 115 |
|
| 116 |
s_async_stream_fill_job_complete(fill_job, eof, error_code); |
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
fill_job->read_step_future = aws_async_input_stream_read(fill_job->stream, fill_job->dest); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
struct aws_future_bool *aws_async_input_stream_read_to_fill( |
| 127 |
struct aws_async_input_stream *stream, |
| 128 |
struct aws_byte_buf *dest) { |
| 129 |
|
| 130 |
AWS_PRECONDITION(stream); |
| 131 |
AWS_PRECONDITION(dest); |
| 132 |
|
| 133 |
struct aws_future_bool *future = aws_future_bool_new(stream->alloc); |
| 134 |
|
| 135 |
|
| 136 |
if (dest->len == dest->capacity) { |
| 137 |
aws_future_bool_set_error(future, AWS_ERROR_SHORT_BUFFER); |
| 138 |
return future; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
struct aws_async_input_stream_fill_job *fill_job = |
| 143 |
aws_mem_calloc(stream->alloc, 1, sizeof(struct aws_async_input_stream_fill_job)); |
| 144 |
fill_job->alloc = stream->alloc; |
| 145 |
fill_job->stream = aws_async_input_stream_acquire(stream); |
| 146 |
fill_job->dest = dest; |
| 147 |
fill_job->on_complete_future = aws_future_bool_acquire(future); |
| 148 |
|
| 149 |
|
| 150 |
s_async_stream_fill_job_loop(fill_job); |
| 151 |
|
| 152 |
return future; |
| 153 |
} |