James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 1 | #ifndef AOS_UTIL_SCOPED_PIPE_H_ |
| 2 | #define AOS_UTIL_SCOPED_PIPE_H_ |
| 3 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 4 | #include <stddef.h> |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 7 | #include <memory> |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 8 | #include <optional> |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 9 | #include <string> |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 10 | |
James Kuszmaul | 731a05d | 2022-01-07 17:59:26 -0800 | [diff] [blame] | 11 | #include "absl/types/span.h" |
| 12 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 13 | namespace aos::util { |
| 14 | |
| 15 | // RAII Pipe for sending individual ints between reader and writer. |
| 16 | class ScopedPipe { |
| 17 | public: |
| 18 | class ScopedReadPipe; |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 19 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 20 | class ScopedWritePipe; |
| 21 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 22 | struct PipePair { |
| 23 | std::unique_ptr<ScopedReadPipe> read; |
| 24 | std::unique_ptr<ScopedWritePipe> write; |
| 25 | }; |
| 26 | |
| 27 | static PipePair MakePipe(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 28 | |
| 29 | virtual ~ScopedPipe(); |
| 30 | |
| 31 | int fd() const { return fd_; } |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 32 | // Sets FD_CLOEXEC on the file descriptor. |
| 33 | void SetCloexec(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | ScopedPipe(int fd = -1); |
| 37 | |
| 38 | int fd_; |
| 39 | |
| 40 | ScopedPipe(const ScopedPipe &) = delete; |
| 41 | ScopedPipe &operator=(const ScopedPipe &) = delete; |
| 42 | ScopedPipe(ScopedPipe &&); |
| 43 | ScopedPipe &operator=(ScopedPipe &&); |
| 44 | }; |
| 45 | |
| 46 | class ScopedPipe::ScopedReadPipe : public ScopedPipe { |
| 47 | public: |
| 48 | std::optional<uint32_t> Read(); |
James Kuszmaul | 731a05d | 2022-01-07 17:59:26 -0800 | [diff] [blame] | 49 | // Reads as many bytes as possible out of the pipe, appending them to the end |
| 50 | // of the provided buffer. Returns the number of bytes read. Dies on errors |
| 51 | // other than EAGAIN or EWOULDBLOCK. |
| 52 | size_t Read(std::string *buffer); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | using ScopedPipe::ScopedPipe; |
| 56 | |
| 57 | friend class ScopedPipe; |
| 58 | }; |
| 59 | |
| 60 | class ScopedPipe::ScopedWritePipe : public ScopedPipe { |
| 61 | public: |
| 62 | void Write(uint32_t data); |
James Kuszmaul | 731a05d | 2022-01-07 17:59:26 -0800 | [diff] [blame] | 63 | // Writes the entirety of the specified buffer to the pipe. Dies on failure. |
| 64 | void Write(absl::Span<const uint8_t> data); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | using ScopedPipe::ScopedPipe; |
| 68 | |
| 69 | friend class ScopedPipe; |
| 70 | }; |
| 71 | |
| 72 | } // namespace aos::util |
| 73 | |
| 74 | #endif // AOS_UTIL_SCOPED_PIPE_H_ |