我正在使用 IO 完成例程通过管道在不同机器上的两个进程之间进行通信。
有时,当调用WriteFileEx 的完成例程时,完成例程参数dwErrorCode 为0(即无错误),GetOverlappedResult 返回true(即无错误),但dwNumberOfBytesTransfered 与WriteFileEx 调用中的nNumberOfBytesToWrite 不匹配。然而,我只在管道的客户端看到这个。
如果传输的字节数与请求传输的字节数不匹配,怎么能认为这是成功的呢?
这是创建客户端对管道的句柄的方式:
mHPipe = CreateFile(pipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
FILE_FLAG_OVERLAPPED | // overlapped
FILE_FLAG_WRITE_THROUGH, // write through mode
NULL); // no template file
// do some checking...
// The pipe connected; change to message-read mode.
DWORD dwMode = PIPE_READMODE_MESSAGE;
BOOL fSuccess = SetNamedPipeHandleState(mHPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
谁能明白为什么会发生这种情况?
谢谢
编辑:
相关的 WriteFileEx 代码如下:
void WINAPI CompletedWriteRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverLap)
{
BOOL fWrite = FALSE;
LPPIPEINST lpPipeInst = (LPPIPEINST)lpOverLap;
//
// ! 99.9% of the time, dwNumberOfBytesTransfered == lpPipeInst->cbDataSize
// but 0.1% of the time, they do not match
//
// Some stuff
// Copy next message to send
memcpy_s(lpPipeInst->chData, sizeof(lpPipeInst->chData), pMsg->msg, pMsg->size);
lpPipeInst->cbDataSize = pMsg->size;
// Some other stuff
fWrite = WriteFileEx(lpPipeInst->hPipeInst,
lpPipeInst->chData,
lpPipeInst->cbDataSize,
(LPOVERLAPPED) lpPipeInst,
(LPOVERLAPPED_COMPLETION_ROUTINE)CompletedWriteRoutine);
// Some other, other stuff
}
其中 LPPIPAINST 被声明为:
typedef struct
{
OVERLAPPED oOverlap; // must remain first item
HANDLE hPipeInst;
TCHAR chData[BUFSIZE];
DWORD cbDataSize;
} PIPEINST, *LPPIPEINST;
并且对 CompletedWriteRoutine 的初始调用给出了这样声明的 lpOverlap 参数:
PIPEINST pipeInstWrite = {0};
pipeInstWrite.hPipeInst = client.getPipeHandle();
pipeInstWrite.oOverlap.hEvent = hEvent[eventWriteComplete];
编辑:
在尝试按照 Harry 的建议重新初始化重叠结构之后,我注意到了一些奇怪的东西。我在每个之前memset
将OVERLAPPED
结构归零WriteFileEx
,大约 1/5000 完成例程回调,cbWritten
参数和OVERLAPPED
结构的InternalHigh
成员现在设置为前一条消息的大小,而不是最近的消息。我在完成例程内的管道的客户端和服务器端都添加了一些日志记录到文件中,并且两端发送和接收的数据完全匹配(以及正确的预期数据)。然后揭示了在将数据写入文件的时间里,结构中的InternalHigh
成员OVERLAPPED
已经改变,现在反映了我期望的消息的大小(cbWritten
仍然是旧的消息大小)。我删除了文件日志记录,现在可以使用以下代码重现类似发条的问题:
void WINAPI CompletedWriteRoutine(DWORD dwErr, DWORD cbWritten, LPOVERLAPPED lpOverLap)
{
LPPIPEINST lpPipeInst = (LPPIPEINST)lpOverLap;
// Completion routine says it wrote the amount of data from the previous callback
if (cbWritten != lpPipeInst->cbDataSize)
{
// Roughly 1 in 5000 callbacks ends up in here
OVERLAPPED ovl1 = lpPipeInst->oOverlap; // Contains size of previous message, i.e. cbWritten
Sleep(100);
OVERLAPPED ovl2 = lpPipeInst->oOverlap; // Contains size of most recent message, i.e lpPipeInst->cbDataSize
}
...
}
似乎有时,完成例程在OVERLAPPED
结构和完成例程输入参数更新之前被调用。我正在使用MsgWaitForMultipleObjectsEx(eventLast, hEvent, INFINITE, QS_POSTMESSAGE, MWMO_ALERTABLE);
在 Windows 7 64 位上调用完成例程。
“系统在调用完成例程后不使用 OVERLAPPED 结构,因此完成例程可以释放重叠结构使用的内存。”
...显然,这段代码可以重现的东西永远不会发生?
这是一个 WINAPI 错误吗?