forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_read_operation.cpp
More file actions
77 lines (66 loc) · 2.1 KB
/
Copy pathfile_read_operation.cpp
File metadata and controls
77 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/file_read_operation.hpp>
#if CPPCORO_OS_WINNT
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
bool cppcoro::file_read_operation_impl::try_start(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
const DWORD numberOfBytesToRead =
m_byteCount <= 0xFFFFFFFF ?
static_cast<DWORD>(m_byteCount) : DWORD(0xFFFFFFFF);
DWORD numberOfBytesRead = 0;
BOOL ok = ::ReadFile(
m_fileHandle,
m_buffer,
numberOfBytesToRead,
&numberOfBytesRead,
operation.get_overlapped());
const DWORD errorCode = ok ? ERROR_SUCCESS : ::GetLastError();
if (errorCode != ERROR_IO_PENDING)
{
// Completed synchronously.
//
// We are assuming that the file-handle has been set to the
// mode where synchronous completions do not post a completion
// event to the I/O completion port and thus can return without
// suspending here.
operation.m_errorCode = errorCode;
operation.m_numberOfBytesTransferred = numberOfBytesRead;
return false;
}
return true;
}
void cppcoro::file_read_operation_impl::cancel(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
(void)::CancelIoEx(m_fileHandle, operation.get_overlapped());
}
#elif CPPCORO_OS_LINUX
bool cppcoro::file_read_operation_impl::try_start(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
auto seek_res = lseek(m_fd, m_offset, SEEK_SET);
if (seek_res < 0) {
operation.m_res = -errno;
return false;
}
operation.m_completeFunc = [=]() {
int res = read(m_fd, m_buffer, m_byteCount);
operation.m_mq->remove_fd_watch(m_fd);
return res;
};
operation.m_mq->add_fd_watch(m_fd, reinterpret_cast<void*>(&operation), EPOLLIN);
return true;
}
void cppcoro::file_read_operation_impl::cancel(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
operation.m_mq->remove_fd_watch(m_fd);
}
#endif