blob: 56caf4b1876341a6794a40aa52c84367f97fc62b (
plain)
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
|
#include "chatServer.hpp"
#include "chatManager.hpp"
#include "commandHandler.hpp"
#include "commands.hpp"
#include "inputManager.hpp"
#include "log.hpp"
#include "server.hpp"
#include "sessionManager.hpp"
CChatServer::CChatServer(uint16_t port)
: m_port(port) {
if (std::atexit([]() { g_pChatServer->cleanup(); }))
log(ERR, "Failed to register CChatServer atexit handler, dtors will not be called");
log(LOG, "ChatServer: initialized");
};
CChatServer::~CChatServer() {
cleanup();
log(SYS, "ChatServer: bye");
}
void CChatServer::cleanup() {
g_pChatManager.reset();
// if (m_sessionManagerThread.joinable())
// m_sessionManagerThread.join();
g_pSessionManager.reset();
g_pServer.reset();
g_pCommandHandler.reset();
g_pInputManager.reset();
}
void CChatServer::initManagers() {
log(LOG, "ChatServer: initializing managers");
log(LOG, "Creating Server");
g_pServer = std::make_unique<CServer>(m_port);
log(LOG, "Creating SessionManager");
g_pSessionManager = std::make_shared<CSessionManager>();
log(LOG, "Creating ChatManager");
g_pChatManager = std::make_unique<CChatManager>();
log(LOG, "Creating InputManager");
g_pInputManager = std::make_unique<CInputManager>();
}
void CChatServer::start() {
initManagers();
log(LOG, "Creating CommandHandler");
g_pCommandHandler = std::make_unique<CCommandHandler>();
registerCommands();
// will await while waiting for accept() to return a new client
m_sessionManagerThread = std::jthread(&CSessionManager::run, g_pSessionManager.get());
m_sessionManagerThread.detach();
g_pInputManager->run();
}
|