windowsGetReadResult
Simple wrapper around GetOverlappedResult to determine the result of a ReadFile operation.
If !allow_aborted, then aborted is never returned (OPERATION_ABORTED is considered unexpected).
The ReadFile documentation states that the number of bytes read by an overlapped ReadFile must be determined using GetOverlappedResult, even if the
operation immediately returns data:
"Use NULL for [lpNumberOfBytesRead] if this is an asynchronous operation to avoid potentially
erroneous results."
"If hFile was opened with FILE_FLAG_OVERLAPPED, the following conditions are in effect: [...]
The lpNumberOfBytesRead parameter should be set to NULL. Use the GetOverlappedResult function to
get the actual number of bytes read."
See: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
Source
Implementation
fn windowsGetReadResult(
handle: windows.HANDLE,
overlapped: *windows.OVERLAPPED,
allow_aborted: bool,
) !union(enum) {
success: u32,
closed,
aborted,
} {
var num_bytes_read: u32 = undefined;
if (0 == windows.kernel32.GetOverlappedResult(
handle,
overlapped,
&num_bytes_read,
0,
)) switch (windows.GetLastError()) {
.BROKEN_PIPE => return .closed,
.OPERATION_ABORTED => |err| if (allow_aborted) {
return .aborted;
} else {
return windows.unexpectedError(err);
},
else => |err| return windows.unexpectedError(err),
};
return .{ .success = num_bytes_read };
}