奇迹世界服务器架构(3)
d、接受SOCKET连接并进行完成端口绑定
VOID IoHandler::ProcessAcceptedSessionList()
{
SESSION_LIST_ITER it;
Session *pSession;
// 立加俊 己傍茄 技记甸阑 罐酒敌 烙矫 府胶飘肺 颗辫
m_pAcceptedSessionList->Lock();
m_pTempList->splice( m_pTempList->end(), *m_pAcceptedSessionList );//将m_pAcceptedSessionList 合并到TEMPLIST
m_pAcceptedSessionList->Unlock();
// 立加俊 己傍茄 技记俊 措茄 贸府
for( it = m_pTempList->begin(); it != m_pTempList->end(); ++it )
{
pSession = *it;
// 弥绊悼立荐甫 檬苞窍绰 版快 角菩
if( m_numActiveSessions >= m_dwMaxAcceptSession )
{
printf( "connection full! no available accept socket!\n" );
m_pTempList->erase( it-- );
ReuseSession( pSession );
continue;
}
// IOCP绑定
CreateIoCompletionPort( (HANDLE)pSession->GetSocket(), m_hIOCP, (ULONG_PTR)pSession, 0 );
// Recv俊 角菩窍绰 版快 贸府
if( !pSession->PreRecv() )
{
m_pTempList->erase( it-- );
ReuseSession( pSession );
continue;
}
//--------------------------------
// 己傍利栏肺 立加等 技记 贸府
//--------------------------------
// 匙飘亏 坷宏璃飘 积己 夸没
NetworkObject *pNetworkObject = m_fnCreateAcceptedObject();
assert( pNetworkObject );
// 匙飘亏 坷宏璃飘 官牢爹
pSession->BindNetworkObject( pNetworkObject );
// 立加矫 檬扁拳 棺 NetworkObject肺 立加 烹瘤
pSession->OnAccept();
// 悼立荐 刘啊
++m_numActiveSessions;
}
if( !m_pTempList->empty() )
{
// 立加俊 己傍茄 技记甸阑 ActiveSessionList俊 眠啊
m_pActiveSessionList->Lock();
m_pActiveSessionList->splice( m_pActiveSessionList->begin(), *m_pTempList );
m_pActiveSessionList->Unlock();
}
}
PreRecv() 的动作判断SOCKET是否继续有效
BOOL Session::PreRecv()
{
WSABUF wsabuf;
m_pRecvBuffer->GetRecvParam( (BYTE**)&wsabuf.buf, (int&)wsabuf.len );
ZeroMemory( &m_recvIoData, sizeof(OVERLAPPEDEX) );
m_recvIoData.dwOperationType = RECV_POSTED;
int ret = WSARecv( GetSocket(), &wsabuf, 1, &m_recvIoData.dwIoSize, &m_recvIoData.dwFlags, &m_recvIoData, NULL );
if( ret == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING )
{
return FALSE;
}
return TRUE;
}