code_app/sdk/webservice/main.cpp

291 lines
7.5 KiB
C++
Raw Normal View History

#include "base/HGDef.h"
#include "base/HGInc.h"
2022-09-23 09:53:00 +00:00
#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"
2024-08-01 05:46:17 +00:00
#include "log/log.h"
2022-05-03 10:25:52 +00:00
2024-08-01 05:46:17 +00:00
HLOG g_hLog = nullptr;
//HGMsgPump msgPumpV1 = NULL;
2024-04-19 03:40:33 +00:00
HGMsgPump msgPumpV2 = NULL;
2024-08-01 05:46:17 +00:00
#if defined(OEM_HANWANG)
2024-08-27 11:54:41 +00:00
const char* oem = "HanvonScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_LISICHENG)
2024-08-27 11:54:41 +00:00
const char* oem = "LanxumScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_CANGTIAN)
2024-08-27 11:54:41 +00:00
const char* oem = "CumtennScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_ZHONGJING)
2024-08-27 11:54:41 +00:00
const char* oem = "MicrotekScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_ZIGUANG)
2024-08-27 11:54:41 +00:00
const char* oem = "UniScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_NEUTRAL)
2024-08-27 11:54:41 +00:00
const char* oem = "NeuScan";
2024-08-01 05:46:17 +00:00
#elif defined(OEM_DELI)
2024-08-27 11:54:41 +00:00
const char* oem = "DeliScan";
#elif defined(OEM_MEISONGDA)
const char *oem = "MaxsoundScan";
#elif defined(OEM_GUANGDIANTONG)
const char *oem = "ToecScan";
2024-08-01 05:46:17 +00:00
#else
2024-08-27 11:54:41 +00:00
const char* oem = "HuaGoScan";
2024-08-01 05:46:17 +00:00
#endif
static void HGAPI CrashFunc(HGPointer crashAddr, HGPointer param)
2022-09-23 09:53:00 +00:00
{
HGChar logPath[256];
HGBase_GetConfigPath(logPath, 256);
2022-09-23 09:53:00 +00:00
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)
2022-05-03 10:25:52 +00:00
{
(void)thread;
HGMsgPump msgPump = (HGMsgPump)param;
2022-05-03 10:25:52 +00:00
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();
}
2022-05-03 10:25:52 +00:00
}
2023-03-20 13:03:28 +00:00
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
2024-04-19 03:40:33 +00:00
#ifndef _DEBUG
2024-04-19 03:40:33 +00:00
#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")
2024-08-27 11:54:41 +00:00
#elif defined(OEM_MEISONGDA)
#define SERVICE_NAME TEXT("MSDWebService")
#elif defined(OEM_GUANGDIANTONG)
#define SERVICE_NAME TEXT("GDTWebService")
2022-05-03 10:25:52 +00:00
#else
2024-04-19 03:40:33 +00:00
#define SERVICE_NAME TEXT("HGWebService")
2022-05-03 10:25:52 +00:00
#endif
2024-04-19 03:40:33 +00:00
SERVICE_STATUS_HANDLE ServiceStatusHandle = NULL;
SERVICE_STATUS ServiceStatus = {0};
void WINAPI ServiceCtrlHandler(DWORD dwControl)
2022-05-03 10:25:52 +00:00
{
2024-04-19 03:40:33 +00:00
switch (dwControl)
{
case SERVICE_CONTROL_STOP: // 暂停控制
ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止
//HGBase_ExitMsgPump(msgPumpV1);
2024-04-19 03:40:33 +00:00
HGBase_ExitMsgPump(msgPumpV2);
break;
case SERVICE_CONTROL_SHUTDOWN:// 关机控制
ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止
//HGBase_ExitMsgPump(msgPumpV1);
2024-04-19 03:40:33 +00:00
HGBase_ExitMsgPump(msgPumpV2);
break;
default:
break;
}
2022-09-23 09:53:00 +00:00
2024-04-19 03:40:33 +00:00
// 设置服务状态
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
}
2022-05-03 10:25:52 +00:00
2024-04-19 03:40:33 +00:00
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);
2023-03-20 13:03:28 +00:00
2024-04-19 03:40:33 +00:00
ServiceStatus.dwCurrentState = SERVICE_RUNNING; // 成功运行则把状态设置为运行状态
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
2023-03-20 13:03:28 +00:00
HGBase_CloseThread(threadV2);
threadV2 = NULL;
HGBase_DestroyMsgPump(msgPumpV2);
msgPumpV2 = NULL;
//HGBase_CloseThread(threadV1);
//threadV1 = NULL;
//HGBase_DestroyMsgPump(msgPumpV1);
//msgPumpV1 = NULL;
2024-04-19 03:40:33 +00:00
curl_global_cleanup();
WSACleanup();
}
2024-04-19 03:40:33 +00:00
int main()
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
g_hLog = EnableLog(nullptr, oem, "app");
2024-04-19 03:40:33 +00:00
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);
2024-08-01 05:46:17 +00:00
DisableLog(g_hLog);
g_hLog = nullptr;
2024-04-19 03:40:33 +00:00
return 0;
}
2024-04-19 03:40:33 +00:00
#else
int main()
{
WSADATA ws = { 0 };
int ret = WSAStartup(MAKEWORD(2, 2), &ws);
assert(0 == ret);
curl_global_init(CURL_GLOBAL_ALL);
g_hLog = EnableLog(nullptr, oem, "app");
2024-08-01 05:46:17 +00:00
//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();
2024-08-01 05:46:17 +00:00
DisableLog(g_hLog);
g_hLog = nullptr;
return 0;
}
#endif
#else
2024-04-19 03:40:33 +00:00
int main()
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
curl_global_init(CURL_GLOBAL_ALL);
g_hLog = EnableLog(nullptr, oem, "app");
2024-08-01 05:46:17 +00:00
//HGBase_CreateMsgPump(&msgPumpV1);
//HGThread threadV1 = NULL;
//HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1);
2024-04-19 03:40:33 +00:00
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;
2024-04-19 03:40:33 +00:00
curl_global_cleanup();
2024-08-01 05:46:17 +00:00
DisableLog(g_hLog);
g_hLog = nullptr;
2024-04-19 03:40:33 +00:00
return 0;
}
#endif