summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-05-21 18:01:42 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-05-21 22:49:45 +0800
commit815c3955dbd9ff7e1a427cf4bc66da38b581b178 (patch)
tree37b7b20175aeadf789e990fac7bd364792dd7e49 /src/sessionManager.cpp
parenta7cd7d8eb37c50554ae7c75f2ba94db74d293a1b (diff)
feat: join and leave message
internal: refactored messaging management, use instnace pointer for verification of session
Diffstat (limited to 'src/sessionManager.cpp')
-rw-r--r--src/sessionManager.cpp21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index b81aeaf..a5dc713 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -1,11 +1,12 @@
#include "sessionManager.hpp"
+#include "IOManager.hpp"
#include "log.hpp"
#include "session.hpp"
#include <algorithm>
-#include <cstring>
#include <ranges>
#include <unistd.h>
#include <utility>
+#include "chatServer.hpp"
void CSessionManager::shutdownSessions() {
if (m_vSessions.empty())
@@ -47,18 +48,23 @@ void CSessionManager::run() {
newSession();
}
-void CSessionManager::broadcast(const std::string &msg) const {
+void CSessionManager::broadcast(const std::string &msg, std::optional<uintptr_t> self) const {
for (const auto &[thread, session] : m_vSessions)
- session->write(msg);
+ if (!self || (self && (uintptr_t)session.get() != *self))
+ session->write(msg);
+
+ g_pIOManager->addCustom({"", msg});
}
-void CSessionManager::broadcastChat(const CChatManager::SMessage &msg) const {
+void CSessionManager::broadcast(const CChatManager::SMessage &msg) const {
for (const auto &[thread, session] : m_vSessions) {
if (!session)
continue;
session->writeChat(msg);
}
+
+ g_pIOManager->addMessage(msg);
}
bool CSessionManager::nameExists(const std::string &name) {
@@ -84,6 +90,11 @@ CSession *CSessionManager::getByIp(const std::string &ip) const {
return it != m_vSessions.end() ? it->second.get() : nullptr;
}
+CSession *CSessionManager::getByPtr(const uintptr_t &p) const {
+ auto it = std::ranges::find_if(m_vSessions, [&p](const auto &s) { return (uintptr_t)s.second.get() == p; });
+ return it != m_vSessions.end() ? it->second.get() : 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>();
}
@@ -97,6 +108,8 @@ void CSessionManager::kick(CSession *session, const bool kill, const std::string
void CSessionManager::kick(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(); });
+ g_pSessionManager->broadcast(NFormatter::fmt(NONEWLINE, "{} has left the chat", session->second->m_name), (uintptr_t)session->second.get());
+
if (it != m_vSessions.end()) {
if (!reason.empty()) {
it->second->setDeaf(false);