SATService.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SATService.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "SATService.h"
  5. #include "SATExecutor.h"
  6. #include "SATDevices.h"
  7. #include "SATTCPServer.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #endif
  11. // 唯一的应用程序对象
  12. CWinApp theApp;
  13. using namespace std;
  14. extern int test();
  15. // 工作启动回调函数;
  16. void CALLBACK WorkStart()
  17. {
  18. #ifdef DBG_SERVER
  19. Sleep(30000);
  20. #endif
  21. GLOBAL::GetIniInfo();
  22. GLOBAL::GetMacAddress();
  23. GLOBAL::GetLocalAddress();
  24. GLOBAL::WriteTextLog(GLOBAL::SAT_TCP, "SATService goto start, tcp port = %ld", GLOBAL::g_stSATConfig.dwTCPSvrPort);
  25. #if defined _DEBUG || defined RTEST
  26. printf("服务端TCP端口号=%ld\n", GLOBAL::g_stSATConfig.dwTCPSvrPort);
  27. #endif
  28. WindowsService::GetDebugPriv();
  29. // 启用SAT执行器;
  30. CSATExecutor::GetInstance()->StartWork();
  31. // 启动SAT TCP服务端;
  32. CSATTCPServer::GetInstance()->LoadSocketLib();
  33. CSATTCPServer::GetInstance()->Start(GLOBAL::g_stSATConfig.dwTCPSvrPort);
  34. // 启动设备扫描器;
  35. CSATDevices::GetInstance()->StartWork();
  36. }
  37. // 工作结束回调函数;
  38. void CALLBACK WorkEndof()
  39. {
  40. GLOBAL::WriteTextLog(GLOBAL::SAT_NOR, "SATService goto end");
  41. CSATTCPServer::GetInstance()->Stop();
  42. CSATTCPServer::GetInstance()->UnloadSocketLib();
  43. CSATDevices::GetInstance()->EndofWork();
  44. CSATExecutor::GetInstance()->EndofWork();
  45. GLOBAL::WriteTextLog(GLOBAL::SAT_NOR, "SATService work end!");
  46. }
  47. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  48. {
  49. int nRetCode = 0;
  50. // 直接获得前景窗口的句柄;
  51. HWND hwnd = GetForegroundWindow();
  52. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  53. HMODULE hModule = ::GetModuleHandle(NULL);
  54. // 初始化 MFC 并在失败时显示错误
  55. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  56. {
  57. // TODO: 更改错误代码以符合您的需要
  58. _tprintf(_T("错误: MFC 初始化失败\n"));
  59. nRetCode = 1;
  60. }
  61. else
  62. {
  63. GLOBAL::Init();
  64. // TODO: 在此处为应用程序的行为编写代码。
  65. #if (defined _DEBUG && !defined DBG_SERVER) || defined RTEST
  66. WorkStart();
  67. // 指定时间停止;
  68. ULONGLONG ulTickCount = GetTickCount64();
  69. //while (GetTickCount64() - ulTickCount < 1800000 )
  70. while(1)
  71. {
  72. //printf("倒计时:%ld\n",GetTickCount64() - ulTickCount);
  73. Sleep(1000);
  74. }
  75. // 关闭TCP服务;
  76. WorkEndof();
  77. #else
  78. // 进程存在,直接退出;
  79. TCHAR szExeName[MAX_PATH] = {0};
  80. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  81. TCHAR szDir[_MAX_DIR] = { 0 };
  82. TCHAR szFna[_MAX_FNAME] = { 0 };
  83. TCHAR szExt[_MAX_EXT] = { 0 };
  84. _tsplitpath_s(GLOBAL::g_szCurModuleFileName, szDrive, szDir, szFna, szExt);
  85. _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
  86. if ( GLOBAL::FindProcessCount(szExeName) > 1)
  87. {// 进程计数大于1,退出;
  88. return 0;
  89. }
  90. // 设置服务名和描述;
  91. _stprintf_s(WindowsService::g_szSvcName, _T("SAT Service"));
  92. _stprintf_s(WindowsService::g_szlpSvrDescription, _T("SAT自动化服务"));
  93. // 1.先判断服务是否存在;
  94. if ( WindowsService::IsSvcInstalled())
  95. {
  96. // 卸载服务;
  97. if ( argc > 1)
  98. {
  99. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  100. WindowsService::DoDeleteSvc(WindowsService::g_szSvcName);
  101. else
  102. _tprintf_s(_T("未有卸载服务参数行\n"));
  103. return 0;
  104. }
  105. else
  106. {
  107. WindowsService::InitSvcVar();
  108. WindowsService::SetCallBack(WorkStart, WorkEndof);
  109. SERVICE_TABLE_ENTRY st[] =
  110. {
  111. { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
  112. { NULL, NULL }
  113. };
  114. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  115. {
  116. //LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
  117. WindowsService::SvcReportEvent(_T("Register Service Main Function Error!"));
  118. return 0;
  119. }
  120. }
  121. }
  122. // 2.不存在,安装服务;
  123. WindowsService::InitSvcVar();
  124. #if 0 // 若服务不存在,无须执行主程序;
  125. WindowsService::SetCallBack(WorkStart, WorkEndof);
  126. SERVICE_TABLE_ENTRY st[] =
  127. {
  128. { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
  129. { NULL, NULL }
  130. };
  131. #endif
  132. WindowsService::SvcInstall();
  133. #endif
  134. }
  135. return nRetCode;
  136. }