NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
file_async.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_FILE_FILE_ASYNC_HPP__
2#define NEFORCE_CORE_FILE_FILE_ASYNC_HPP__
3
10
14#include "NeForce/core/exception/error_code.hpp"
17#ifdef NEFORCE_PLATFORM_LINUX
18# include <aio.h>
19#endif
20NEFORCE_BEGIN_NAMESPACE__
21
27
41class NEFORCE_API file_async {
42public:
43#ifdef NEFORCE_PLATFORM_WINDOWS
44 using size_type = ::DWORD;
45 using difference_type = ::LONGLONG;
46 using aiocb_type = ::OVERLAPPED;
47#else
48 using size_type = size_t;
49 using difference_type = ::off_t;
50 using aiocb_type = ::aiocb;
51#endif
52
54
55private:
60 struct async_context {
61 string data{};
62 string* buffer = nullptr;
63 aiocb_type* cb = nullptr;
64 bool is_write = false;
65
70 explicit async_context(string d);
71
76 explicit async_context(string* buf);
77
81 ~async_context();
82 };
83
84public:
91 struct async_result {
92 bool completed = false;
93 size_t bytes_transferred = 0;
94 int error_code = 0;
95 aiocb_type* cb = nullptr;
96 async_context* user_context = nullptr;
97 };
98
99private:
100 native_handle_type handle_;
101 mutable mutex mutex_;
102 mutable vector<aiocb_type*> operations_;
104
105 bool complete_result(async_result& result, size_type bytes) noexcept;
106 bool check_completion(async_result& result) noexcept;
107
108public:
116
123
124 file_async(const file_async&) = delete;
125 file_async& operator=(const file_async&) = delete;
126
131 file_async(file_async&& other) noexcept;
132
138 file_async& operator=(file_async&& other) noexcept;
139
150 async_result read(string& buffer, size_type size, difference_type offset = -1) const;
151
163
174
182 void cancel(async_result& result) noexcept;
183};
184 // File
186
187NEFORCE_END_NAMESPACE__
188#endif // NEFORCE_CORE_FILE_FILE_ASYNC_HPP__
::aiocb aiocb_type
异步I/O控制块类型
async_result write(string data, size_type size, difference_type offset=-1)
提交异步写入操作
void cancel(async_result &result) noexcept
取消异步操作
bool wait(async_result &result, uint32_t timeout_ms=numeric_traits< uint32_t >::max())
等待异步操作完成
file_async(file_async &&other) noexcept
移动构造函数
_NEFORCE native_handle_type native_handle_type
原生文件句柄类型
async_result read(string &buffer, size_type size, difference_type offset=-1) const
提交异步读取操作
file_async(native_handle_type handle)
构造函数
file_async & operator=(file_async &&other) noexcept
移动赋值运算符
~file_async()
析构函数
::off_t difference_type
偏移量类型
size_t size_type
大小类型
非递归互斥锁
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
无序映射容器
动态大小数组容器
文件操作常量定义
unsigned int uint32_t
32位无符号整数类型
@ OVERLAPPED
Linux不支持异步I/O标志
uint64_t size_t
无符号大小类型
NEFORCE_ALWAYS_INLINE_INLINE thread::native_handle_type handle() noexcept
获取当前线程句柄
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
互斥锁
字符串类型别名和实用函数
异步操作结果句柄
bool completed
操作是否已完成
size_t bytes_transferred
实际传输的字节数
int error_code
错误码(0表示成功)
aiocb_type * cb
异步I/O控制块指针
async_context * user_context
用户上下文指针
无序映射容器
动态大小数组容器