1#ifndef NEFORCE_CORE_SYSTEM_PIPE_HPP__
2#define NEFORCE_CORE_SYSTEM_PIPE_HPP__
11#include "NeForce/core/exception/system_exception.hpp"
12NEFORCE_BEGIN_NAMESPACE__
24struct pipe_exception final : system_exception {
25 explicit pipe_exception(
const char* info =
"Pipe Operation Failed.",
const error_code code =
last_error()) noexcept
27 system_exception(info, code) {}
29 explicit pipe_exception(
const exception& e) :
30 system_exception(e) {}
32 ~pipe_exception()
override =
default;
34 NEFORCE_NODISCARD
const char*
type() const noexcept
override {
return "pipe_exception"; }
54 using native_handle_type = _NEFORCE native_handle_type;
57#ifdef NEFORCE_PLATFORM_WINDOWS
58 native_handle_type read_handle_ =
nullptr;
59 native_handle_type write_handle_ =
nullptr;
61 native_handle_type fds_[2] = {-1, -1};
63 bool nonblocking_{
false};
82 explicit
pipe(
bool inheritable,
bool nonblocking = false);
92 pipe& operator=(const
pipe&) = delete;
176#ifdef NEFORCE_PLATFORM_WINDOWS
187#ifdef NEFORCE_PLATFORM_WINDOWS
188 return write_handle_;
215 using native_handle_type = _NEFORCE native_handle_type;
218#ifdef NEFORCE_PLATFORM_WINDOWS
219 native_handle_type pipe_handle_;
220 bool is_server_{
false};
222 native_handle_type fd_{-1};
226 bool nonblocking_{
false};
321 NEFORCE_NODISCARD
const string&
name() const noexcept {
return name_; }
328#ifdef NEFORCE_PLATFORM_WINDOWS
345NEFORCE_END_NAMESPACE__
native_handle_type native_handle() const noexcept
获取原生句柄
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)
创建命名管道服务端
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
获取异常类型