void CServer::Run() { Init(); CSocket sock; if(!sock.Socket()) return; if(!sock.Bind( CConfig::GetInstance()->GetPortPID(ID_SERVER), CConfig::GetInstance()->GetAddrPID(ID_SERVER)) ) return; cout << "listening.." << endl; sock.Listen(); CWireThread* pThreadWire = new CWireThread(); pThreadWire->Start(); CGateThread* pThreadGate = new CGateThread(); pThreadGate->Start(); if(!sock.Accept(m_sockMain)) return; if(!sock.Accept(m_sockOT)) return; sock.Close(); cout << "accepted sockets!" << endl; COTThread* pThreadOT = new COTThread(); pThreadOT->Start(); RunMainThread(); m_sockMain.Close(); pThreadOT->Wait(); m_sockOT.Close(); pThreadGate->Wait(); pThreadWire->Wait(); Cleanup(); delete pThreadOT; delete pThreadGate; delete pThreadWire; }
void CClientManager::ShutdownServer( IN_ADDR addrHost, bool bForward ) { CSocket sock; sock.Socket( SOCK_DGRAM ); if( addrHost.s_addr==INADDR_ANY ){ //send shutdown message to all the known host addresses CObjLocker<CClientManager> locker(this); set<IN_ADDR>::iterator pos; for( pos=m_HostAddrs.begin(); pos!=m_HostAddrs.end(); ++pos ){ CMessageBase msg; msg.Init( NCM_SHUTDOWN, CSessionAddr(GetLocalInAddr(), 0), CSessionAddr(*pos, 0), bForward ); CSockAddrIn dstaddr( *pos, NCP_SERVER_PING_PORT ); SendMessageTo( sock, &msg, dstaddr ); } }else{ //send shutdown to a specific addresse CMessageBase msg; msg.Init( NCM_SHUTDOWN, CSessionAddr(GetLocalInAddr(), 0), CSessionAddr(addrHost, 0), bForward ); CSockAddrIn dstaddr( addrHost, NCP_SERVER_PING_PORT ); SendMessageTo( sock, &msg, dstaddr ); } }