aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-02-12 15:47:31 +0800
committercrupest <crupest@outlook.com>2024-02-12 15:47:31 +0800
commit96ed44c31b92f1492be68c084e8b18972d549743 (patch)
tree62c56538c150ef29ddf31de417b640d210f001fe /src/common
parentf3af6c7e5b46f4209a4981e5d7be217368f40b15 (diff)
downloadcru-96ed44c31b92f1492be68c084e8b18972d549743.tar.gz
cru-96ed44c31b92f1492be68c084e8b18972d549743.tar.bz2
cru-96ed44c31b92f1492be68c084e8b18972d549743.zip
WORKING: add unix pipe.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt3
-rw-r--r--src/common/platform/unix/UnixPipe.cpp38
2 files changed, 40 insertions, 1 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 2414831b..aaff70fe 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -22,8 +22,9 @@ target_compile_definitions(CruBase PUBLIC $<$<CONFIG:Debug>:CRU_DEBUG>)
if (UNIX AND NOT EMSCRIPTEN)
target_sources(CruBase PRIVATE
+ platform/unix/PosixSpawnSubProcess.cpp
platform/unix/UnixFileStream.cpp
- platform/unix/UnixFileStream.cpp
+ platform/unix/UnixPipe.cpp
)
if (NOT APPLE)
diff --git a/src/common/platform/unix/UnixPipe.cpp b/src/common/platform/unix/UnixPipe.cpp
new file mode 100644
index 00000000..8393f022
--- /dev/null
+++ b/src/common/platform/unix/UnixPipe.cpp
@@ -0,0 +1,38 @@
+#include "cru/common/platform/unix/UnixPipe.h"
+#include "cru/common/Exception.h"
+#include "cru/common/log/Logger.h"
+
+#include <unistd.h>
+
+namespace cru::platform::unix {
+UnixPipe::UnixPipe(Usage usage) : usage_(usage) {
+ int fds[2];
+ if (pipe(fds) != 0) {
+ throw ErrnoException(u"Failed to create unix pipe.");
+ }
+ read_fd_ = fds[0];
+ write_fd_ = fds[1];
+}
+
+int UnixPipe::GetSelfFileDescriptor() {
+ if (usage_ == Usage::Send) {
+ return write_fd_;
+ } else {
+ return read_fd_;
+ }
+}
+
+int UnixPipe::GetOtherFileDescriptor() {
+ if (usage_ == Usage::Send) {
+ return read_fd_;
+ } else {
+ return write_fd_;
+ }
+}
+
+UnixPipe::~UnixPipe() {
+ if (close(GetSelfFileDescriptor()) != 0) {
+ CRU_LOG_ERROR(u"Failed to close unix pipe file descriptor.");
+ }
+}
+} // namespace cru::platform::unix