From e702fc9cdbe9808a8cc7a60bf3c414f874d81d02 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Tue, 11 Mar 2025 19:09:47 +0800 Subject: session: get/use IP and port on connect --- src/session.cpp | 14 ++++++++++---- src/session.hpp | 21 +++++++++++++++++---- src/sessionManager.cpp | 7 +++---- src/sessionManager.hpp | 2 +- 4 files changed, 31 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/session.cpp b/src/session.cpp index b3994ee..64dbda1 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -1,16 +1,23 @@ #include "session.hpp" #include "chatManager.hpp" #include "log.hpp" +#include "server.hpp" #include "sessionManager.hpp" #include #include #include +#include + +CSession::CSession() : m_addrLen(sizeof(m_addr)) { + m_sockfd = Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket()->get(), reinterpret_cast(&m_addr), &m_addrLen)}; // if this is in the init list, it will run before m_addrLen, so it won't work :/ -CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd) - : m_sockfd(std::move(sockfd)) { if (!m_sockfd.isValid()) throw std::runtime_error("session: Failed to create socket"); + inet_ntop(AF_INET, &m_addr.sin_addr, m_ip, INET_ADDRSTRLEN); + m_port = ntohs(m_addr.sin_port); + log(TRACE, "session({}): connected on port {}", m_ip, m_port); + log(TRACE, "session: initialized"); } @@ -82,8 +89,7 @@ CSession::SRecvData CSession::read(const std::string &msg) { void CSession::run() { onConnect(); registerSession() ? recvLoop() : - // TODO: get ip and print the ip also - log(LOG, "User exited without even registering"); + log(LOG, "Client {} exited without even registering", m_ip); g_pSessionManager->removeSession(self); } diff --git a/src/session.hpp b/src/session.hpp index 9133176..235870c 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -3,12 +3,13 @@ #include "FileDescriptor.hpp" #include "format.hpp" #include +#include #include #include class CSession { public: - CSession(Hyprutils::OS::CFileDescriptor m_sockfd); + CSession(); ~CSession(); struct SRecvData { @@ -18,30 +19,42 @@ class CSession { }; std::string getName(); - bool isValid(); + void setSelf(std::pair> *self); + SRecvData read(); SRecvData read(const std::string &msg); bool write(const std::string &msg, eFormatType type); template bool write(eFormatType type, std::format_string fmt, Args &&...args); + void run(); - void setSelf(std::pair> *self); + + bool isValid(); + private: - std::pair> *self; enum eEventType : std::uint8_t { READ, WRITE }; + + std::pair> *self; Hyprutils::OS::CFileDescriptor m_sockfd; + sockaddr_in m_addr; + socklen_t m_addrLen = sizeof(m_addr); std::string m_name; + char m_ip[INET_ADDRSTRLEN]; + int m_port; + void recvLoop(); bool registerSession(); + void onConnect(); void onDisconnect(); void onErrno(eEventType); void onRecv(const SRecvData &data); void onSend(const std::string &msg); void onShutdown(); + friend class CSessionManager; }; diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index 7b40bc2..d77c0ae 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -1,6 +1,5 @@ #include "sessionManager.hpp" #include "log.hpp" -#include "server.hpp" #include "session.hpp" #include #include @@ -26,8 +25,8 @@ CSessionManager::~CSessionManager() { log(SYS, "SessionManager: bye"); } -std::pair> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { - std::shared_ptr session = std::make_shared(std::move(sockfd)); +std::pair> *CSessionManager::newSession() { + std::shared_ptr session = std::make_shared(); const auto instance = &m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session)); instance->second->setSelf(instance); return instance; @@ -35,7 +34,7 @@ std::pair> *CSessionManager::newSession( void CSessionManager::run() { while (true) - newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket()->get(), nullptr, nullptr) /* awaits new client/session */}); + newSession(); } void CSessionManager::broadcast(const std::string &msg, eFormatType type) const { diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp index be81dd9..4be5ce8 100644 --- a/src/sessionManager.hpp +++ b/src/sessionManager.hpp @@ -11,7 +11,7 @@ class CSessionManager { CSessionManager(); ~CSessionManager(); - std::pair> *newSession(Hyprutils::OS::CFileDescriptor sockfd); + std::pair> *newSession(); void run(); void broadcast(const std::string &msg, eFormatType type = NONE) const; -- cgit v1.2.3