我有以下简化的IO完成端口服务器C代码:
int main(..)
{
startCompletionPortThreadProc();
// Await client connection
sockaddr_in clientAddress;
int clientAddressSize = sizeof( clientAddress );
SOCKET acceptSocket = WSAAccept( serverSocket, (SOCKADDR*)&clientAddress, &clientAddressSize, NULL, NULL);
// Connected
CreateIoCompletionPort( (HANDLE)acceptSocket, completionPort, 0, 0 );
// Issue initial read
read( acceptSocket );
}
DWORD WINAPI completionPortThreadProc( LPVOID param )
{
DWORD bytesTransferred = 0;
ULONG_PTR completionKey = NULL;
LPPER_IO_DATA perIoData = NULL;
while( GetQueuedCompletionStatus( completionPort, &bytesTransferred, &completionKey, (LPOVERLAPPED*)&perIoData, INFINITE ) )
{
if( WaitForSingleObject( exitEvent, 0 ) == WAIT_OBJECT_0 )
{
break;
}
if( !perIoData )
continue;
if( bytesTransferred == 0 )
{
//TODO
}
switch( perIoData->operation )
{
case OPERATION_READ:
{
// Bytes have been received
if( bytesTransferred < perIoData->WSABuf.len )
{
// Terminate string
perIoData->WSABuf.buf[bytesTransferred] = '\0';
perIoData->WSABuf.buf[bytesTransferred+1] = '\0';
}
// Add data to message build
message += std::tstring( (TCHAR*)perIoData->WSABuf.buf );
// Perform next read
perIoData->WSABuf.len = sizeof( perIoData->inOutBuffer );
perIoData->flags = 0;
if( WSARecv( perIoData->socket, &( perIoData->WSABuf ), 1, &bytesTransferred, &( perIoData->flags ), &( perIoData->overlapped ), NULL ) == 0 )
{
// Part message
continue;
}
if( WSAGetLastError() == WSA_IO_PENDING )
{
// End of message
//TODO: Process message here
continue;
}
}
}
break;
case OPERATION_WRITE:
{
perIoData->bytesSent += bytesTransferred;
if( perIoData->bytesSent < perIoData->bytesToSend )
{
perIoData->WSABuf.buf = (char*)&( perIoData->inOutBuffer[perIoData->bytesSent] );
perIoData->WSABuf.len = ( perIoData->bytesToSend - perIoData->bytesSent);
}
else
{
perIoData->WSABuf.buf = (char*)perIoData->inOutBuffer;
perIoData->WSABuf.len = _tcslen( perIoData->inOutBuffer ) * sizeof( TCHAR );
perIoData->bytesSent = 0;
perIoData->bytesToSend = perIoData->WSABuf.len;
}
if( perIoData->bytesToSend )
{
if( WSASend( perIoData->socket, &( perIoData->WSABuf ), 1, &bytesTransferred, 0, &( perIoData->overlapped ), NULL ) == 0 )
continue;
if( WSAGetLastError() == WSA_IO_PENDING )
continue;
}
}
break;
}
}
return 0;
}
bool SocketServer::read( SOCKET socket, HANDLE completionPort )
{
PER_IO_DATA* perIoData = new PER_IO_DATA;
ZeroMemory( perIoData, sizeof( PER_IO_DATA ) );
perIoData->socket = socket;
perIoData->operation = OPERATION_READ;
perIoData->WSABuf.buf = (char*)perIoData->inOutBuffer;
perIoData->WSABuf.len = sizeof( perIoData->inOutBuffer );
perIoData->overlapped.hEvent = WSACreateEvent();
DWORD bytesReceived = 0;
if( WSARecv( perIoData->socket, &( perIoData->WSABuf ), 1, &bytesReceived, &( perIoData->flags ), &( perIoData->overlapped ), NULL ) == SOCKET_ERROR )
{
int gle = WSAGetLastError();
if( WSAGetLastError() != WSA_IO_PENDING )
{
delete perIoData;
return false;
}
}
return true;
}
bool SocketServer::write( SOCKET socket, std::tstring& data )
{
PER_IO_DATA* perIoData = new PER_IO_DATA;
ZeroMemory( perIoData, sizeof( PER_IO_DATA ) );
perIoData->socket = socket;
perIoData->operation = OPERATION_WRITE;
perIoData->WSABuf.buf = (char*)data.c_str();
perIoData->WSABuf.len = _tcslen( data.c_str() ) * sizeof( TCHAR );
perIoData->bytesToSend = perIoData->WSABuf.len;
perIoData->overlapped.hEvent = WSACreateEvent();
DWORD bytesSent = 0;
if( WSASend( perIoData->socket, &( perIoData->WSABuf ), 1, &bytesSent, 0, &( perIoData->overlapped ), NULL ) == SOCKET_ERROR )
{
if( WSAGetLastError() != WSA_IO_PENDING )
{
delete perIoData;
return false;
}
}
return true;
}
1)我的第一个问题是初始阅读.
在客户端连接(接受)上,我发出一个读取.由于客户端尚未发送任何数据,WSAGetLastError()是WSA_IO_PENDING并且read方法返回.
当客户端然后发送数据时,线程仍然停留在GetQueuedCompletionStatus调用中(因为我假设我需要另一个WSARecv调用?).
我应该继续循环读取方法,直到数据到达?这似乎不合逻辑,我想通过发布初始读取GetQueuedCompletionStatus将在数据到达时完成.
2)我需要在没有确认的情况下双向读写数据.因此我也用IOCP线程创建了一个客户端.实际上是否可以使用完成端口执行此操作,或者必须在写入之后执行读操作?
对于感觉基本问题感到抱歉,但在浏览互联网和构建IOCP示例后,我仍然无法回答问题.
提前谢谢了.
最佳答案
On client connection (accept), I issue a read. As the client hasn’t sent any data yet, WSAGetLastError() is WSA_IO_PENDING and the read method returns.
这是正常的行为.
When the client then sends data, the thread remains stuck in the GetQueuedCompletionStatus call (as I assume I need another WSARecv call?).
不,你不需要另一个电话.如果它被卡住,那么您没有正确地将读取与I / O完成端口关联.
Am I supposed to keep looping the read method until data arrives?
不需要.您需要一次调用WSARecv()进行初始读取. WSA_IO_PENDING错误表示读取正在等待数据,并在数据实际到达时发出I / O完成端口的信号.在该信号实际到达之前,请勿调用WSARecv()(或任何其他读取函数).然后,您可以再次调用WSARecv()以等待更多数据.重复直到插座断开连接.
I thought by issuing the initial read GetQueuedCompletionStatus would complete when data arrived.
这正是应该发生的事情.
2) I need to read and write data bi-directional without acknowledgements. Therefore I’ve also created a client with the IOCP thread. Is it actually possible to do this with completion ports
是.阅读和写作是分开的操作,它们不是相互依赖的.
does a read have to be followed by a write?
如果您的协议不需要它,请不要.
现在,说到这一点,您的代码存在一些问题.
在次要说明中,WSAAccept()是同步的,您应该考虑使用AcceptEx(),以便它可以使用相同的I / O完成端口来报告新连接.
但更重要的是,当挂起的I / O操作失败时,GetQueuedCompletionStatus()返回FALSE,返回的LPOVERLAPPED指针将为非NULL,GetLastError()将报告I / O操作失败的原因.但是,如果GetQueuedCompletionStatus()本身失败,则返回的LPOVERLAPPED指针将为NULL,GetLastError()将报告GetQueuedCompletionStatus()失败的原因.在documentation中清楚地解释了这种差异,但是你的while循环不能解释它.请改用do..while循环并根据LPOVERLAPPED指针执行操作:
DWORD WINAPI completionPortThreadProc( LPVOID param )
{
DWORD bytesTransferred = 0;
ULONG_PTR completionKey = NULL;
LPPER_IO_DATA perIoData = NULL;
do
{
if( GetQueuedCompletionStatus( completionPort, &bytesTransferred, &completionKey, (LPOVERLAPPED*)&perIoData, INFINITE ) )
{
// I/O success, handle perIoData based on completionKey as needed...
}
else if( perIoData )
{
// I/O failed, handle perIoData based on completionKey as needed...
}
else
{
// GetQueuedCompletionStatus() failure...
break;
}
}
while( WaitForSingleObject( exitEvent, 0 ) == WAIT_TIMEOUT );
return 0;
}
在旁注中,不要使用事件对象来指示completionPortThreadProc()何时应该退出,而是考虑使用PostQueuedCompletionionStatus()
代替将终止completionKey发布到I / O完成端口,然后您的循环可以查找该值:
DWORD WINAPI completionPortThreadProc( LPVOID param )
{
DWORD bytesTransferred = 0;
ULONG_PTR completionKey = NULL;
LPPER_IO_DATA perIoData = NULL;
do
{
if( GetQueuedCompletionStatus( completionPort, &bytesTransferred, &completionKey, (LPOVERLAPPED*)&perIoData, INFINITE ) )
{
if( completionKey == MyTerminateKey )
break;
if( completionKey == MySocketIOKey )
{
// I/O success, handle perIoData as needed...
}
}
else if( perIoData )
{
// I/O failed, handle perIoData based on completionKey as needed...
}
else
{
// GetQueuedCompletionStatus() failure...
break;
}
}
while( true );
return 0;
}
CreateIoCompletionPort( (HANDLE)acceptSocket, completionPort, MySocketIOKey, 0 );
PostQueuedCompletionStatus( completionPort, 0, MyTerminateKey, NULL );