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 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/session.cpp') 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); } -- cgit v1.2.3