-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathfile_read_operation.cpp
More file actions
99 lines (83 loc) · 2.26 KB
/
file_read_operation.cpp
File metadata and controls
99 lines (83 loc) · 2.26 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
///////////////////////////////////////////////////////////////////////////////
// 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::try_start() noexcept
{
const DWORD numberOfBytesToRead =
m_byteCount <= 0xFFFFFFFF ?
static_cast<DWORD>(m_byteCount) : DWORD(0xFFFFFFFF);
BOOL ok = ::ReadFile(
m_fileHandle,
m_buffer,
numberOfBytesToRead,
nullptr,
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.
m_errorCode = errorCode;
ok = ::GetOverlappedResult(
m_fileHandle,
get_overlapped(),
&m_numberOfBytesTransferred,
FALSE);
if (!ok)
{
m_numberOfBytesTransferred = 0;
}
return false;
}
return true;
}
bool cppcoro::file_read_operation_cancellable::try_start() noexcept
{
const DWORD numberOfBytesToRead =
m_byteCount <= 0xFFFFFFFF ?
static_cast<DWORD>(m_byteCount) : DWORD(0xFFFFFFFF);
BOOL ok = ::ReadFile(
m_fileHandle,
m_buffer,
numberOfBytesToRead,
nullptr,
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.
this->m_errorCode = errorCode;
ok = ::GetOverlappedResult(
m_fileHandle,
get_overlapped(),
&m_numberOfBytesTransferred,
FALSE);
if (!ok)
{
m_numberOfBytesTransferred = 0;
}
return false;
}
return true;
}
void cppcoro::file_read_operation_cancellable::cancel() noexcept
{
(void)::CancelIoEx(m_fileHandle, this->get_overlapped());
}
#endif // CPPCORO_OS_WINNT