// RcfServer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include #include #include "RcfEcho.h" #include "RCF\RcfConfig.h" #include "CRedis.h" #include "HttpServer.h" // IsAlreadyRunning - 是否已经运行 BOOL IsAlreadyRunning() { char _szAppName[] = "RcfServer"; char _szTitle[] = "RcfServer"; BOOL bRet = FALSE; HANDLE hMutex = ::CreateMutex(NULL, TRUE, _szAppName); if (GetLastError() == ERROR_ALREADY_EXISTS) { if (hMutex) { ::ReleaseMutex(hMutex); } bRet = TRUE; HWND hWnd = FindWindow(_szAppName, _szTitle); if (::IsIconic(hWnd)) ::ShowWindow(hWnd, SW_RESTORE); //SW_SHOW SetForegroundWindow(hWnd); // 将主窗激活 ::SetForegroundWindow(::GetLastActivePopup(hWnd)); // 将主窗的对话框激活 } return bRet; } int main(int argc, char **argv) { // 禁止多个实例的同时运行 if (IsAlreadyRunning()) { std::cout << "当前程序已运行,本次运行无效,请关闭当前窗口" << std::endl; system("pause"); return 0; } try { RCF::RcfInit rcfInit; CRcfEcho echo; RCF::RcfServer server(RCF::TcpEndpoint("0.0.0.0", RcfConfig::GetInstance()->RcfPort())); server.bind(echo, "agent"); server.setThreadPool(RCF::ThreadPoolPtr(new RCF::ThreadPool(1, 5))); server.start(); std::cout << "RCF服务启动..." << "0.0.0.0 " << RcfConfig::GetInstance()->RcfPort() << std::endl; if (!CRedis::GetInstance()->connect(RcfConfig::GetInstance()->RedisIP().c_str(), RcfConfig::GetInstance()->RedisPort(), "")) std::cout << "redis 连接失败!" << std::endl; HttpServer httpServer; httpServer.run(RcfConfig::GetInstance()->HttpPort(), RcfConfig::GetInstance()->HttpsPort()); while (true) { std::this_thread::sleep_for(std::chrono::hours(24)); } } catch (const RCF::Exception & e) { std::cout << e.getErrorMessage() << std::endl; std::cout << "3秒后自动关闭" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(3)); return 0; } return 0; }