| 1 | /** |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0. |
| 4 | */ |
| 5 | |
| 6 | #ifndef AWS_IO_ASYNC_STREAM_H |
| 7 | #define AWS_IO_ASYNC_STREAM_H |
| 8 | |
| 9 | /** |
| 10 | * THIS IS AN EXPERIMENTAL AND UNSTABLE API |
| 11 | * TODO: logging |
| 12 | * TODO: modify API to return byte-bufs, instead of filling in the provided byte-buf? |
| 13 | * this would avoid a copy in the use-cases we know of, but it's more complex |
| 14 | * TODO: vtable acquire()/release()? |
| 15 | * TODO: protect against simultaneous reads? |
| 16 | * TODO: check results of vtable->read() (i.e. 0 byte reads not allowed)? |
| 17 | * this would require 1 or 2 additional allocations per read |
| 18 | */ |
| 19 | |
| 20 | #include <aws/io/io.h> |
| 21 | |
| 22 | #include <aws/common/ref_count.h> |
| 23 | |
| 24 | AWS_PUSH_SANE_WARNING_LEVEL |
| 25 | |
| 26 | struct aws_async_input_stream; |
| 27 | struct aws_byte_buf; |
| 28 | struct aws_future_bool; |
| 29 | struct aws_input_stream; |
| 30 | |
| 31 | struct aws_async_input_stream { |
| 32 | const struct aws_async_input_stream_vtable *vtable; |
| 33 | struct aws_allocator *alloc; |
| 34 | struct aws_ref_count ref_count; |
| 35 | void *impl; |
| 36 | }; |
| 37 | |
| 38 | struct aws_async_input_stream_vtable { |
| 39 | /** |
| 40 | * Destroy the stream, its refcount has reached 0. |
| 41 | */ |
| 42 | void (*destroy)(struct aws_async_input_stream *stream); |
| 43 | |
| 44 | /** |
| 45 | * Read once into the buffer. |
| 46 | * Complete the read when at least 1 byte is read, the buffer is full, or EOF is reached. |
| 47 | * Do not resize the buffer (do not use "aws_byte_buf_xyz_dynamic()" functions) |
| 48 | * Do not assume that buffer len starts at 0. |
| 49 | * You may assume that read() won't be called again until the current one completes. |
| 50 | * You may assume that the buffer has some space available. |
| 51 | * Return a future, which will contain an error code if something went wrong, |
| 52 | * or a result bool indicating whether EOF has been reached. |
| 53 | */ |
| 54 | struct aws_future_bool *(*read)(struct aws_async_input_stream *stream, struct aws_byte_buf *dest); |
| 55 | }; |
| 56 | |
| 57 | AWS_EXTERN_C_BEGIN |
| 58 | |
| 59 | /** |
| 60 | * Initialize aws_async_input_stream "base class" |
| 61 | */ |
| 62 | AWS_IO_API |
| 63 | void aws_async_input_stream_init_base( |
| 64 | struct aws_async_input_stream *stream, |
| 65 | struct aws_allocator *alloc, |
| 66 | const struct aws_async_input_stream_vtable *vtable, |
| 67 | void *impl); |
| 68 | |
| 69 | /** |
| 70 | * Increment reference count. |
| 71 | * You may pass in NULL (has no effect). |
| 72 | * Returns whatever pointer was passed in. |
| 73 | */ |
| 74 | AWS_IO_API |
| 75 | struct aws_async_input_stream *aws_async_input_stream_acquire(struct aws_async_input_stream *stream); |
| 76 | |
| 77 | /** |
| 78 | * Decrement reference count. |
| 79 | * You may pass in NULL (has no effect). |
| 80 | * Always returns NULL. |
| 81 | */ |
| 82 | AWS_IO_API |
| 83 | struct aws_async_input_stream *aws_async_input_stream_release(struct aws_async_input_stream *stream); |
| 84 | |
| 85 | /** |
| 86 | * Read once from the async stream into the buffer. |
| 87 | * The read completes when at least 1 byte is read, the buffer is full, or EOF is reached. |
| 88 | * Depending on implementation, the read could complete at any time. |
| 89 | * It may complete synchronously. It may complete on another thread. |
| 90 | * Returns a future, which will contain an error code if something went wrong, |
| 91 | * or a result bool indicating whether EOF has been reached. |
| 92 | * |
| 93 | * WARNING: The buffer must have space available. |
| 94 | * WARNING: Do not read again until the previous read is complete. |
| 95 | */ |
| 96 | AWS_IO_API |
| 97 | struct aws_future_bool *aws_async_input_stream_read(struct aws_async_input_stream *stream, struct aws_byte_buf *dest); |
| 98 | |
| 99 | /** |
| 100 | * Read repeatedly from the async stream until the buffer is full, or EOF is reached. |
| 101 | * Depending on implementation, this could complete at any time. |
| 102 | * It may complete synchronously. It may complete on another thread. |
| 103 | * Returns a future, which will contain an error code if something went wrong, |
| 104 | * or a result bool indicating whether EOF has been reached. |
| 105 | * |
| 106 | * WARNING: The buffer must have space available. |
| 107 | * WARNING: Do not read again until the previous read is complete. |
| 108 | */ |
| 109 | AWS_IO_API |
| 110 | struct aws_future_bool *aws_async_input_stream_read_to_fill( |
| 111 | struct aws_async_input_stream *stream, |
| 112 | struct aws_byte_buf *dest); |
| 113 | |
| 114 | AWS_EXTERN_C_END |
| 115 | AWS_POP_SANE_WARNING_LEVEL |
| 116 | |
| 117 | #endif /* AWS_IO_ASYNC_STREAM_H */ |