summaryrefslogtreecommitdiff
path: root/src/session.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-10 18:36:00 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-20 23:44:58 +0800
commit131dc244e67e3b6e9304e7076c397c761860044c (patch)
tree56f634c1a16c4e5b6eb2224ba876ca97f68e4dee /src/session.cpp
parent4e8644b8ab3b259a91387c93a3295092bd9757a0 (diff)
fix(registerName): no longer allow same name
the previous behaviour was it wouldn't broadcast the message to the same name debug: more logging, maybe we should disable TRACE by default in the future? WIP: partial impl of signal handling(SIGINT(keyboardinterrupt) etc)
Diffstat (limited to 'src/session.cpp')
-rw-r--r--src/session.cpp26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/session.cpp b/src/session.cpp
index b944f5d..f8506c2 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -1,5 +1,4 @@
#include "session.hpp"
-#include "format.hpp"
#include "log.hpp"
#include "sessionManager.hpp"
#include <cerrno>
@@ -7,15 +6,21 @@
#include <sys/socket.h>
#include "chatManager.hpp"
+
CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd)
: m_sockfd(std::move(sockfd)) {
if (!m_sockfd.isValid())
- throw std::runtime_error("Failed to create socket");
+ throw std::runtime_error("session: Failed to create socket");
+
+ log(TRACE, "session: initialized");
}
CSession::~CSession() {
onDisconnect();
m_sockfd.reset();
+
+ self->second.reset();
+ log(TRACE, "session({}): bye", m_name);
}
void CSession::onConnect() {
@@ -49,6 +54,7 @@ void CSession::recvLoop() {
SRecvData recvData = read();
if (!recvData.good)
break;
+
g_pChatManager->newMessage({.msg=recvData.data, .username=m_name});
}
}
@@ -82,21 +88,27 @@ void CSession::run() {
g_pSessionManager->removeSession(self);
}
-bool CSession::registerSession() {
+void CSession::registerSession() {
SRecvData recvData = read("Name: ");
if (!recvData.good)
- return false;
+ return;
if (recvData.data[0] == '\n' || recvData.data[0] == ' ') {
write("Name cannot be empty", eFormatType::ERR);
registerSession();
- }
+ return;
+ }
+
*std::remove(recvData.data, recvData.data+strlen(recvData.data), '\n') = '\0';
+ if (g_pSessionManager->nameExists(recvData.data)) {
+ write("Name already exists", eFormatType::ERR);
+ registerSession();
+ return;
+ }
+
this->m_name = recvData.data;
log(LOG, "Client registered as: {}", m_name);
-
- return true;
}
bool CSession::isValid() {