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