summaryrefslogtreecommitdiff
path: root/src/chatServer.cpp
blob: 7b64e795630d46bc0f447d2a8e7c2288e50003f9 (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
58
59
60
61
62
63
64
65
#include "chatServer.hpp"
#include "IOManager.hpp"
#include "chatManager.hpp"
#include "commandHandler.hpp"
#include "commands.hpp"
#include "inputManager.hpp"
#include "log.hpp"
#include "renderManager.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();
	g_pIOManager.reset();
	g_pRenderManager.reset();
}

void CChatServer::initManagers() {
	log(LOG, "ChatServer: initializing managers");
	log(LOG, "Creating RenderManager");
	g_pRenderManager = std::make_unique<CRenderManager>();
	log(LOG, "Creating InputManager");
	g_pInputManager = std::make_unique<CInputManager>();
	log(LOG, "Creating Server");
	g_pServer = std::make_unique<CServer>(m_port);
	log(LOG, "Creating IOManager");
	g_pIOManager = std::make_unique<CIOManager>();
	log(LOG, "Creating SessionManager");
	g_pSessionManager = std::make_shared<CSessionManager>();
	log(LOG, "Creating ChatManager");
	g_pChatManager = std::make_unique<CChatManager>();
}

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->inputLoop();
}