我有一个用python + ctypes实现的简单命名管道服务器:
pipe = windll.kernel32.CreateNamedPipeA('\\\\.\\pipe\\pipe_name', PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 0, 0, 0, None)
overlapped_struct = OVERLAPPED()
windll.kernel32.ConnectNamedPipe(pipe, byref(overlapped_struct))
iocp = windll.kernel32.CreateIoCompletionPort(pipe, None, WPARAM(707070707), 0)
while True:
bytes_transferred = DWORD()
completion_key = WPARAM()
overlapped_struct_ptr = POINTER(OVERLAPPED)()
windll.kernel32.GetQueuedCompletionStatus(iocp, byref(bytes_transferred), byref(completion_key), byref(overlapped_struct_ptr), INFINITE)
bytes_available = DWORD()
windll.kernel32.PeekNamedPipe(pipe, None, 0, None, byref(bytes_available))
buf = create_string_buffer(bytes_available.value)
ret_code = windll.kernel32.ReadFile(pipe, byref(buf), bytes_available.value, None, overlapped_struct_ptr)
它从外部程序接收数据。我希望只有当某些东西到达管道时才会返回GetQueuedCompletionStatus
,但情况并非总是如此。有时,在它使完成数据包出列后,我可以看到bytes_available == 0
,ret_code == 0
和overlapped_struct.Internal == 256
(我假设它意味着ERROR_NO_MORE_ITEMS)。
关于它为什么会发生的任何想法?
答案 0 :(得分:0)
我明白了。完成数据包在管道上的任何操作之后排队。这意味着GetQueuedCompletionStatus
将在客户端连接,写入和读取操作后返回。
我的情况发生了什么:
ReadFile
返回0并且overlapped_struct.Internal
指示ERROR_NO_MORE_ITEMS,这是完全正确的:管道中没有新数据。