在调试时,当调用 WSARecv 时,我为函数提供PerIoData->WSABUF结构的地址。这应该将发送的数据分配给WSABUF.buf char* 数组,看起来是这样。当工作线程循环回到等待的GetQueuedCompletionStatus时,它似乎以某种方式(神奇地)将该数据发送到 PerIoData.Buffer (char* 数组)。所以本质上,PerIoData.Buffer和PerIoData.WSABUF.buf都等于同一个 char* 数组。当我从PER_IO_DATA结构(以及对它的所有引用)中删除PerIoData.Buffer时,尽管我知道客户端发送数据时GetQueuedCompletionStaus永远不会返回WSABUF.buf应该填充数据。
相关资料:
- 我正在实现“Microsoft Windows 网络编程”(第 157 页)中的完成端口模型。尽管那本书中的示例还有很多有待独立发现,但我的代码现在可以正常工作了。
- ServerWorkerThread的while循环中:首先调用GetQueuedCompletionStatus,接收per_handle_data,per_io_data
per_io_data 结构是这样的:
struct _PER_IO_DATA{ //in the interests of an efficient question, i'm omitting the //constructor/destructor code public: OVERLAPPED Overlapped; WSABUF DataBuf; char myBuffer[BUFFER_LENGTH]; int BufferLen; int OperationType; }; typedef _PER_IO_DATA PER_IO_DATA; typedef _PER_IO_DATA *PPER_IO_DATA;
我的 GetQueuedCompletionStatus 函数调用如下:
ret = GetQueuedCompletionStatus(CompletionPort, &BytesTransferred, (LPDWORD)&PerHandleData, (LPOVERLAPPED *)&PerIoData, INFINITE);
我的 WSARecv 函数是这样调用的:
WSARecv(PerHandleData->Socket, &(PerIoData->DataBuf), 1, NULL, &Flags, ((LPWSAOVERLAPPED)&PerIoData->Overlapped), NULL); //i know casting the Overlapped structure as LPWSAOVERLAPPED is unnecessary, but I was tweaking the //code when I didn't fully understand the problems I was having.
我的问题是我从来没有明确地为PerIoData->Buffer分配任何东西,但它似乎总是被发送的数据填充。我相信 GetQueuedCompletionStatus “知道”将此数据发送到该PerIoData->Buffer尽管它期望一个指向LPOVERLAPPED结构的指针(我将包含相关Buffer char 数组的PerIoData结构实例传递给该结构)。这真的让我很烦......也许它的行为不像我想的那样,但我可以看到PerIoData->Buffer被填充的唯一地方是来自GetQueuedCompletionStatus方法。如果不是这样,那么PerIoData->Buffer似乎无处不在?我已经搜索了 MSDN 和谷歌好几天了。我会继续寻找,如果我找到答案,我会发布更新。请帮忙?提前致谢!
*注意:我会创建标签 WSABUF 和 GetQueuedCompletionStatus,但这是我的第一篇文章。
--编辑:我发布了结构和工作线程,省略了所有其他不相关的代码。-- 您会注意到 _PER_IO_DATA::DataBuf.buf 已分配内存,然后归零。不指向 myBuffer 数组....
#include "stdafx.h"
#define SEND_POSTED 1
#define RECV_POSTED 2
#define BUFFER_LENGTH 1024
HANDLE CompletionPort;
SOCKADDR_IN serverAddress, *clientAddress;
SOCKET listener, client;
unsigned short port = 5000;
SYSTEM_INFO SystemInfo;
int i;
struct _PER_HANDLE_DATA{//Per handle data structure
SOCKET Socket;
SOCKADDR_STORAGE Address;
_PER_HANDLE_DATA(){
Socket = 0;
ZeroMemory(&Address, sizeof(SOCKADDR_STORAGE));
}
~_PER_HANDLE_DATA(){
Socket = NULL;
ZeroMemory(&Address, sizeof(SOCKADDR_STORAGE));
}
};typedef _PER_HANDLE_DATA PER_HANDLE_DATA;typedef _PER_HANDLE_DATA *PPER_HANDLE_DATA;
struct _PER_IO_DATA{
public:
OVERLAPPED Overlapped;
WSABUF DataBuf;
char myBuffer[BUFFER_LENGTH];
int BufferLen;
int OperationType;
_PER_IO_DATA(){
OperationType = 0;
DataBuf.len = BUFFER_LENGTH;
DataBuf.buf = (char*)malloc(BUFFER_LENGTH+1);
BufferLen = BUFFER_LENGTH;
ZeroMemory(DataBuf.buf, (sizeof(BUFFER_LENGTH+1)));
ZeroMemory(&myBuffer, (sizeof(char)*BUFFER_LENGTH));
SecureZeroMemory((PVOID)&Overlapped, sizeof(Overlapped));
}
~_PER_IO_DATA(){
free(&DataBuf.buf);
}
};
typedef _PER_IO_DATA PER_IO_DATA;
typedef _PER_IO_DATA *PPER_IO_DATA;
unsigned _stdcall ServerWorkerThread(LPVOID CompletionPortID);
int _tmain(int argc, _TCHAR* argv[])
{
/*
INITIALIZE WINSOCK AND COMPLETION PORT, AND ACCEPT CONNECTIONS
*/
}
unsigned _stdcall ServerWorkerThread(LPVOID CompletionPortID){
printf("ServerWorkerThread(%d) Working\n", GetCurrentThreadId());
HANDLE CompletionPort = (HANDLE) CompletionPortID;
DWORD BytesTransferred;
PPER_HANDLE_DATA PerHandleData = new PER_HANDLE_DATA;
PPER_IO_DATA PerIoData = new PER_IO_DATA;
DWORD SendBytes = 0, RecvBytes = 0;
DWORD Flags;
BOOL ret;
Sleep(2000);
while(TRUE){
ret = GetQueuedCompletionStatus(CompletionPort,
&BytesTransferred,
(LPDWORD)&PerHandleData,
(LPOVERLAPPED *)&PerIoData,
INFINITE);
//printf("\n\nBytesTransferred: %d\n\n", BytesTransferred);
if(BytesTransferred == 0 && (PerIoData->OperationType == RECV_POSTED || PerIoData->OperationType == SEND_POSTED)){
closesocket(PerHandleData->Socket);
GlobalFree(PerHandleData);
GlobalFree(PerIoData);
continue;
}
if(PerIoData->OperationType == RECV_POSTED){
//output received data
if(!strcmp(PerIoData->DataBuf.buf, "Disconnect") || !strcmp(PerIoData->DataBuf.buf, "disconnect")){
printf("Disconnecting...\n");
if(!shutdown(PerHandleData->Socket, SD_BOTH)){
closesocket(PerHandleData->Socket);
delete(PerHandleData);
}
}else{
printf("RECV_POSTED: %s\n", PerIoData->DataBuf.buf);
}
}
Flags = 0;
SecureZeroMemory((PVOID)&PerIoData->Overlapped, sizeof(WSAOVERLAPPED));
PerIoData->DataBuf.len = BUFFER_LENGTH;
//***************************************************************************
//Even though the following is commented out, PerIoData->DataBuf.buf
//is still being populated and so is PerIoData-myBuffer
//So why is myBuffer being populated with data when DataBuf.buf is not pointing to it??
//PerIoData->DataBuf.buf = PerIoData->myBuffer;
//Also, if you comment out all references of myBuffer, GetQueuedCompletionStatus(),
//will never return if myBuffer doesn't exist...how does it seem to be 'aware' of myBuffer?
//***************************************************************************
PerIoData->OperationType = RECV_POSTED;
WSARecv(PerHandleData->Socket, &(PerIoData->DataBuf), 1, NULL, &Flags, ((LPWSAOVERLAPPED)&PerIoData->Overlapped), NULL);
}
return 0;
}