code_app/sdk/webservice/main.cpp

172 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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"
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 ThreadFunc(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");
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();
}
}
}
#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
#if defined(HG_CMP_MSC)
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE iPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
#else
int main()
#endif
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
#if defined(HG_CMP_MSC)
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;
}
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);
HGThread thread = NULL;
HGBase_OpenThread(ThreadFunc, msgPump, &thread);
#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));
lstrcpyA(nid.szTip, "扫描服务程序(连接数 0");
Shell_NotifyIconA(NIM_ADD, &nid);
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#endif
HGBase_CloseThread(thread);
thread = NULL;
HGBase_DestroyMsgPump(msgPump);
msgPump = NULL;
curl_global_cleanup();
#if defined(HG_CMP_MSC)
WSACleanup();
CloseHandle(hMutex);
#endif
return 0;
}