code_app/sdk/webservice/main.cpp

172 lines
4.1 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"
2022-05-03 10:25:52 +00:00
static void HGAPI CrashFunc(HGPointer crashAddr, HGPointer param)
2022-09-23 09:53:00 +00:00
{
HGChar logPath[256];
HGBase_GetLogFilePath(logPath, 256);
strcat(logPath, "crash.dmp");
HGBase_MakeCrashFile(logPath);
}
static void HGAPI ThreadFunc(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, "config.ini");
HGInt verNum = 2;
HGBase_GetProfileInt(cfgName, "version", "verNum", 2, &verNum);
if (1 == verNum) // 使用V1版本接口
{
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();
}
}
else // 使用V2版本接口
{
ver_2::ManagerV2 manager(msgPump);
ver_2::WSServer wsServer(msgPump, &manager);
sprintf(cfgName, "%s%s", cfgPath, "config2.ini");
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
#if defined(HG_CMP_MSC)
#include <shellapi.h>
#include "resource.h"
#define WM_TRAY (WM_USER + 100)
ULONG taskbarMsgId = 0;
NOTIFYICONDATAA nid;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (WM_DESTROY == uMsg)
{
Shell_NotifyIconA(NIM_DELETE, &nid);
PostQuitMessage(0);
return 0;
}
else if (uMsg == taskbarMsgId)
{
//系统Explorer崩溃重启时重新加载托盘
Shell_NotifyIconA(NIM_ADD, &nid);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
#endif
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE iPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
#else
int main()
#endif
{
2022-09-23 09:53:00 +00:00
HGBase_RegisterCrashFunc(CrashFunc, NULL);
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
2023-03-20 13:03:28 +00:00
HANDLE hMutex = CreateMutexW(NULL, FALSE, L"{1A67EFC9-B41C-4CE6-B95A-A16958E7010F}");
assert(NULL != hMutex);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBoxA(NULL, "扫描服务已打开", "提示", 0);
CloseHandle(hMutex);
return -1;
}
2022-05-03 10:25:52 +00:00
WSADATA ws = { 0 };
int ret = WSAStartup(MAKEWORD(2, 2), &ws);
assert(0 == ret);
#endif
curl_global_init(CURL_GLOBAL_ALL);
HGMsgPump msgPump = NULL;
HGBase_CreateMsgPump(&msgPump);
2022-05-03 10:25:52 +00:00
HGThread thread = NULL;
HGBase_OpenThread(ThreadFunc, msgPump, &thread);
2023-03-20 13:03:28 +00:00
#if defined(HG_CMP_MSC)
WNDCLASSA wc = { 0 };
wc.style = NULL;
wc.hIcon = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "HGWebServie";
wc.hCursor = NULL;
RegisterClassA(&wc);
HWND hWnd = CreateWindowExA(WS_EX_TOOLWINDOW, "HGWebServie", "HGWebServie", WS_POPUP, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
taskbarMsgId = RegisterWindowMessageA("TaskbarCreated");
nid.cbSize = sizeof(NOTIFYICONDATAA);
nid.hWnd = hWnd;
nid.uID = IDI_ICON_HGWEBSERVICE;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;
nid.uCallbackMessage = WM_TRAY;
nid.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_HGWEBSERVICE));
2023-08-03 03:14:31 +00:00
lstrcpyA(nid.szTip, "扫描服务程序(连接数 0");
2023-03-20 13:03:28 +00:00
Shell_NotifyIconA(NIM_ADD, &nid);
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#endif
2022-05-03 10:25:52 +00:00
HGBase_CloseThread(thread);
thread = NULL;
HGBase_DestroyMsgPump(msgPump);
msgPump = NULL;
curl_global_cleanup();
2022-05-03 10:25:52 +00:00
#if defined(HG_CMP_MSC)
WSACleanup();
2023-03-20 13:03:28 +00:00
CloseHandle(hMutex);
2022-05-03 10:25:52 +00:00
#endif
return 0;
}