-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeService.cpp
More file actions
187 lines (153 loc) · 4.86 KB
/
theService.cpp
File metadata and controls
187 lines (153 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* last changed 12.5.26 */
#include "stdafx.h"
#include "theService.h"
#include "helper_funcs.h"
#include "sensitive_data.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CBgHelperSrv, CWinApp)
//{{AFX_MSG_MAP(CBgHelperSrv)
//}}AFX_MSG
// ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
#define SERVICE_NAME _T("bg_helper")
CBgHelperSrv::CBgHelperSrv() {
rLogger::init_logging();
constexpr char sz_unique_named_mutex[] = "bghelpermutex";
const HANDLE h_handle = CreateMutex(nullptr, TRUE, sz_unique_named_mutex);
rLogger::LAST_ERROR = GetLastError();
if (ERROR_ALREADY_EXISTS == rLogger::LAST_ERROR)
{
LOG_SAVE << "The program is already running - exiting.";
return;
}
const std::string app_data = get_env("appdata");
const std::string ini_file = app_data + inf_config_file_path_;
const CA2W inf_config_path(ini_file.c_str());
tcp_port_ = GetPrivateProfileIntW(L"Manufacturer", L"tcp_port", 7777, inf_config_path);
/* Hide console window: */
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
CBgHelperSrv::InitInstance();
/* Explicitly release mutex */
ReleaseMutex(h_handle);
CloseHandle(h_handle); /* close handle before terminating */
}
bool CBgHelperSrv::socket_connect() {
auto wsa_res = WSAStartup(MAKEWORD(2, 0), &wsa_data_);
int i_result;
if(!m_server_socket)
{
m_server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
InetPton(AF_INET, "127.0.0.1", &addr_.sin_addr.s_addr);
addr_.sin_family = AF_INET;
addr_.sin_port = htons(tcp_port_);
}
const bool running = is_process_running(L"ifc_exporter.exe");
if(!running)
{
int wtrue = 1;
setsockopt(m_server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&wtrue, sizeof(int));
shutdown(m_server_socket, 2);
closesocket(m_server_socket);
m_server_socket = 0;
m_connected_to_qml = false;
return false;
}
if(!m_connected_to_qml)
{
i_result = connect(m_server_socket, reinterpret_cast<SOCKADDR*>(&addr_), sizeof(addr_));
if (i_result == SOCKET_ERROR)
{
int wtrue = 1;
setsockopt(m_server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&wtrue, sizeof(int));
shutdown(m_server_socket, 2);
closesocket(m_server_socket);
m_server_socket = 0;
m_connected_to_qml = false;
return false;
}
}
m_connected_to_qml = true;
return true;
}
void CBgHelperSrv::close_socket() const
{
closesocket(m_server_socket);
WSACleanup();
}
/////////////////////////////////////////////////////////////////////////////
// CBgHelperSrv initialization
BOOL CBgHelperSrv::InitInstance() {
// Standard initialization
#ifdef _AFXDLL
// CWinApp::Enable3dControls is no longer needed.You should remove this call
// Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
inf_config_file_path_ = get_config_file_path();
LOG_SAVE << "SocketConnect()...";
socket_connect();
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
LOG_SAVE << "CoInitializeEx HRESULT: " << hr;
/* Бесконечный цикл */
active_object obj([this] { socket_connect(); std::this_thread::sleep_for(200ms); });
/* Запущено как приложение */
LPTSTR argvc = AfxGetApp()->m_lpCmdLine;
if (__argc == 1) {
try
{
std::mutex g_mutex;
wstring name(L"\\\\.\\Pipe\\bghelperpipe");
/* Иногда выдает ошибку 'CreateNamedPipeW error code 5 ("отказано в доступе?") */
WIN32_FIND_DATAW fd;
HANDLE hwnd_pipe = FindFirstFileW(L"\\\\.\\Pipe\\bghelperpipe", &fd);
if(hwnd_pipe)
DisconnectNamedPipe(hwnd_pipe);
LOG_SAVE << "Starting pipeServer... ";
CNamedPipeServer pipe_server(name,
pipe_message_handler,
&g_mutex,
256,
256,
10);
char choice = 's';
do {
std::thread t(ini_timer_check, false, &m_started_status);
/* active_object obj([] { iniTimer_check(); std::this_thread::sleep_for(200ms); }); */
if (choice == 'q') {
LOG_SAVE << "Initiating shutdown";
pipe_server.Shutdown();
LOG_SAVE << "Waiting until everything is shutdown";
pipe_server.WaitUntilFinished(INFINITE);
LOG_SAVE << "Shutdown finished";
}
else if (choice == 's') {
LOG_SAVE << "Starting pipeServer connections";
pipe_server.StartServing();
choice = 'i'; /* infinite */
}
t.join();
} while (choice != 'q'); /* while (SIGABORT != "1"); */
}
catch (exception& ex)
{
LOG_SAVE << ex.what();
}
}
// Close the COM library
CoUninitialize();
close_socket();
LOG_SAVE << "InitInstance: Exit";
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}