文档清楚地说,如果 WSASend 立即完成,您将获得 WSA_IO_PENDING,但这永远不会发生。我总是得到 0,并且 dwBytesTransferred 总是与我发送的字节匹配。但是,似乎有时会调用我的完成例程,有时则不会。我为发送分配了缓冲区,所以如果不调用完成例程,我需要释放缓冲区。
我有三个即兴计数器,m_dwAsyncSend、m_dwSyncSend 和 m_dwCompletions。m_dwAsycSend 始终为零,并且 m_dwSyncSend 和 m_dwCompletions 始终相距甚远,因为一个 m_dwSyncSend 可能是 750 而 m_dwCompletions 是 2。线程有很多时候是可警告的,所以我认为我不会那样饿死它。
这让我疯了!现在是午夜之后,我整天都在这。如果其中任何一个不连贯,我会责备它!
这是代码,我认为您不需要类文件来查看我在做什么。
void
CALLBACK
SendCompletion(
DWORD dwError,
DWORD cbTransferred,
LPOVERLAPPED pOvl,
DWORD dwFlags
)
{
LPWSABUF pBuffer = (LPWSABUF)((DWORD_PTR)pOvl + sizeof(OVERLAPPED));
CNetAsyncSocket *pSock = (CNetAsyncSocket *)pOvl->hEvent;
pSock->m_dwCompletions++;
if(dwError != NO_ERROR)
{
// If things didn't go as planned, ring the bell and disconnect.
pSock->Disconnect();
tracef(TT_REGULAR, 1,
"SOCKET_ERROR in CNetAsyncSocket::Send(), disconnecting, error code: %ld, on socket: %s:%ld",
dwError, pSock->GetIP(), pSock->GetPort());
free(pOvl);
}
else
{
// If we sent less than we meant to, queue up the rest.
if(cbTransferred < pBuffer->len)
{
DWORD dwRemaining = pBuffer->len - cbTransferred;
memmove(pBuffer->buf, (PVOID)((DWORD_PTR)pBuffer->buf + dwRemaining), dwRemaining);
pBuffer->len = dwRemaining;
}
else
{
free(pOvl);
}
}
}
void CNetAsyncSocket::SendAsync(PBYTE pData, DWORD dwLength)
{
// We want to minimize heap churn, so let's do this in one allocation.
// Also, having this in one chunk of memory makes it easier to interpret
// it on the other side.
DWORD dwAllocSize =
sizeof(OVERLAPPED) + // The OVERLAPPED struct.
sizeof(WSABUF) + // The buffer info.
dwLength; // The actual buffer we're copying.
LPOVERLAPPED pOvl = (LPOVERLAPPED)malloc(dwAllocSize);
if(pOvl == NULL)
{
// Out of memory.
}
// Initialize the allocation.
ZeroMemory(pOvl, dwAllocSize);
// Build the pointers.
LPWSABUF pBuffer = (LPWSABUF)((DWORD_PTR)pOvl + sizeof(OVERLAPPED));
pBuffer->len = dwLength;
assert(pBuffer->len < 1000000);
pBuffer->buf = (PCHAR)((DWORD_PTR)pBuffer + sizeof(WSABUF));
// When you have a completion routine, the hEvent member is ignored, so we
// can use it to pass our this pointer to the completion routine.
pOvl->hEvent = (PVOID)this;
// Copy the data to the buffer.
CopyMemory(pBuffer->buf, pData, dwLength);
// Send the data.
DWORD dwSent = 0;
int iResult = WSASend(
m_hSocket, // The socket.
pBuffer, // The WSABUF struct.
1, // Number of structs (1).
&dwSent, // Bytes sent. Updated if it happens synchronously.
0, // No flags.
pOvl, // The overlapped struct.
SendCompletion); // Completion routine.
if(iResult == NO_ERROR)
{
// If the send happened synchronously, we can go ahead and delete the
// memory that we allocated.
// TODO: look at bytes transferred, and if they're less than the total
// then issue another send with the remainder.
if(HasOverlappedIoCompleted(pOvl))
{
// If I actually free this here, the completion routine gets garbage.
//free(pOvl);
m_dwSyncSend++;
}
else
{
m_dwAsyncSend++;
}
}
else
{
// If we got WSA_IO_PENDING, then that just means the completion routine
// will take care of it.
if(iResult != WSA_IO_PENDING)
{
Disconnect();
tracef(TT_REGULAR, 1,
"SOCKET_ERROR in CNetAsyncSocket::Send(), disconnecting, error code: %ld, on socket: %s:%ld",
iResult, GetIP(), GetPort());
// Don't need the payload anymore.
free(pOvl);
}
else
{
m_dwAsyncSend++;
}
}
}