- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将WinQueues与GetQueuedCompletionStatus一起使用,但似乎无法正确处理。步骤如下:
void foo() {
...
SOCKET sck = WSASocket(AF_INET, SOCK_DGRAM, 0,
NULL, 0, WSA_FLAG_OVERLAPPED);
....
bind(sck,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
HANDLE hPort = CreateIoCompletionPort((HANDLE)sck, NULL, 0, 0 );
OVERLAPPED pOverlapped = {0,};
WSARecvFrom(sck,NULL,0,NULL,NULL,(struct sockaddr *)&laddr,&lsize,&pOverlapped,0);
BOOL bReturn = GetQueuedCompletionStatus(
hPort,
&rbytes,
(LPDWORD)&lpContext,
&pOutOverlapped,
INFINITE);
...
}
最佳答案
就像任何读取操作一样,无论是否使用IOCP,都必须为WSARecvFrom()
提供一个缓冲区。您必须确保缓冲区在内存中保持有效,直到IOCP操作完成为止。 IOCP将填充您提供的缓冲区,然后在完成时通知完成端口。
UDP在单个数据报中不能传输超过65535个字节,因此您可以将其用作最大缓冲区大小。
在您的示例中,您的代码被编写为同步运行(完全违背了使用IOCP的目的),因此您可以使用本地缓冲区:
void foo() {
...
SOCKET sck = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (sck == INVALID_SOCKET)
{
// error, do something...
return;
}
....
bind(sck,(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
HANDLE hPort = CreateIoCompletionPort((HANDLE)sck, NULL, 0, 0 );
if (!hPort)
{
// error, do something...
return;
}
WSAOVERLAPPED Overlapped = {0};
Overlapped.hEvent = WSACreateEvent();
BYTE buffer[0xFFFF];
DWORD dwBytesRecvd = 0;
DWORD dwFlags = 0;
sockaddr_in fromaddr = {0};
int fromaddrlen = sizeof(fromaddr);
WSABUF buf;
buf.len = sizeof(buffer);
buf.buf = buffer;
int iRet = WSARecvFrom(sck, &buf, 1, &dwBytesRecvd, &dwFlags, (sockaddr*)&fromaddr, &fromaddrlen, &Overlapped, NULL);
if (iRet == SOCKET_ERROR)
{
if (WSAGetLastError() != WSA_IO_PENDING)
{
// error, do something...
return;
}
DWORD rBytes;
ULONG_PTR key;
LPOVERLAPPED pOverlapped = NULL;
if (!GetQueuedCompletionStatus(hPort, &rbytes, &key, &pOverlapped, INFINITE))
{
if (pOverlapped)
{
// WSARecvFrom() failed...
}
else
{
// GetQueuedCompletionStatus() failed...
}
// do something...
return;
}
}
// I/O complete, use buffer, dwBytesRecvd, dwFlags, and fromaddr as needed...
}
recvfrom()
并让它阻塞调用线程,直到数据到达为止。当您有一个为完成端口服务的线程池时,IOCP效果最佳。调用
WSARecvFrom()
并使其在后台运行,请勿等待。让一个单独的线程调用
GetQueuedCompletionPort()
并在接收到数据时对其进行处理,例如:
struct MyOverlapped
{
WSAOVERLAPPED overlapped;
BYTE buffer[0xFFFF];
DWORD buflen;
DWORD flags;
sockaddr_storage fromaddr;
int fromaddrLen;
};
HANDLE hPort = NULL;
void foo() {
...
SOCKET sck = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (sck == INVALID_SOCKET)
{
// error, do something...
return;
}
....
bind(sck,(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
hPort = CreateIoCompletionPort((HANDLE)sck, NULL, 0, 0 );
if (!hPort)
{
// error, do something...
return;
}
MyOverlapped *ov = new MyOverlapped;
ZeroMemory(ov, sizeof(*ov));
ov->overlapped.hEvent = WSACreateEvent();
ov->fromaddrlen = sizeof(ov->fromaddr);
WSABUF buf;
buf.len = sizeof(ov->buffer);
buf.buf = ov->buffer;
int iRet = WSARecvFrom(sck, &buf, 1, &ov->buflen, &ov->flags, (sockaddr*)&ov->fromaddr, &ov->fromaddrlen, (WSAOVERLAPPED*)ov, NULL);
if (iRet == SOCKET_ERROR)
{
if (WSAGetLastError() != WSA_IO_PENDING)
{
// error, do something...
return;
}
// WSARecvFrom() is now operating in the background,
// the IOCP port will be signaled when finished...
}
else
{
// data is already available,
// the IOCP port will be signaled immediately...
}
...
}
...
// in another thread...
{
...
DWORD rbytes;
ULONG_PTR key;
MyOverlapped *ov = NULL;
if (!GetQueuedCompletionStatus(hPort, &rbytes, &key, (LPOVERLAPPED*)&ov, INFINITE))
{
if (ov)
{
// WSARecvFrom() failed...
// free ov, or reuse it for another operation...
}
else
{
// GetQueuedCompletionStatus() failed...
}
}
else
{
// use ov as needed...
// free ov, or reuse it for another operation...
}
...
}
关于sockets - 使用GetQueuedCompletionStatus和ERROR_MORE_DATA的套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883438/
我正在尝试使用 windows ddk 7 包中提供的 offreg.dll 在内存中创建一个离线注册表。 您可以在此处找到有关 offreg.dll 的更多信息:MSDN 目前,在尝试从打开的注册表
无论如何,我对 RegEnumValue 的调用总是在键的最后一个值上返回 ERROR_MORE_DATA (234)。 我通过以下方式获取所需的缓冲区大小: retCode = RegQueryIn
我正在尝试将 GetQueuedCompletionStatus 与 Winsocks 一起使用,但我似乎无法正确执行。流程如下: void foo() { ... SOCKET sc
在 C 领域工作几年后,我试图再次进入 C 领域。抱歉问这个问题,这对你们中的一些人来说可能很容易。 我试图理解为什么下面的代码会为 RegEnumKeyExA 的返回值提供 234 错误代码 (ER
Win32 SDK 中返回可变大小数据的函数通常允许您传递零大小的缓冲区,然后它们会告诉您需要多少空间。它们往往看起来像以下之一(简化): LSTATUS RegQueryValueExA (HKEY
我是一名优秀的程序员,十分优秀!