summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
blob: 6d9df28730eabfa7ef477d4d582028741816a8eb (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
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
#include "sessionManager.hpp"
#include "log.hpp"
#include "session.hpp"
#include <algorithm>
#include <cstring>
#include <ranges>
#include <unistd.h>
#include <utility>

void CSessionManager::shutdownSessions() {
	if (m_vSessions.empty())
		return;

	for (auto &session : m_vSessions)
		kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&session), false, "Server shutting down");
}

CSessionManager::CSessionManager() {
	if (std::atexit([]() {
			if (g_pSessionManager)
				g_pSessionManager->shutdownSessions();
		}))
		log(ERR, "Failed to register SessionManager atexit handler, will fallback to quick_exit after hang");

	log(LOG, "SessionManager: initialized");
};

CSessionManager::~CSessionManager() {
	for (auto &[thread, session] : m_vSessions) {
		if (thread.joinable())
			thread.detach();
		session.reset();
	}

	log(SYS, "SessionManager: bye");
}

void CSessionManager::newSession() {
	std::shared_ptr session	 = std::make_shared<CSession>();
	auto			instance = std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&m_vSessions.emplace_back(std::jthread(&CSession::run, session.get()), session));
	instance->second->setSelf(std::move(instance));
}

void CSessionManager::run() {
	while (true)
		newSession();
}

void CSessionManager::broadcast(const std::string &msg) const {
	for (const auto &[thread, session] : m_vSessions)
		session->write(msg);
}

void CSessionManager::broadcastChat(const CChatManager::SMessage &msg) const {
	for (const auto &[thread, session] : m_vSessions) {
		if (!session)
			continue;

		session->writeChat(msg);
	}
}

bool CSessionManager::nameExists(const std::string &name) {
	auto it = std::ranges::find_if(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; });

	if (it != m_vSessions.end()) {
		if (!it->second->isValid()) {
			kick(it->second, true, "Connection lost");
			return false;
		}
		return true;
	}
	return false;
}

std::shared_ptr<CSession> CSessionManager::getByName(const std::string &name) const {
	auto it = std::ranges::find_if(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; });
	return it != m_vSessions.end() ? it->second : nullptr;
}

std::shared_ptr<CSession> CSessionManager::getByIp(const std::string &ip) const {
	auto it = std::ranges::find_if(m_vSessions, [&ip](const auto &s) { log(LOG, s.second->m_ip); return s.second->m_ip == ip; });
	return it != m_vSessions.end() ? it->second : nullptr;
}

std::vector<std::shared_ptr<CSession>> CSessionManager::getSessions() const {
	return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to<std::vector>();
}

void CSessionManager::kick(std::shared_ptr<CSession> session, const bool kill, const std::string &reason) {
	for (auto &_session : m_vSessions)
		if (_session.second == session)
			kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&_session), kill, reason);
}

void CSessionManager::kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> session, const bool kill, const std::string &reason) {
	auto it = std::ranges::find_if(m_vSessions, [&session](const auto &s) { return s.second.get() == session->second.get(); });

	if (it != m_vSessions.end()) {
		if (!reason.empty()) {
			it->second->setDeaf(false);
			it->second->write(reason);
		}
		const auto native_handle = it->first.native_handle();
		if (it->first.joinable())
			it->first.detach(); // .detach the thread since it's removing itself
		it->second.reset();
		m_vSessions.erase(it);
		if (kill && native_handle != 0)
			pthread_cancel(native_handle);
	}
}