summaryrefslogtreecommitdiff
path: root/src/session.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-05 23:44:56 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-07 18:03:03 +0800
commitc37bd3e34f5840773949bb55848cda3af6688d20 (patch)
tree1d31ea7c3cf8811ea294da9c094027a9e805dfcb /src/session.cpp
parentaa21e4ea0687fd0c8d333161f5ec31a580512e9e (diff)
refactor: logging system
Diffstat (limited to 'src/session.cpp')
-rw-r--r--src/session.cpp60
1 files changed, 42 insertions, 18 deletions
diff --git a/src/session.cpp b/src/session.cpp
index bfadb5d..d18dbd6 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -1,8 +1,9 @@
#include "session.hpp"
+#include "log.hpp"
#include <cerrno>
#include <cstring>
-#include <print>
#include <sys/socket.h>
+#include <unistd.h>
CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd)
: m_sockfd(std::move(sockfd)) {
@@ -11,29 +12,34 @@ CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd)
}
CSession::~CSession() {
+ onDisconnect();
m_sockfd.reset();
}
void CSession::onConnect() const {
- std::println("Client connected");
+ log(INFO, "Client connected");
}
void CSession::onDisconnect() const {
- std::println("Client disconnected");
+ log(INFO, "Client disconnected");
}
void CSession::onErrno(eEventType eventType) const {
const auto err = strerror(errno);
switch (eventType) {
case READ:
- std::println("Error while reading: {}", err);
+ log(ERROR, "Error while reading: {}", err);
case WRITE:
- std::println("Error while writing: {}", err);
+ log(ERROR, "Error while writing: {}", err);
}
}
-void CSession::onRecv(SRecvData &data) const {
- std::println("Received: {}", data.data, data.dataSize);
+void CSession::onRecv(const SRecvData &data) const {
+ log(INFO, "Received: {}", data.data, data.dataSize);
+}
+
+void CSession::onSend(const std::string &msg) const {
+ log(INFO, "Sent: {}", msg);
}
void CSession::recvManager() {
@@ -55,26 +61,32 @@ CSession::SRecvData CSession::read() {
recvData.good = false;
onDisconnect();
}
+
onRecv(recvData);
return recvData;
}
-CSession::SRecvData CSession::read(std::string &msg) {
- write(msg);
+CSession::SRecvData CSession::read(const std::string &msg) {
+ write(eFormatType::STANDARD, "{}", msg, false);
return read();
}
void CSession::run() {
onConnect();
- // std::jthread recvThread(&CSession::recvManager, this);
registerSession();
recvManager();
}
bool CSession::registerSession() {
- SRecvData recvData = read((std::string &)"Name: ");
+ SRecvData recvData = read("Name: ");
if (!recvData.good)
return false;
+
+ if (recvData.data[0] == '\n' || recvData.data[0] == ' ') {
+ write("Name cannot be empty", eFormatType::ERROR);
+ registerSession();
+ }
+
m_name = recvData.data;
std::println("Client registered as: {}", m_name);
@@ -83,17 +95,29 @@ bool CSession::registerSession() {
}
bool CSession::isValid() const {
- return m_sockfd.isValid() && !m_name.empty();
+ if (!m_sockfd.isValid() || m_name.empty())
+ return false;
+
+ if (send(m_sockfd.get(), "", 0, MSG_NOSIGNAL) < 0)
+ return false;
+
+ return true;
}
-bool CSession::write(const std::string &msg) const {
- bool bad = false;
- ssize_t sentSize = send(m_sockfd.get(), msg.c_str(), msg.size(), 0);
- if (sentSize < 0) {
- bad = true;
+template <typename... Args>
+bool CSession::write(eFormatType type, std::format_string<Args...> fmt, Args &&...args) const {
+ std::string msg = CFormatter::fmt(type, fmt, std::forward<Args>(args)...);
+
+ if (send(m_sockfd.get(), msg.c_str(), msg.size(), 0) < 0) {
onErrno(WRITE);
+ return false;
}
- return bad;
+ onSend(msg);
+ return true;
+}
+
+bool CSession::write(const std::string &msg, eFormatType type) const {
+ return write(type, "{}", msg);
}
std::string CSession::getName() const {