-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
94 lines (73 loc) · 2.38 KB
/
main.cpp
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
#pragma once
#include "main.h"
#include "CDatabase.h"
#include "Service.h"
#include "Utils.h"
#include "CServer.h"
#include "CConfig.h"
using std::exception;
WCHAR ConectionString[512];
HWND hWnd;
static int running_from_service = 0;
int wmain()
{
static CConfig cfg;
if( ! running_from_service )
{
cfg.Load();
LOG_IF(FATAL, CConfig::Status::ERROR == cfg.getStatus()) <<"An error was, while reading settings or start/stop script does not exist." ;
running_from_service = 1;
if( service_register((LPWSTR)cfg.keyBindings.serviceName.c_str()) )
{
VLOG(1) << "DEBUG: We've been called as a service. Register service and exit this thread.";
/* We've been called as a service. Register service
* and exit this thread. main() would be called from
* service.c next time.
*
* Note that if service_register() succeedes it does
* not return until the service is stopped.
* That is why we should set running_from_service
* before calling service_register and unset it
* afterwards.
*/
return 0;
}
LOG(INFO) <<"Started as console application.";
running_from_service = 0;
}
try
{
boost::asio::io_context io_context;
ZeroMemory(ConectionString, sizeof(ConectionString));
wmemcpy_s(ConectionString,
sizeof(ConectionString), cfg.keyBindings.connectionString.c_str(),
cfg.keyBindings.connectionString.size());
hWnd = GetDesktopWindow(); // need for connection to ODBC driver
// try connect to db, to see if everything is ok with connection string
// and db is well configured
// try to connect to ODBC driver
auto db = new ODBCDatabase::CDatabase();
// if connected, ok, if not - exit with exeption
if( db->ConnectedOk() )
{
LOG(INFO) << "Connection to db was success";
delete db;
} else {
LOG(FATAL) << "Can't connect to db. Check connection string in configuration file";
}
if(cfg.keyBindings.ipAdress.empty())
CServer Server(io_context, cfg.keyBindings.port, cfg.keyBindings.threads);
else
CServer Server(io_context, cfg.keyBindings.ipAdress, cfg.keyBindings.port, cfg.keyBindings.threads);
} catch(exception& e) {
LOG(FATAL) << "Server has been crashed: " << e.what() << std::endl;
}
return 0;
}
void SafeExit()
{
LOG(INFO) <<"Server stopped safely.";
//We just exit from program. All connections wrapped in shared_ptr, so they will be closed soon
//We don't need to watch them
google::ShutdownGoogleLogging();
}