NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
pipe.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_SYSTEM_PIPE_HPP__
2#define NEFORCE_CORE_SYSTEM_PIPE_HPP__
3
10
11#include "NeForce/core/exception/system_exception.hpp"
12NEFORCE_BEGIN_NAMESPACE__
13
19
24struct pipe_exception final : system_exception {
25 explicit pipe_exception(const char* info = "Pipe Operation Failed.", const error_code code = last_error()) noexcept
26 :
27 system_exception(info, code) {}
28
29 explicit pipe_exception(const exception& e) :
30 system_exception(e) {}
31
32 ~pipe_exception() override = default;
33
34 NEFORCE_NODISCARD const char* type() const noexcept override { return "pipe_exception"; }
35};
36 // Exceptions
38
44
52class NEFORCE_API pipe {
53public:
54 using native_handle_type = _NEFORCE native_handle_type;
55
56private:
57#ifdef NEFORCE_PLATFORM_WINDOWS
58 native_handle_type read_handle_ = nullptr;
59 native_handle_type write_handle_ = nullptr;
60#else
61 native_handle_type fds_[2] = {-1, -1};
62#endif
63 bool nonblocking_{false};
64
65public:
71 pipe() noexcept = default;
72
82 explicit pipe(bool inheritable, bool nonblocking = false);
83
89 ~pipe();
90
91 pipe(const pipe&) = delete;
92 pipe& operator=(const pipe&) = delete;
93
97 pipe(pipe&& other) noexcept;
98
102 pipe& operator=(pipe&& other) noexcept;
103
111 static void ignore_sigpipe() noexcept;
112
119 int read(void* buffer, size_t size) noexcept;
120
125 NEFORCE_NODISCARD string read_available();
126
135 bool set_nonblocking(bool v) noexcept;
136
141 NEFORCE_NODISCARD bool is_nonblocking() const noexcept { return nonblocking_; }
142
149 int write(const void* data, size_t size) noexcept;
150
154 void close_read() noexcept;
155
159 void close_write() noexcept;
160
164 void close() noexcept;
165
170 NEFORCE_NODISCARD bool is_valid() const noexcept;
171
175 NEFORCE_NODISCARD native_handle_type native_read_handle() const noexcept {
176#ifdef NEFORCE_PLATFORM_WINDOWS
177 return read_handle_;
178#else
179 return fds_[0];
180#endif
181 }
182
186 NEFORCE_NODISCARD native_handle_type native_write_handle() const noexcept {
187#ifdef NEFORCE_PLATFORM_WINDOWS
188 return write_handle_;
189#else
190 return fds_[1];
191#endif
192 }
193
197 NEFORCE_NODISCARD native_handle_type detach_read_handle() noexcept;
198
202 NEFORCE_NODISCARD native_handle_type detach_write_handle() noexcept;
203};
204
205
213class NEFORCE_API named_pipe {
214public:
215 using native_handle_type = _NEFORCE native_handle_type;
216
217private:
218#ifdef NEFORCE_PLATFORM_WINDOWS
219 native_handle_type pipe_handle_;
220 bool is_server_{false};
221#else
222 native_handle_type fd_{-1};
223 string fifo_path_;
224#endif
225 string name_;
226 bool nonblocking_{false};
227
228public:
233
238
239 named_pipe(const named_pipe&) = delete;
240 named_pipe& operator=(const named_pipe&) = delete;
241
245 named_pipe(named_pipe&& other) noexcept;
246
250 named_pipe& operator=(named_pipe&& other) noexcept;
251
259 bool create(const string& name, bool nonblocking = false);
260
268 bool connect(const string& name, int timeout_ms = -1);
269
276
283 int read(void* buffer, size_t size) noexcept;
284
291 int write(const void* data, size_t size) noexcept;
292
296 void close() noexcept;
297
302 NEFORCE_NODISCARD bool is_valid() const noexcept;
303
309 bool set_nonblocking(bool v) noexcept;
310
315 NEFORCE_NODISCARD bool is_nonblocking() const noexcept { return nonblocking_; }
316
321 NEFORCE_NODISCARD const string& name() const noexcept { return name_; }
322
327 NEFORCE_NODISCARD native_handle_type native_handle() const noexcept {
328#ifdef NEFORCE_PLATFORM_WINDOWS
329 return pipe_handle_;
330#else
331 return fd_;
332#endif
333 }
334
340 static bool remove(const string& path);
341};
342 // Pipe
344
345NEFORCE_END_NAMESPACE__
346#endif // NEFORCE_CORE_SYSTEM_PIPE_HPP__
native_handle_type native_handle() const noexcept
获取原生句柄
named_pipe()
默认构造函数
named_pipe & operator=(named_pipe &&other) noexcept
移动赋值运算符
int write(const void *data, size_t size) noexcept
向管道写入数据
int read(void *buffer, size_t size) noexcept
从管道读取数据
static bool remove(const string &path)
删除命名管道(仅 Linux FIFO)
bool create(const string &name, bool nonblocking=false)
创建命名管道服务端
~named_pipe()
析构函数
const string & name() const noexcept
获取管道名称
bool connect(const string &name, int timeout_ms=-1)
连接到已存在的命名管道(客户端)
bool wait_for_client()
等待客户端连接(服务端)
named_pipe(named_pipe &&other) noexcept
移动构造函数
bool is_valid() const noexcept
检查管道是否有效
bool set_nonblocking(bool v) noexcept
设置非阻塞模式
bool is_nonblocking() const noexcept
查询是否非阻塞
void close() noexcept
关闭管道
文件路径类
native_handle_type detach_read_handle() noexcept
分离读端句柄(调用者负责关闭)
native_handle_type detach_write_handle() noexcept
分离写端句柄(调用者负责关闭)
bool is_nonblocking() const noexcept
查询管道是否处于非阻塞模式
void close() noexcept
关闭管道的所有端
native_handle_type native_write_handle() const noexcept
获取写端句柄
void close_write() noexcept
关闭管道的写端
int read(void *buffer, size_t size) noexcept
从管道读取数据
string read_available()
从管道读取所有可用数据(非阻塞)
bool is_valid() const noexcept
检查管道是否有效
void close_read() noexcept
关闭管道的读端
bool set_nonblocking(bool v) noexcept
设置管道非阻塞模式
int write(const void *data, size_t size) noexcept
向管道写入数据
native_handle_type native_read_handle() const noexcept
获取读端句柄
pipe() noexcept=default
默认构造函数
static void ignore_sigpipe() noexcept
忽略 SIGPIPE 信号
error_code last_error() noexcept
获取当前系统错误码
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
const char * type() const noexcept override
获取异常类型