bool CServer::CreateListenSocket(CStdString ports, bool ssl) { bool success = true; ports += _T(" "); int pos = ports.Find(' '); while (pos > 0) { CStdString ipBindings = (m_pOptions ? m_pOptions->GetOption(OPTION_IPBINDINGS) : _T("*")); int nPort = _ttoi(ports.Left(pos)); ports = ports.Mid(pos + 1); pos = ports.Find(' '); CStdString str; str.Format(_T("Creating listen socket on port %d..."), nPort); ShowStatus(str, 0); if (ipBindings.empty() || ipBindings == _T("*")) { ipBindings = _T("::0 0.0.0.0"); } bool bError = false; str.Format(_T("Failed to bind the listen socket on port %d to the following IPs:"), nPort); ipBindings += _T(" "); while (ipBindings != _T("")) { int pos = ipBindings.Find(' '); if (pos == -1) break; CStdString ip = ipBindings.Left(pos); ipBindings = ipBindings.Mid(pos + 1); CListenSocket *pListenSocket = new CListenSocket(*this, m_ThreadArray, ssl); int family; if (ip.Find(':') != -1) family = AF_INET6; else family = AF_INET; if (!pListenSocket->Create(nPort, SOCK_STREAM, FD_ACCEPT, ip, family) || !pListenSocket->Listen(32)) { delete pListenSocket; bError = true; str += _T(" ") + ip; } else m_ListenSocketList.push_back(pListenSocket); } if (bError) { ShowStatus(str, 1); success = false; } } return success; }
/*................无用...............*/ BOOL CWorkThread::Accept(CListenSocket& ListenSocket) { CAsyncSocket asAccept; if(!ListenSocket.Accept(asAccept)) return FALSE; CString strOut; strOut.Format(_T("CPoolThread::Accept() socket :%d Accept!\n"),asAccept.m_hSocket); TRACE(strOut); //线程池是否已满 if(!WorkItem::IsPoolFull()) { if(fpQueueUserWorkItem != NULL) { // fpQueueUserWorkItem(WorkItem::ClientWorkItemThread, (LPVOID)asAccept.Detach(), WT_EXECUTELONGFUNCTION); } else { //启动WorkItem类的ClientWorkThread线程 AfxBeginThread(WorkItem::ClientWorkThread, (LPVOID)asAccept.Detach()); } } else { // refuse request,close socket!拒绝客户端请求,关闭socket asAccept.Close(); } return TRUE; }
bool Setup( void ){ if( m_ListenSock.Create( 50001 ) ){ SESSIONS::New( m_ListenSock, 0xFF, this ); return true; } return false; }
DWORD HandleEvent( DWORD mask ){ SOCKADDR SockAddr = {0}; int nLen = sizeof(SOCKADDR); CSocket sock = m_ListenSock.Accept(SockAddr, nLen); CSession* pSession = SESSIONS::New( sock, mask, new Connection ); if( pSession ) { SESSIONS::Delete( *pSession ); } return 0; }