aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/pipe.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/pipe.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/pipe.h')
-rw-r--r--util/system/pipe.h90
1 files changed, 90 insertions, 0 deletions
diff --git a/util/system/pipe.h b/util/system/pipe.h
new file mode 100644
index 0000000000..75d0360049
--- /dev/null
+++ b/util/system/pipe.h
@@ -0,0 +1,90 @@
+#pragma once
+
+#ifdef __GNUC__
+ #pragma GCC diagnostic push
+ #pragma GCC diagnostic ignored "-Warray-bounds" //need because of bug in gcc4.9.2
+#endif
+
+#include "defaults.h"
+#include "file.h"
+#include <util/generic/ptr.h>
+#include <util/network/pair.h>
+#include <util/generic/noncopyable.h>
+
+using PIPEHANDLE = SOCKET;
+#define INVALID_PIPEHANDLE INVALID_SOCKET
+
+/// Pipe-like object: pipe on POSIX and socket on windows
+class TPipeHandle: public TNonCopyable {
+public:
+ inline TPipeHandle() noexcept
+ : Fd_(INVALID_PIPEHANDLE)
+ {
+ }
+
+ inline TPipeHandle(PIPEHANDLE fd) noexcept
+ : Fd_(fd)
+ {
+ }
+
+ inline ~TPipeHandle() {
+ Close();
+ }
+
+ bool Close() noexcept;
+
+ inline PIPEHANDLE Release() noexcept {
+ PIPEHANDLE ret = Fd_;
+ Fd_ = INVALID_PIPEHANDLE;
+ return ret;
+ }
+
+ inline void Swap(TPipeHandle& r) noexcept {
+ DoSwap(Fd_, r.Fd_);
+ }
+
+ inline operator PIPEHANDLE() const noexcept {
+ return Fd_;
+ }
+
+ inline bool IsOpen() const noexcept {
+ return Fd_ != INVALID_PIPEHANDLE;
+ }
+
+ ssize_t Read(void* buffer, size_t byteCount) const noexcept;
+ ssize_t Write(const void* buffer, size_t byteCount) const noexcept;
+
+ // Only CloseOnExec is supported
+ static void Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode = 0);
+
+private:
+ PIPEHANDLE Fd_;
+};
+
+class TPipe {
+public:
+ TPipe();
+ /// Takes ownership of handle, so closes it when the last holder of descriptor dies.
+ explicit TPipe(PIPEHANDLE fd);
+ ~TPipe();
+
+ void Close();
+
+ bool IsOpen() const noexcept;
+ PIPEHANDLE GetHandle() const noexcept;
+
+ size_t Read(void* buf, size_t len) const;
+ size_t Write(const void* buf, size_t len) const;
+
+ // Only CloseOnExec is supported
+ static void Pipe(TPipe& reader, TPipe& writer, EOpenMode mode = 0);
+
+private:
+ class TImpl;
+ using TImplRef = TSimpleIntrusivePtr<TImpl>;
+ TImplRef Impl_;
+};
+
+#ifdef __GNUC__
+ #pragma GCC diagnostic pop
+#endif