使用 I/O 完成端口的异步操作返回传输的 0 字节
尽管 I/O 操作按预期工作(我的读取缓冲区已满),但使用 I/O 完成端口的异步操作返回传输的 0 个字节。
BYTE buffer[1024] = {0};
OVERLAPPED o = {0};
HANDLE file = CreateFile(
_T("hello.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
HANDLE completion_port = CreateIoCompletionPort(
file,
NULL,
0,
0
);
ReadFile(
file,
buffer,
1024,
NULL,
&o
);
在工作线程中:
DWORD numBytes = 0;
LPOVERLAPPED po;
GetQueuedCompletionStatus(
completion_port,
&numBytes,
0,
&po,
INFINITE
);
GetOverlappedResult(file, &o, &numBytes, FALSE);
两个函数都返回 numBytes 中的 0 字节,但 buffer
正在填充。这是预期的行为吗?
谢谢。
Asynchronous operations with I/O Completion Ports return 0 bytes transferred, although the I/O operations work as expected (my read buffers become full).
BYTE buffer[1024] = {0};
OVERLAPPED o = {0};
HANDLE file = CreateFile(
_T("hello.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
HANDLE completion_port = CreateIoCompletionPort(
file,
NULL,
0,
0
);
ReadFile(
file,
buffer,
1024,
NULL,
&o
);
In the work thread:
DWORD numBytes = 0;
LPOVERLAPPED po;
GetQueuedCompletionStatus(
completion_port,
&numBytes,
0,
&po,
INFINITE
);
GetOverlappedResult(file, &o, &numBytes, FALSE);
Both functions return 0 bytes in numBytes, but buffer
is filling. Is this expected behaviour?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使
GetIoCompletionPort
正常工作,您需要指定一个指向ULONG_PTR
的非空指针,以便将“key”值写入:要使用
GetOverlappedResult< /code> 成功,我相信您需要在
OVERLAPPED
结构中指定一个事件句柄(在任何情况下都强烈建议):像您一样连续调用这两个并没有真正完成太多工作 - 它们两者都告诉你同样的事情。不过,如果您连续调用两者,则需要通过将
CreateEvent
的第三个参数更改为 TRUE,将事件更改为手动重置。我的猜测是,您只是尝试两者,看看是否可以让其中一个工作。考虑到所有因素,我可能只使用GetQueuedCompletionStatus
,然后就这样。当然,您通常要做的不仅仅是调用一次然后退出。您通常在循环中调用它,处理您已读取的当前缓冲区,然后再次调用 ReadFile 来读取另一个信息缓冲区,如下所示:至少在我的机器上进行快速测试,这显示正确读取的字节数(
sizeof(buffer)
直到最后一个数据包,然后是文件的剩余大小)。For
GetIoCompletionPort
to work correctly, you need to specify a non-null pointer to aULONG_PTR
for it to write the 'key' value to:To use
GetOverlappedResult
successfully, I believe you need to specify an event handle in theOVERLAPPED
structure (strongly recommended in any case):Calling the two in succession as you were doesn't really accomplish much -- they both tell you about the same things. Though if you do call both in succession, you'll need to change the Event to be a manual-reset by changing the third parameter to
CreateEvent
to TRUE. My guess is that you were just trying both to see if you could get one to work. All things considered, I'd probably just useGetQueuedCompletionStatus
, and leave it at that. Of course, you'll usually do more than call it once and quit. You normally call it in a loop, processing the current buffer you've read, then callingReadFile
again to read another buffer of info, something like this:At least in a quick test on my machine, this showed the number of bytes read correctly (
sizeof(buffer)
up to the last packet, then the remaining size of the file).