diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/session.cpp | 14 | ||||
| -rw-r--r-- | src/session.hpp | 21 | ||||
| -rw-r--r-- | src/sessionManager.cpp | 7 | ||||
| -rw-r--r-- | src/sessionManager.hpp | 2 |
4 files changed, 31 insertions, 13 deletions
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 <cerrno> #include <cstring> #include <sys/socket.h> +#include <arpa/inet.h> + +CSession::CSession() : m_addrLen(sizeof(m_addr)) { + m_sockfd = Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket()->get(), reinterpret_cast<sockaddr *>(&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 <memory> +#include <netinet/in.h> #include <string> #include <thread> 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<std::jthread, std::shared_ptr<CSession>> *self); + SRecvData read(); SRecvData read(const std::string &msg); bool write(const std::string &msg, eFormatType type); template <typename... Args> bool write(eFormatType type, std::format_string<Args...> fmt, Args &&...args); + void run(); - void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self); + + bool isValid(); + private: - std::pair<std::jthread, std::shared_ptr<CSession>> *self; enum eEventType : std::uint8_t { READ, WRITE }; + + std::pair<std::jthread, std::shared_ptr<CSession>> *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 <algorithm> #include <unistd.h> @@ -26,8 +25,8 @@ CSessionManager::~CSessionManager() { log(SYS, "SessionManager: bye"); } -std::pair<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { - std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd)); +std::pair<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession() { + std::shared_ptr session = std::make_shared<CSession>(); 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<std::jthread, std::shared_ptr<CSession>> *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<std::jthread, std::shared_ptr<CSession>> *newSession(Hyprutils::OS::CFileDescriptor sockfd); + std::pair<std::jthread, std::shared_ptr<CSession>> *newSession(); void run(); void broadcast(const std::string &msg, eFormatType type = NONE) const; |
