bool CControlListen::AddListen( const char* pListenIP, uint32 u4Port, uint8 u1IPType, int nPacketParseID) { bool blState = App_ConnectAcceptorManager::instance()->CheckIPInfo(pListenIP, u4Port); if(true == blState) { //当前监听已经存在,不可以重复建设 OUR_DEBUG((LM_INFO, "[CServerManager::AddListen](%s:%d) is exist.\n", pListenIP, u4Port)); return false; } ACE_INET_Addr listenAddr; //判断IPv4还是IPv6 int nErr = 0; if (u1IPType == TYPE_IPV4) { nErr = listenAddr.set(u4Port, pListenIP); } else { nErr = listenAddr.set(u4Port, pListenIP, 1, PF_INET6); } if (nErr != 0) { OUR_DEBUG((LM_INFO, "[CControlListen::AddListen](%s:%d)set_address error[%d].\n", pListenIP, u4Port, errno)); return false; } //得到接收器 ConnectAcceptor* pConnectAcceptor = App_ConnectAcceptorManager::instance()->GetNewConnectAcceptor(); if (NULL == pConnectAcceptor) { OUR_DEBUG((LM_INFO, "[CControlListen::AddListen](%s:%d)pConnectAcceptor is NULL.\n", pListenIP, u4Port)); return false; } pConnectAcceptor->SetPacketParseInfoID(nPacketParseID); int nRet = pConnectAcceptor->open2(listenAddr, App_ReactorManager::instance()->GetAce_Reactor(REACTOR_CLIENTDEFINE), ACE_NONBLOCK, (int)GetXmlConfigAttribute(xmlNetWorkMode)->BackLog); if (-1 == nRet) { OUR_DEBUG((LM_INFO, "[CControlListen::AddListen] Listen from [%s:%d] error(%d).\n", listenAddr.get_host_addr(), listenAddr.get_port_number(), errno)); return false; } OUR_DEBUG((LM_INFO, "[CControlListen::AddListen] Listen from [%s:%d] OK.\n", listenAddr.get_host_addr(), listenAddr.get_port_number())); return true; }
void CConnectAcceptorManager::Close() { for (int i = 0; i < (int)m_vecConnectAcceptor.size(); i++) { ConnectAcceptor* pConnectAcceptor = (ConnectAcceptor*)m_vecConnectAcceptor[i]; if (NULL != pConnectAcceptor) { pConnectAcceptor->close(); delete pConnectAcceptor; pConnectAcceptor = NULL; } } m_vecConnectAcceptor.clear(); m_nAcceptorCount = 0; }
bool CConnectAcceptorManager::CheckIPInfo(const char* pIP, uint32 n4Port) { //找到符合条件指定的端口停止监听 for(vecConnectAcceptor::iterator b = m_vecConnectAcceptor.begin(); b != m_vecConnectAcceptor.end(); b++) { ConnectAcceptor* pConnectAcceptor = (ConnectAcceptor*)(*b); if (NULL != pConnectAcceptor) { if(ACE_OS::strcmp(pConnectAcceptor->GetListenIP(), pIP) == 0 && pConnectAcceptor->GetListenPort() == n4Port) { return true; } } } return false; }
bool CConnectAcceptorManager::Close(const char* pIP, uint32 n4Port) { //找到符合条件指定的端口停止监听 for(vecConnectAcceptor::iterator b = m_vecConnectAcceptor.begin(); b != m_vecConnectAcceptor.end(); b++) { ConnectAcceptor* pConnectAcceptor = (ConnectAcceptor*)(*b); if (NULL != pConnectAcceptor) { if(ACE_OS::strcmp(pConnectAcceptor->GetListenIP(), pIP) == 0 && pConnectAcceptor->GetListenPort() == n4Port) { pConnectAcceptor->close(); SAFE_DELETE(pConnectAcceptor); m_vecConnectAcceptor.erase(b); break; } } } return true; }
uint32 CControlListen::GetListenCount() { m_vecListenList.clear(); if (0 == App_ConnectAcceptorManager::instance()->GetCount()) { //监控尚未启动,需要从配置文件中获取 int nServerPortCount = (int)GetXmlConfigAttribute(xmlTCPServerIPs)->vec.size(); for (int i = 0; i < nServerPortCount; i++) { _ControlInfo objInfo; sprintf_safe(objInfo.m_szListenIP, MAX_BUFF_20, "%s", GetXmlConfigAttribute(xmlTCPServerIPs)->vec[i].ip.c_str()); objInfo.m_u4Port = GetXmlConfigAttribute(xmlTCPServerIPs)->vec[i].port; m_vecListenList.push_back(objInfo); } } else { for (int i = 0; i < App_ConnectAcceptorManager::instance()->GetCount(); i++) { ConnectAcceptor* pConnectAcceptor = App_ConnectAcceptorManager::instance()->GetConnectAcceptor(i); if (NULL != pConnectAcceptor) { _ControlInfo objInfo; sprintf_safe(objInfo.m_szListenIP, MAX_BUFF_20, "%s", pConnectAcceptor->GetListenIP()); objInfo.m_u4Port = pConnectAcceptor->GetListenPort(); m_vecListenList.push_back(objInfo); } } } return (uint32)m_vecListenList.size(); }
bool CServerManager::Start() { //注册信号量 if(0 != App_SigHandler::instance()->RegisterSignal(App_ReactorManager::instance()->GetAce_Reactor(REACTOR_CLIENTDEFINE))) { return false; } //启动TCP监听 int nServerPortCount = App_MainConfig::instance()->GetServerPortCount(); for(int i = 0 ; i < nServerPortCount; i++) { ACE_INET_Addr listenAddr; _ServerInfo* pServerInfo = App_MainConfig::instance()->GetServerPort(i); if(NULL == pServerInfo) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]pServerInfo [%d] is NULL.\n", i)); return false; } //判断IPv4还是IPv6 int nErr = 0; if(pServerInfo->m_u1IPType == TYPE_IPV4) { nErr = listenAddr.set(pServerInfo->m_nPort, pServerInfo->m_szServerIP); } else { nErr = listenAddr.set(pServerInfo->m_nPort, pServerInfo->m_szServerIP, 1, PF_INET6); } if(nErr != 0) { OUR_DEBUG((LM_INFO, "[CServerManager::Start](%d)set_address error[%d].\n", i, errno)); return false; } //得到接收器 ConnectAcceptor* pConnectAcceptor = m_ConnectAcceptorManager.GetConnectAcceptor(i); if(NULL == pConnectAcceptor) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]pConnectAcceptor[%d] is NULL.\n", i)); return false; } int nRet = pConnectAcceptor->open2(listenAddr, App_ReactorManager::instance()->GetAce_Reactor(REACTOR_CLIENTDEFINE), ACE_NONBLOCK, (int)App_MainConfig::instance()->GetBacklog()); if(-1 == nRet) { OUR_DEBUG((LM_INFO, "[CServerManager::Start] pConnectAcceptor->open[%d] is error.\n", i)); OUR_DEBUG((LM_INFO, "[CServerManager::Start] Listen from [%s:%d] error(%d).\n",listenAddr.get_host_addr(), listenAddr.get_port_number(), errno)); return false; } OUR_DEBUG((LM_INFO, "[CServerManager::Start] Listen from [%s:%d] OK.\n",listenAddr.get_host_addr(), listenAddr.get_port_number())); } //启动UDP监听 int nUDPServerPortCount = App_MainConfig::instance()->GetUDPServerPortCount(); for(int i = 0 ; i < nUDPServerPortCount; i++) { ACE_INET_Addr listenAddr; _ServerInfo* pServerInfo = App_MainConfig::instance()->GetUDPServerPort(i); if(NULL == pServerInfo) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]UDP pServerInfo [%d] is NULL.\n", i)); return false; } int nErr = 0; if(pServerInfo->m_u1IPType == TYPE_IPV4) { nErr = listenAddr.set(pServerInfo->m_nPort, pServerInfo->m_szServerIP); } else { nErr = listenAddr.set(pServerInfo->m_nPort, pServerInfo->m_szServerIP, 1, PF_INET6); } if(nErr != 0) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]UDP (%d)set_address error[%d].\n", i, errno)); return false; } //得到接收器 CReactorUDPHander* pReactorUDPHandler = App_ReUDPManager::instance()->Create(); if(NULL == pReactorUDPHandler) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]UDP pReactorUDPHandler[%d] is NULL.\n", i)); return false; } int nRet = pReactorUDPHandler->OpenAddress(listenAddr, App_ReactorManager::instance()->GetAce_Reactor(REACTOR_CLIENTDEFINE)); if(-1 == nRet) { OUR_DEBUG((LM_INFO, "[CServerManager::Start] UDP Listen from [%s:%d] error(%d).\n",listenAddr.get_host_addr(), listenAddr.get_port_number(), errno)); return false; } OUR_DEBUG((LM_INFO, "[CServerManager::Start] UDP Listen from [%s:%d] OK.\n",listenAddr.get_host_addr(), listenAddr.get_port_number())); } //启动后台管理端口监听 if(App_MainConfig::instance()->GetConsoleSupport() == CONSOLE_ENABLE) { ACE_INET_Addr listenConsoleAddr; int nErr = 0; if(App_MainConfig::instance()->GetConsoleIPType() == TYPE_IPV4) { nErr = listenConsoleAddr.set(App_MainConfig::instance()->GetConsolePort(), App_MainConfig::instance()->GetConsoleIP()); } else { nErr = listenConsoleAddr.set(App_MainConfig::instance()->GetConsolePort(), App_MainConfig::instance()->GetConsoleIP(), 1, PF_INET6); } if(nErr != 0) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]listenConsoleAddr set_address error[%d].\n", errno)); return false; } int nRet = m_ConnectConsoleAcceptor.open(listenConsoleAddr, App_ReactorManager::instance()->GetAce_Reactor(REACTOR_CLIENTDEFINE), ACE_NONBLOCK); if(-1 == nRet) { OUR_DEBUG((LM_INFO, "[CServerManager::Start] pConnectAcceptor->open is error.\n")); OUR_DEBUG((LM_INFO, "[CServerManager::Start] Listen from [%s:%d] error(%d).\n",listenConsoleAddr.get_host_addr(), listenConsoleAddr.get_port_number(), errno)); return false; } } //启动日志服务线程 if(0 != AppLogManager::instance()->Start()) { AppLogManager::instance()->WriteLog(LOG_SYSTEM, "[CServerManager::Init]AppLogManager is ERROR."); } else { AppLogManager::instance()->WriteLog(LOG_SYSTEM, "[CServerManager::Init]AppLogManager is OK."); } /* //设置定时器策略(使用高精度定时器) (void) ACE_High_Res_Timer::global_scale_factor (); auto_ptr<ACE_Timer_Heap_Variable_Time_Source> tq(new ACE_Timer_Heap_Variable_Time_Source); tq->set_time_policy(&ACE_High_Res_Timer::gettimeofday_hr); App_TimerManager::instance()->timer_queue(tq); tq.release(); */ //启动定时器 if(0 != App_TimerManager::instance()->activate()) { OUR_DEBUG((LM_INFO, "[CProServerManager::Start]App_TimerManager::instance()->Start() is error.\n")); return false; } //先启动其他的Reactor,最后启动原始的Reactor,因为原始的会挂起线程,所以最后启动一下。 if(!App_ReactorManager::instance()->StartReactor()) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]App_ReactorManager::instance()->StartReactor is error.\n")); return false; } //启动中间服务器链接管理器 App_ClientReConnectManager::instance()->Init(App_ReactorManager::instance()->GetAce_Reactor(REACTOR_POSTDEFINE)); App_ClientReConnectManager::instance()->StartConnectTask(App_MainConfig::instance()->GetConnectServerCheck()); //初始化模块加载,因为这里可能包含了中间服务器连接加载 bool blState = App_ModuleLoader::instance()->LoadModule(App_MainConfig::instance()->GetModulePath(), App_MainConfig::instance()->GetModuleString()); if(false == blState) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]LoadModule is error.\n")); return false; } //开始消息处理线程 App_MessageServiceGroup::instance()->Start(); //开始启动链接发送定时器 App_ConnectManager::instance()->StartTimer(); //最后启动反应器 OUR_DEBUG((LM_INFO, "[CServerManager::Start]App_ReactorManager::instance()->StartReactorDefault begin....\n")); if(!App_ReactorManager::instance()->StartReactorDefault()) { OUR_DEBUG((LM_INFO, "[CServerManager::Start]App_ReactorManager::instance()->StartReactorDefault is error.\n")); return false; } return true; }