| 1 | #ifndef AWS_IO_PIPE_H |
| 2 | #define AWS_IO_PIPE_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/byte_buf.h> |
| 10 | #include <aws/io/io.h> |
| 11 | |
| 12 | AWS_PUSH_SANE_WARNING_LEVEL |
| 13 | |
| 14 | struct aws_event_loop; |
| 15 | |
| 16 | struct aws_pipe_read_end { |
| 17 | void *impl_data; |
| 18 | }; |
| 19 | |
| 20 | struct aws_pipe_write_end { |
| 21 | void *impl_data; |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * Callback for when the pipe is readable (edge-triggered), or an error has occurred. |
| 26 | * Afer subscribing, the callback is invoked when the pipe has data to read, or the pipe has an error. |
| 27 | * The readable callback is invoked again any time the user reads all data, and then more data arrives. |
| 28 | * Note that it will not be invoked again if the pipe still has unread data when more data arrives. |
| 29 | * `error_code` of AWS_ERROR_SUCCESS indicates a readable event, and otherwise contains the value of the error. |
| 30 | * `user_data` corresponds to the `user_data` passed into aws_pipe_subscribe_to_read_events(). |
| 31 | * This callback is always invoked on the read-end's event-loop thread. |
| 32 | */ |
| 33 | typedef void(aws_pipe_on_readable_fn)(struct aws_pipe_read_end *read_end, int error_code, void *user_data); |
| 34 | |
| 35 | /** |
| 36 | * Callback for when the asynchronous aws_pipe_write() operation has either completed or failed. |
| 37 | * `write_end` will be NULL if this callback is invoked after the the write-end has been cleaned up, |
| 38 | * this does not necessarily mean that the write operation failed. |
| 39 | * `error_code` will be AWS_ERROR_SUCCESS if all data was written, or a code corresponding to the error. |
| 40 | * `src_buffer` corresponds to the buffer passed into aws_pipe_write() |
| 41 | * `user_data` corresponds to the `user_data` passed into aws_pipe_write(). |
| 42 | * This callback is always invoked on the write-end's event-loop thread. |
| 43 | */ |
| 44 | typedef void(aws_pipe_on_write_completed_fn)( |
| 45 | struct aws_pipe_write_end *write_end, |
| 46 | int error_code, |
| 47 | struct aws_byte_cursor src_buffer, |
| 48 | void *user_data); |
| 49 | |
| 50 | AWS_EXTERN_C_BEGIN |
| 51 | |
| 52 | /** |
| 53 | * Opens an OS specific bidirectional pipe. |
| 54 | * The read direction is stored in read_end. Write direction is stored in write_end. |
| 55 | * Each end must be connected to an event-loop, and further calls to each end must happen on that event-loop's thread. |
| 56 | */ |
| 57 | AWS_IO_API |
| 58 | int aws_pipe_init( |
| 59 | struct aws_pipe_read_end *read_end, |
| 60 | struct aws_event_loop *read_end_event_loop, |
| 61 | struct aws_pipe_write_end *write_end, |
| 62 | struct aws_event_loop *write_end_event_loop, |
| 63 | struct aws_allocator *allocator); |
| 64 | |
| 65 | /** |
| 66 | * Clean up the read-end of the pipe. |
| 67 | * This must be called on the thread of the connected event-loop. |
| 68 | */ |
| 69 | AWS_IO_API |
| 70 | int aws_pipe_clean_up_read_end(struct aws_pipe_read_end *read_end); |
| 71 | |
| 72 | /** |
| 73 | * Clean up the write-end of the pipe. |
| 74 | * This must be called on the thread of the connected event-loop. |
| 75 | */ |
| 76 | AWS_IO_API |
| 77 | int aws_pipe_clean_up_write_end(struct aws_pipe_write_end *write_end); |
| 78 | |
| 79 | /** |
| 80 | * Get the event-loop connected to the read-end of the pipe. |
| 81 | * This may be called on any thread. |
| 82 | */ |
| 83 | AWS_IO_API |
| 84 | struct aws_event_loop *aws_pipe_get_read_end_event_loop(const struct aws_pipe_read_end *read_end); |
| 85 | |
| 86 | /** |
| 87 | * Get the event-loop connected to the write-end of the pipe. |
| 88 | * This may be called on any thread. |
| 89 | */ |
| 90 | AWS_IO_API |
| 91 | struct aws_event_loop *aws_pipe_get_write_end_event_loop(const struct aws_pipe_write_end *write_end); |
| 92 | |
| 93 | /** |
| 94 | * Initiates an asynchrous write from the source buffer to the pipe. |
| 95 | * The data referenced by `src_buffer` must remain in memory until the operation completes. |
| 96 | * `on_complete` is called on the event-loop thread when the operation has either completed or failed. |
| 97 | * The callback's pipe argument will be NULL if the callback is invoked after the pipe has been cleaned up. |
| 98 | * This must be called on the thread of the connected event-loop. |
| 99 | */ |
| 100 | AWS_IO_API |
| 101 | int aws_pipe_write( |
| 102 | struct aws_pipe_write_end *write_end, |
| 103 | struct aws_byte_cursor src_buffer, |
| 104 | aws_pipe_on_write_completed_fn *on_completed, |
| 105 | void *user_data); |
| 106 | |
| 107 | /** |
| 108 | * Read data from the pipe into the destination buffer. |
| 109 | * Attempts to read enough to fill all remaining space in the buffer, from `dst_buffer->len` to `dst_buffer->capacity`. |
| 110 | * `dst_buffer->len` is updated to reflect the buffer's new length. |
| 111 | * `num_bytes_read` (optional) is set to the total number of bytes read. |
| 112 | * This function never blocks. If no bytes could be read without blocking, then AWS_OP_ERR is returned and |
| 113 | * aws_last_error() code will be AWS_IO_READ_WOULD_BLOCK. |
| 114 | * This must be called on the thread of the connected event-loop. |
| 115 | */ |
| 116 | AWS_IO_API |
| 117 | int aws_pipe_read(struct aws_pipe_read_end *read_end, struct aws_byte_buf *dst_buffer, size_t *num_bytes_read); |
| 118 | |
| 119 | /** |
| 120 | * Subscribe to be notified when the pipe becomes readable (edge-triggered), or an error occurs. |
| 121 | * `on_readable` is invoked on the event-loop's thread when the pipe has data to read, or the pipe has an error. |
| 122 | * `on_readable` is invoked again any time the user reads all data, and then more data arrives. |
| 123 | * Note that it will not be invoked again if the pipe still has unread data when more data arrives. |
| 124 | * This must be called on the thread of the connected event-loop. |
| 125 | */ |
| 126 | AWS_IO_API |
| 127 | int aws_pipe_subscribe_to_readable_events( |
| 128 | struct aws_pipe_read_end *read_end, |
| 129 | aws_pipe_on_readable_fn *on_readable, |
| 130 | void *user_data); |
| 131 | |
| 132 | /** |
| 133 | * Stop receiving notifications about events on the read-end of the pipe. |
| 134 | * This must be called on the thread of the connected event-loop. |
| 135 | */ |
| 136 | AWS_IO_API |
| 137 | int aws_pipe_unsubscribe_from_readable_events(struct aws_pipe_read_end *read_end); |
| 138 | |
| 139 | #if defined(_WIN32) |
| 140 | /** |
| 141 | * Generate a unique pipe name. |
| 142 | * The suggested dst_size is 256. |
| 143 | */ |
| 144 | AWS_IO_API |
| 145 | int aws_pipe_get_unique_name(char *dst, size_t dst_size); |
| 146 | #endif |
| 147 | |
| 148 | AWS_EXTERN_C_END |
| 149 | AWS_POP_SANE_WARNING_LEVEL |
| 150 | |
| 151 | #endif /* AWS_IO_PIPE_H */ |