#include "base/HGDef.h" #include "base/HGInc.h" #include "base/HGCrash.h" #include "base/HGThread.h" #include "base/HGUtility.h" #include "base/HGIni.h" #include "base/HGMsgPump.h" #include "ManagerV1.h" #include "ManagerV2.h" #include "HttpServer.h" #include "SockIoServer.h" #include "WSServer.h" #include "MsgPumpCallback.h" #include "curl/curl.h" //HGMsgPump msgPumpV1 = NULL; HGMsgPump msgPumpV2 = NULL; static void HGAPI CrashFunc(HGPointer crashAddr, HGPointer param) { HGChar logPath[256]; HGBase_GetLogFilePath(logPath, 256); strcat(logPath, "crash.dmp"); HGBase_MakeCrashFile(logPath); } static void HGAPI ThreadFuncV1(HGThread thread, HGPointer param) { (void)thread; HGMsgPump msgPump = (HGMsgPump)param; HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGChar cfgName[256]; sprintf(cfgName, "%s%s", cfgPath, "config.ini"); ver_1::ManagerV1 manager(msgPump); ver_1::HttpServer httpServer(msgPump, &manager); ver_1::SockIoServer sockIoServer(msgPump, &manager); if (httpServer.Open(18999)) { if (sockIoServer.Open(28999)) { HGBase_RunMsgPump(msgPump, ver_1::HGMsgPumpCallback, NULL); sockIoServer.Close(); } httpServer.Close(); } } static void HGAPI ThreadFuncV2(HGThread thread, HGPointer param) { (void)thread; HGMsgPump msgPump = (HGMsgPump)param; HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGChar cfgName[256]; sprintf(cfgName, "%s%s", cfgPath, "config2.ini"); ver_2::ManagerV2 manager(msgPump); ver_2::WSServer wsServer(msgPump, &manager); HGInt port = 38999; HGBase_GetProfileInt(cfgName, "connect", "port", 38999, &port); if (wsServer.Open(port)) { HGBase_RunMsgPump(msgPump, ver_2::HGMsgPumpCallback, NULL); wsServer.Close(); } } #if defined(HG_CMP_MSC) #ifndef _DEBUG #if defined(OEM_HANWANG) #define SERVICE_NAME TEXT("HWWebService") #elif defined(OEM_LISICHENG) #define SERVICE_NAME TEXT("LSCWebService") #elif defined(OEM_CANGTIAN) #define SERVICE_NAME TEXT("CTSWebService") #elif defined(OEM_ZHONGJING) #define SERVICE_NAME TEXT("ZJWebService") #elif defined(OEM_ZIGUANG) #define SERVICE_NAME TEXT("ZGWebService") #elif defined(OEM_NEUTRAL) #define SERVICE_NAME TEXT("NEUWebService") #elif defined(OEM_DELI) #define SERVICE_NAME TEXT("DLWebService") #else #define SERVICE_NAME TEXT("HGWebService") #endif SERVICE_STATUS_HANDLE ServiceStatusHandle = NULL; SERVICE_STATUS ServiceStatus = {0}; void WINAPI ServiceCtrlHandler(DWORD dwControl) { switch (dwControl) { case SERVICE_CONTROL_STOP: // 暂停控制 ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止 //HGBase_ExitMsgPump(msgPumpV1); HGBase_ExitMsgPump(msgPumpV2); break; case SERVICE_CONTROL_SHUTDOWN:// 关机控制 ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止 //HGBase_ExitMsgPump(msgPumpV1); HGBase_ExitMsgPump(msgPumpV2); break; default: break; } // 设置服务状态 SetServiceStatus(ServiceStatusHandle, &ServiceStatus); } void WINAPI ServiceMain(DWORD dwNumServicesArgs, LPTSTR* lpServiceArgVectors) { ServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler); if (NULL == ServiceStatusHandle) { return; } ServiceStatus.dwServiceType = SERVICE_WIN32; ServiceStatus.dwCurrentState = SERVICE_START_PENDING; // 服务正在启动 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP; // 接受关机与暂停两种控制 ServiceStatus.dwWin32ExitCode = 0; // 下面四个默认都为0 ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwWaitHint = 0; WSADATA ws = { 0 }; int ret = WSAStartup(MAKEWORD(2, 2), &ws); assert(0 == ret); curl_global_init(CURL_GLOBAL_ALL); //HGBase_CreateMsgPump(&msgPumpV1); //HGThread threadV1 = NULL; //HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1); HGBase_CreateMsgPump(&msgPumpV2); HGThread threadV2 = NULL; HGBase_OpenThread(ThreadFuncV2, msgPumpV2, &threadV2); ServiceStatus.dwCurrentState = SERVICE_RUNNING; // 成功运行则把状态设置为运行状态 SetServiceStatus(ServiceStatusHandle, &ServiceStatus); HGBase_CloseThread(threadV2); threadV2 = NULL; HGBase_DestroyMsgPump(msgPumpV2); msgPumpV2 = NULL; //HGBase_CloseThread(threadV1); //threadV1 = NULL; //HGBase_DestroyMsgPump(msgPumpV1); //msgPumpV1 = NULL; curl_global_cleanup(); WSACleanup(); } int main() { HGBase_RegisterCrashFunc(CrashFunc, NULL); TCHAR serviceName[128]; lstrcpy(serviceName, SERVICE_NAME); SERVICE_TABLE_ENTRY tableEntry[2]; tableEntry[0].lpServiceName = serviceName; // 服务程序名称 tableEntry[0].lpServiceProc = ServiceMain; // 服务程序入口 tableEntry[1].lpServiceName = NULL; tableEntry[1].lpServiceProc = NULL; StartServiceCtrlDispatcher(tableEntry); return 0; } #else int main() { WSADATA ws = { 0 }; int ret = WSAStartup(MAKEWORD(2, 2), &ws); assert(0 == ret); curl_global_init(CURL_GLOBAL_ALL); //HGBase_CreateMsgPump(&msgPumpV1); //HGThread threadV1 = NULL; //HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1); HGBase_CreateMsgPump(&msgPumpV2); HGThread threadV2 = NULL; HGBase_OpenThread(ThreadFuncV2, msgPumpV2, &threadV2); HGBase_CloseThread(threadV2); threadV2 = NULL; HGBase_DestroyMsgPump(msgPumpV2); msgPumpV2 = NULL; //HGBase_CloseThread(threadV1); //threadV1 = NULL; //HGBase_DestroyMsgPump(msgPumpV1); //msgPumpV1 = NULL; curl_global_cleanup(); WSACleanup(); return 0; } #endif #else int main() { HGBase_RegisterCrashFunc(CrashFunc, NULL); curl_global_init(CURL_GLOBAL_ALL); //HGBase_CreateMsgPump(&msgPumpV1); //HGThread threadV1 = NULL; //HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1); HGBase_CreateMsgPump(&msgPumpV2); HGThread threadV2 = NULL; HGBase_OpenThread(ThreadFuncV2, msgPumpV2, &threadV2); HGBase_CloseThread(threadV2); threadV2 = NULL; HGBase_DestroyMsgPump(msgPumpV2); msgPumpV2 = NULL; //HGBase_CloseThread(threadV1); //threadV1 = NULL; //HGBase_DestroyMsgPump(msgPumpV1); //msgPumpV1 = NULL; curl_global_cleanup(); return 0; } #endif