From 96ed44c31b92f1492be68c084e8b18972d549743 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 12 Feb 2024 15:47:31 +0800 Subject: WORKING: add unix pipe. --- include/cru/common/platform/unix/UnixPipe.h | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 include/cru/common/platform/unix/UnixPipe.h (limited to 'include/cru/common/platform/unix/UnixPipe.h') diff --git a/include/cru/common/platform/unix/UnixPipe.h b/include/cru/common/platform/unix/UnixPipe.h new file mode 100644 index 00000000..153c52bf --- /dev/null +++ b/include/cru/common/platform/unix/UnixPipe.h @@ -0,0 +1,59 @@ +#pragma once + +#include "../../PreConfig.h" + +#ifdef CRU_PLATFORM_UNIX + +#include "../../Base.h" + +namespace cru::platform::unix { +/** + * @brief an unix pipe, commonly for communication of parent process and child + * process. + * + * There are two types of pipes sorted by its usage. For stdin, parent process + * SEND data to child process. For stdout and stderr, parent process RECEIVE + * data from child process. Each pipe has two ends, one for read and the other + * for write. But for send and receive, they are reversed. It is a little + * confused to consider which end should be used by parent and which end should + * be used by child. So this class help you make it clear. You specify SEND or + * RECEIVE, and this class give you a parent used end and a child used end. + * + * This class will only close the end used by parent when it is destructed. It + * is the user's duty to close the one used by child. + */ +class UnixPipe : public Object { + private: + constexpr static auto kLogTag = u"cru::platform::unix::UnixPipe"; + + public: + enum class Usage { + Send, + Receive, + }; + + explicit UnixPipe(Usage usage); + + CRU_DELETE_COPY(UnixPipe) + CRU_DELETE_MOVE(UnixPipe) + + ~UnixPipe(); + + /** + * @brief aka, the one used by parent process. + */ + int GetSelfFileDescriptor(); + + /** + * @brief aka, the one used by child process. + */ + int GetOtherFileDescriptor(); + + private: + Usage usage_; + int read_fd_; + int write_fd_; +}; +} // namespace cru::platform::unix + +#endif -- cgit v1.2.3