CONNID CUdpServer::HandleAccept(TUdpBufferObj* pBufferObj) { CONNID dwConnID = 0; { CCriSecLock locallock(m_csAccept); dwConnID = FindConnectionID(&pBufferObj->clientAddr); if(dwConnID == 0) { dwConnID = ::GenerateConnectionID(m_dwConnID); TUdpSocketObj* pSocketObj = GetFreeSocketObj(dwConnID); memcpy(&pSocketObj->clientAddr, &pBufferObj->clientAddr, pBufferObj->addrLen); AddClientSocketObj(dwConnID, pSocketObj); } else return dwConnID; } if(FireAccept(dwConnID, &pBufferObj->clientAddr) == ISocketListener::HR_ERROR) { AddFreeSocketObj(dwConnID); dwConnID = 0; } return dwConnID; }
CONNID CUdpServer::HandleAccept(TUdpBufferObj* pBufferObj) { CONNID dwConnID = FindConnectionID(&pBufferObj->remoteAddr); if(dwConnID == 0) { CCriSecLock locallock(m_csAccept); dwConnID = FindConnectionID(&pBufferObj->remoteAddr); if(dwConnID != 0) return dwConnID; else { dwConnID = ::GenerateConnectionID(); TUdpSocketObj* pSocketObj = GetFreeSocketObj(dwConnID); memcpy(&pSocketObj->remoteAddr, &pBufferObj->remoteAddr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); } } if(FireAccept(dwConnID, &pBufferObj->remoteAddr) == HR_ERROR) { AddFreeSocketObj(dwConnID); dwConnID = 0; } return dwConnID; }
EnHandleResult CTcpServer::TriggerFireAccept(TSocketObj* pSocketObj) { EnHandleResult rs = TRIGGER(FireAccept(pSocketObj)); pSocketObj->csRecv.Unlock(); return rs; }
void CIocpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { DeleteAcceptSocket(pBufferObj->client); int iLocalSockaddrLen; int iRemoteSockaddrLen; SOCKADDR* pLocalSockAddr; SOCKADDR* pRemoteSockAddr; //将AcceptEx接受的第一块数据中的本地和远程机器的地址返回给用户 m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (SOCKADDR **)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR **)&pRemoteSockAddr, &iRemoteSockaddrLen ); TSocketObj* pSocketObj = GetFreeSocketObj(); pSocketObj->socket = pBufferObj->client; pSocketObj->connID = ::InterlockedIncrement((LONG*)&m_dwConnID); //InterlockedIncrement 对m_dwConnID加1 在对m_dwConnID访问的时候其他线程不能访问这个变量 memcpy(&pSocketObj->clientAddr, pRemoteSockAddr, sizeof(SOCKADDR_IN)); //远程机器的地址 AddClientSocketObj(pSocketObj->connID, pSocketObj); int result = 0; result = ::SSO_UpdateAcceptContext(pSocketObj->socket, soListen); ASSERT(result == 0); if(m_dwKeepAliveTimes > 0 && m_dwKeepAliveInterval > 0) { result = ::SSO_KeepAliveVals(pSocketObj->socket, TRUE, m_dwKeepAliveTimes, m_dwKeepAliveInterval); ASSERT(result == 0); } //result = ::SSO_NoDelay(pSocketObj->socket, TRUE); //ASSERT(result == 0); VERIFY(::CreateIoCompletionPort((HANDLE)pSocketObj->socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)); FireAccept(pSocketObj->connID); //打印连接 //接受连接成功后 投递一个接收 DoReceive(pSocketObj, pBufferObj); }
void CTcpServer::HandleAccept(SOCKET soListen, TBufferObj* pBufferObj) { ::PostQueuedCompletionStatus(m_hCompletePort, IOCP_SI_ACCEPT, 0, nullptr); int iLocalSockaddrLen; int iRemoteSockaddrLen; SOCKADDR* pLocalSockAddr; SOCKADDR* pRemoteSockAddr; m_pfnGetAcceptExSockaddrs ( pBufferObj->buff.buf, 0, sizeof(SOCKADDR_IN) + 16, sizeof(SOCKADDR_IN) + 16, (SOCKADDR **)&pLocalSockAddr, &iLocalSockaddrLen, (SOCKADDR **)&pRemoteSockAddr, &iRemoteSockaddrLen ); SOCKET socket = pBufferObj->client; CONNID dwConnID = ::GenerateConnectionID(m_dwConnID); TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, socket); memcpy(&pSocketObj->clientAddr, pRemoteSockAddr, sizeof(SOCKADDR_IN)); AddClientSocketObj(dwConnID, pSocketObj); VERIFY(::SSO_UpdateAcceptContext(socket, soListen) == NO_ERROR); BOOL bOnOff = (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0); VERIFY(::SSO_KeepAliveVals(socket, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR); if(FireAccept(dwConnID, socket) != ISocketListener::HR_ERROR) { VERIFY(::CreateIoCompletionPort((HANDLE)socket, m_hCompletePort, (ULONG_PTR)pSocketObj, 0)); DoReceive(dwConnID, pSocketObj, pBufferObj); } else { AddFreeSocketObj(dwConnID, SCF_NONE); AddFreeBufferObj(pBufferObj); } }
EnHandleResult CTcpServer::TriggerFireAccept(TSocketObj* pSocketObj) { CReentrantSpinLock locallock(pSocketObj->csRecv); return FireAccept(pSocketObj); }