//-------------------------------------------------------------------------------- BOOL CNTService::ProcessCommandLine(LPCTSTR pCmdLine) { AFX_MANAGE_STATE_IF_DLL CCmdLine cmdLine(pCmdLine); int nIndex; if(cmdLine.HasParam('D', nIndex) || cmdLine.HasParam(_T("-debug"), nIndex)) return FALSE; if(cmdLine.HasParam('I', nIndex) || cmdLine.HasParam(_T("-install"), nIndex)) { InstallService(); return TRUE; } if(cmdLine.HasParam('R', nIndex) || cmdLine.HasParam(_T("-remove"), nIndex)) { RemoveService(); return TRUE; } if(cmdLine.HasParam('E', nIndex) || cmdLine.HasParam(_T("-end"), nIndex) || cmdLine.HasParam(_T("-stop"), nIndex)) { EndService(); return TRUE; } if(cmdLine.HasParam('S', nIndex) || cmdLine.HasParam(_T("-start"), nIndex)) { StartupService(); return TRUE; } return FALSE; }
//*================================================================================= //*原型: void TServer::Shutdown() //*功能: Shutdown服务器 //*参数: 无 //*返回: 无 //*说明: WINNT服务器基类 //*================================================================================= void TServer::Shutdown() { EndService(m_pszServiceName); PostQuitMessage(0); SetEvent(m_hShutdown); Sleep(2000); }
//---------------------------------------------------------------- SelectServer::~SelectServer() { try{ EndService(); }catch(ConnectionException &e){ fprintf(stderr, "Exception occurs when endding service, description %s\n", e.what()); } }
//析构函数 CKernelService::~CKernelService() { #ifdef _DEBUG LOG(INFO) <<"~CKernelService"; #endif //停止服务 EndService(); return; }
//析构函数 CTimerEngine::~CTimerEngine(void) { INT_PTR i = 0; //停止服务 EndService(); //清理内存 for(CTimerItemMap::iterator iter = m_TimerItemFree.begin(); iter != m_TimerItemFree.end(); ++iter) { delete iter->second; } for(CTimerItemMap::iterator iter = m_TimerItemActive.begin(); iter != m_TimerItemActive.end(); ++iter) { delete iter->second; } m_TimerItemFree.clear(); m_TimerItemActive.clear(); }
void TServer::StopServer(LPCTSTR pszServiceName) { EndService(pszServiceName); if( bDebug ) return ; SERVICE_STATUS ssStatus; SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if( schSCManager == NULL ) { throw TException("不能打开服务管理器!", GetLastError()); } SC_HANDLE schService = OpenService(schSCManager, pszServiceName, SC_MANAGER_ALL_ACCESS); if( schService == NULL ) { CloseServiceHandle(schSCManager); throw TException("停止服务程序失败!", GetLastError()); } if( !ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus) ) { CloseServiceHandle(schService); CloseServiceHandle(schSCManager); throw TException("停止服务程序失败!", GetLastError()); } while( QueryServiceStatus(schService, &ssStatus) ) { if( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) Sleep(1000); else break; } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); }
//开始服务 bool CCenterService::BeginService() { try { //变量定义 bool bSuccess=false; //创建服务对象 if (m_ServiceEngine.IsValid()==false) { if (m_ServiceEngine.CreateInstance()==false) { throw m_ServiceEngine.GetErrorMessage(); } if (m_pIEventService!=NULL) { m_ServiceEngine->SetEventService(m_pIEventService); } //注册调度外挂服务 IUnknownEx * pIAttemperEngine=GET_OBJECT_INTERFACE(m_AttemperSink,IUnknownEx); if (m_ServiceEngine->SetAttemperEngineSink(pIAttemperEngine)==false) { TraceEventString(TEXT("注册调度外挂服务失败"),Level_Exception); throw 0; } //注册数据库外挂服务 IUnknownEx * pIDataBaseSink=GET_OBJECT_INTERFACE(m_DataBaseSink,IUnknownEx); if (m_ServiceEngine->SetDataBaseSink(pIDataBaseSink)==false) { TraceEventString(TEXT("注册数据库外挂服务失败"),Level_Exception); throw 0; } } //判断状态 if (m_ServiceEngine->IsService()==true) { TraceEventString(TEXT("中心服务已经了启动了"),Level_Warning); return true; } //配置服务 m_InitParamter.LoadInitParamter(); if (m_DataBaseSink.InitDataBaseSink(&m_InitParamter,m_ServiceEngine.GetInterface())==false) { throw 0; } if (m_AttemperSink.InitAttemperSink(&m_InitParamter,m_ServiceEngine.GetInterface())==false) { throw 0; } if (m_ServiceEngine->InitServiceEngine(m_InitParamter.m_wListenPort,m_InitParamter.m_wMaxConnect)==false) { throw 0; } //启动服务 if (m_ServiceEngine->BeginService()==false) { throw 0; } return true; } catch (LPCTSTR pszErrorMsg) { TraceEventString(pszErrorMsg,Level_Exception); TraceEventString(TEXT("中心服务启动失败"),Level_Exception); EndService(); } catch (...) { TraceEventString(TEXT("中心服务启动失败"),Level_Exception); EndService(); } return false; }
CLFGameServer::~CLFGameServer() { EndService(); }
//*================================================================================= //*原型: void TServer::RunService(LPCTSTR pszServiceName, //* LPCTSTR pszDisplayName, LPSTR lpCmdLine) //*功能: 服务器入口 //*参数: pszServiceName -- 服务器名称 //* pszDisplayName -- 服务器显示名称 //* lpCmdLine -- 命令行 //*返回: 无 //*说明: WINNT服务器基类 //*================================================================================= void TServer::RunService(LPCTSTR pszServiceName, LPCTSTR pszDisplayName, LPSTR lpCmdLine) { //检测操作系统版本 CheckOSVersion(); m_pszServiceName = pszServiceName ; m_pszDisplayName = pszDisplayName ; bDebug = TRUE; //add by lina 20050311 //帮助 if( !lstrcmp(lpCmdLine, "help") ) { PrintHelp(); return ; } //调试模式(仅供现场测试及内部测试用) else if( !lstrcmp(lpCmdLine, "debug") ) { bDebug = TRUE ; } //安装为服务器 else if( !lstrcmp(lpCmdLine, "install") ) { InstallService(m_pszServiceName, m_pszDisplayName); return ; } //删除服务器 else if( !lstrcmp(lpCmdLine, "delete") ) { DeleteService(m_pszServiceName); return ; } //Start服务器 else if( !lstrcmp(lpCmdLine, "start") ) { StartServer(m_pszServiceName); return ; } //停止服务器 else if( !lstrcmp(lpCmdLine, "stop") ) { WriteLog("停止服务器!!!!........\n"); printf("停止服务器!!!!........\n"); //StopServer(m_pszServiceName); EndService(m_pszServiceName); return ; } CheckInstance(); /*SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = &sd; if( InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) ) { SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE); } m_hShutdown = CreateEvent(&sa, FALSE, FALSE, "__SMART_SERVER_SHUTDOWN__"); if( m_hShutdown == NULL ) { throw TException("Call CreateEvent() Faild!", GetLastError()); }*/ if( bDebug ) { SetConsole(); ServiceMain(0, NULL); } else { SERVICE_TABLE_ENTRY DispatchTable[] = { {(LPTSTR)m_pszServiceName, ServiceMain}, {NULL, NULL} }; //先试着从服务管理器中启动, 如失败, 则用无窗口的方式启动 if( !StartServiceCtrlDispatcher(DispatchTable)) { /* bDebug = TRUE; ServiceMain(0, NULL); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); */ } } }