如何使用 IOCP 将用户定义的数据传递给工作线程?
嘿...我使用 I/O 完成端口和 Winsock 创建了一个小型测试服务器。 我可以成功连接并将套接字句柄与完成端口关联。 但我不知道如何将用户定义的数据结构传递到工作线程中...
到目前为止我所尝试的是将用户结构传递为 (ULONG_PTR)&struct as
CreateIoCompletionPort()
关联调用中的完成键 但这没有用。
现在我尝试定义自己的重叠结构并使用 CONTAINING_RECORD() ,如此处所述 http: //msdn.microsoft.com/en-us/magazine/cc302334.aspx 和 http://msdn.microsoft.com/en-us/magazine/bb985148.aspx。 但这也行不通。 (我得到 pHelper 内容的奇怪值)
所以我的问题是:如何使用 WSARecv()、GetQueuedCompletionStatus() 和完成包或 OVERLAPPED-strucutre 将数据传递到工作线程?
编辑:我如何成功传输“每个连接数据”?...似乎我的艺术(如上面两个链接中所解释的)错误。
这是我的代码:(是的,它很丑,而且是唯一的测试代码)
struct helper
{
SOCKET m_sock;
unsigned int m_key;
OVERLAPPED over;
};
///////
SOCKET newSock = INVALID_SOCKET;
WSABUF wsabuffer;
char cbuf[250];
wsabuffer.buf = cbuf;
wsabuffer.len = 250;
DWORD flags, bytesrecvd;
while(true)
{
newSock = accept(AcceptorSock, NULL, NULL);
if(newSock == INVALID_SOCKET)
ErrorAbort("could not accept a connection");
//associate socket with the CP
if(CreateIoCompletionPort((HANDLE)newSock, hCompletionPort, 3,0) != hCompletionPort)
ErrorAbort("Wrong port associated with the connection");
else
cout << "New Connection made and associated\n";
helper* pHelper = new helper;
pHelper->m_key = 3;
pHelper->m_sock = newSock;
memset(&(pHelper->over), 0, sizeof(OVERLAPPED));
flags = 0;
bytesrecvd = 0;
if(WSARecv(newSock, &wsabuffer, 1, NULL, &flags, (OVERLAPPED*)pHelper, NULL) != 0)
{
if(WSAGetLastError() != WSA_IO_PENDING)
ErrorAbort("WSARecv didnt work");
}
}
//Cleanup
CloseHandle(hCompletionPort);
cin.get();
return 0;
}
DWORD WINAPI ThreadProc(HANDLE h)
{
DWORD dwNumberOfBytes = 0;
OVERLAPPED* pOver = nullptr;
helper* pHelper = nullptr;
WSABUF RecvBuf;
char cBuffer[250];
RecvBuf.buf = cBuffer;
RecvBuf.len = 250;
DWORD dwRecvBytes = 0;
DWORD dwFlags = 0;
ULONG_PTR Key = 0;
GetQueuedCompletionStatus(h, &dwNumberOfBytes, &Key, &pOver, INFINITE);
//Extract helper
pHelper = (helper*)CONTAINING_RECORD(pOver, helper, over);
cout << "Received Overlapped item" << endl;
if(WSARecv(pHelper->m_sock, &RecvBuf, 1, &dwRecvBytes, &dwFlags, pOver, NULL) != 0)
cout << "Could not receive data\n";
else
cout << "Data Received: " << RecvBuf.buf << endl;
ExitThread(0);
}
Hey... I created a small test server using I/O completion ports and winsock.
I can successfully connect and associate a socket handle with the completion port.
But I don´t know how to pass user-defined data-structures into the wroker thread...
What I´ve tried so far was passing a user-structure as (ULONG_PTR)&structure as
the Completion Key in the association-call of CreateIoCompletionPort()
But that did not work.
Now I tried defining my own OVERLAPPED-structure and using CONTAINING_RECORD() as described here http://msdn.microsoft.com/en-us/magazine/cc302334.aspx and http://msdn.microsoft.com/en-us/magazine/bb985148.aspx.
But that does not work, too. (I get freaky values for the contents of pHelper)
So my Question is: How can I pass data to the worker thread using WSARecv(), GetQueuedCompletionStatus() and the Completion packet or the OVERLAPPED-strucutre?
EDIT: How can I successfully transmit "per-connection-data"?... It seems like I got the art of doing it (like explained in the two links above) wrong.
Here goes my code: (Yes, its ugly and its only TEST-code)
struct helper
{
SOCKET m_sock;
unsigned int m_key;
OVERLAPPED over;
};
///////
SOCKET newSock = INVALID_SOCKET;
WSABUF wsabuffer;
char cbuf[250];
wsabuffer.buf = cbuf;
wsabuffer.len = 250;
DWORD flags, bytesrecvd;
while(true)
{
newSock = accept(AcceptorSock, NULL, NULL);
if(newSock == INVALID_SOCKET)
ErrorAbort("could not accept a connection");
//associate socket with the CP
if(CreateIoCompletionPort((HANDLE)newSock, hCompletionPort, 3,0) != hCompletionPort)
ErrorAbort("Wrong port associated with the connection");
else
cout << "New Connection made and associated\n";
helper* pHelper = new helper;
pHelper->m_key = 3;
pHelper->m_sock = newSock;
memset(&(pHelper->over), 0, sizeof(OVERLAPPED));
flags = 0;
bytesrecvd = 0;
if(WSARecv(newSock, &wsabuffer, 1, NULL, &flags, (OVERLAPPED*)pHelper, NULL) != 0)
{
if(WSAGetLastError() != WSA_IO_PENDING)
ErrorAbort("WSARecv didnt work");
}
}
//Cleanup
CloseHandle(hCompletionPort);
cin.get();
return 0;
}
DWORD WINAPI ThreadProc(HANDLE h)
{
DWORD dwNumberOfBytes = 0;
OVERLAPPED* pOver = nullptr;
helper* pHelper = nullptr;
WSABUF RecvBuf;
char cBuffer[250];
RecvBuf.buf = cBuffer;
RecvBuf.len = 250;
DWORD dwRecvBytes = 0;
DWORD dwFlags = 0;
ULONG_PTR Key = 0;
GetQueuedCompletionStatus(h, &dwNumberOfBytes, &Key, &pOver, INFINITE);
//Extract helper
pHelper = (helper*)CONTAINING_RECORD(pOver, helper, over);
cout << "Received Overlapped item" << endl;
if(WSARecv(pHelper->m_sock, &RecvBuf, 1, &dwRecvBytes, &dwFlags, pOver, NULL) != 0)
cout << "Could not receive data\n";
else
cout << "Data Received: " << RecvBuf.buf << endl;
ExitThread(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你像这样传递你的结构,它应该可以正常工作:
编辑以添加每个 IO 数据:
异步 api 经常被滥用的功能之一是它们不复制 OVERLAPPED 结构,它们只是使用提供的结构 - 因此重叠从 GetQueuedCompletionStatus 返回的结构指向最初提供的结构。所以:
请再次注意,在您的原始示例中,您的铸造错误。 (OVERLAPPED*)pHelper 正在将指针传递给辅助结构的 START,但 OVERLAPPED 部分是最后声明的。我将其更改为传递实际重叠部分的地址,这意味着代码无需强制转换即可编译,这让我们知道我们正在做正确的事情。我还将重叠的结构移动为该结构的第一个成员。
要捕获另一侧的数据:
在这一侧,重叠结构是辅助结构的第一个成员尤为重要,因为这样可以轻松地从 api 提供给我们的 OVERLAPPED* 和辅助结构 * 中进行转换我们实际上想要。
If you pass your struct like this it should work just fine:
Edit to add per IO data:
One of the frequently abused features of the asynchronous apis is they don't copy the OVERLAPPED struct, they simply use the provided one - hence the overlapped struct returned from GetQueuedCompletionStatus points to the originally provided struct. So:
Notice that, again, in your original sample, you were getting your casting wrong. (OVERLAPPED*)pHelper was passing a pointer to the START of the helper struct, but the OVERLAPPED part was declared last. I changed it to pass the address of the actual overlapped part, which means that the code compiles without a cast, which lets us know we are doing the correct thing. I also moved the overlapped struct to be the first member of the struct.
To catch the data on the other side:
On this side it is particularly important that the overlapped struct is the first member of the helper struct, as that makes it easy to cast back from the OVERLAPPED* the api gives us, and the helper* we actually want.
您可以通过 PostQueuedCompletionStatus。
You can send special-purpose data of your own to the completion port via PostQueuedCompletionStatus.
我使用标准套接字例程(socket、closesocket、bind、accept、connect ...)来创建/销毁,并使用 ReadFile/WriteFile 进行 I/O,因为它们允许使用 OVERLAPPED 结构。
在您的套接字接受或连接后,您应该将其与其服务的会话上下文关联起来。然后,将套接字关联到 IOCP 并(在第三个参数中)为其提供对会话上下文的引用。 IOCP 不知道这个引用是什么,也不关心这个问题。该引用供您使用,以便当您通过 GetQueuedCompletionStatus 获取 IOC 时,参数 3 指向的变量将用该引用填充,以便您立即找到与套接字事件关联的上下文并可以开始为该事件提供服务。我通常使用包含(除其他外)套接字声明、重叠结构以及其他特定于会话的数据的索引结构。我在参数 3 中传递给 CreateIoCompletionPort 的引用将是包含套接字的结构成员的索引。
您需要检查 GetQueuedCompletionStatus 是否返回完成或超时。通过超时,您可以运行索引结构并查看(例如)其中一个结构是否超时或其他情况,并采取适当的内务操作。
还需要检查重叠结构以确保 I/O 正确完成。
为 IOCP 提供服务的函数应该是一个单独的多线程实体。使用与系统中的核心数相同的线程数,或者至少不要超过这个数,因为这会浪费系统资源(用于为事件提供服务的资源不会比系统中的核心数更多,对吧?) 。
IOCP 确实是世界上最好的(好得令人难以置信),任何说“每个套接字一个线程”或“在一个函数中等待多个套接字列表”的人都不知道他们在说什么。前者给你的调度程序带来压力,后者是轮询,而轮询总是非常浪费。
I use the standard socket routines (socket, closesocket, bind, accept, connect ...) for creating/destroying and ReadFile/WriteFile for I/O as they allow use of the OVERLAPPED structure.
After your socket has accepted or connected you should associate it with the session context that it services. Then you associate your socket to an IOCP and (in the third parameter) provide it with a reference to the session context. The IOCP does not know what this reference is and doesn't care either for that matter. The reference is for YOUR use so that when you get an IOC through GetQueuedCompletionStatus the variable pointed to by parameter 3 will be filled in with the reference so that you immediately find the context associated with the socket event and can begin servicing the event. I usually use an indexed structure containing (among other things) the socket declaration, the overlapped structure as well as other session-specific data. The reference I pass to CreateIoCompletionPort in parameter 3 will be the index to the structure member containing the socket.
You need to check if GetQueuedCompletionStatus returned a completion or a timeout. With a timeout you can run through your indexed structure and see (for example) if one of them has timed out or something else and take appropriate house-keeping actions.
The overlapped structure also needs to be checked to see that the I/O completed correctly.
The function servicing the IOCP should be a separate, multi-threaded entity. Use the same number of threads that you have cores in your system, or at least no more than that as it wastes system resources (you don't have more resources for servicing the event than the number of cores in your system, right?).
IOCPs really are the best of all worlds (too good to be true) and anyone who says "one thread per socket" or "wait on multiple-socket list in one function" don't know what they are talking about. The former stresses your scheduler and the latter is polling and polling is ALWAYS extremely wasteful.