diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-04 23:39:03 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-04 23:39:03 +0800 |
| commit | b8228c249975306f1d35a43d79a99d4c6dc5f116 (patch) | |
| tree | aea632d177634d234f94efb9eadb661c66e39d28 /src | |
| parent | a93355a8e566aacfc289f6393dec247a17895afb (diff) | |
clang-format & clang-tidy
Diffstat (limited to 'src')
| -rw-r--r-- | src/FileDescriptor.cpp | 73 | ||||
| -rw-r--r-- | src/FileDescriptor.hpp | 4 | ||||
| -rw-r--r-- | src/log.hpp | 13 | ||||
| -rw-r--r-- | src/main.cpp | 8 | ||||
| -rw-r--r-- | src/server.cpp | 13 | ||||
| -rw-r--r-- | src/server.hpp | 10 | ||||
| -rw-r--r-- | src/session.cpp | 121 | ||||
| -rw-r--r-- | src/session.hpp | 38 | ||||
| -rw-r--r-- | src/sessionManager.cpp | 21 | ||||
| -rw-r--r-- | src/sessionManager.hpp | 15 |
10 files changed, 155 insertions, 161 deletions
diff --git a/src/FileDescriptor.cpp b/src/FileDescriptor.cpp index 8592f42..6f17f2a 100644 --- a/src/FileDescriptor.cpp +++ b/src/FileDescriptor.cpp @@ -1,89 +1,88 @@ #include <cstdlib> -#include <hyprutils/os/FileDescriptor.hpp> #include <fcntl.h> +#include <hyprutils/os/FileDescriptor.hpp> #include <sys/poll.h> #include <unistd.h> #include <utility> using namespace Hyprutils::OS; -CFileDescriptor::CFileDescriptor(int const fd) : m_fd(fd) {} +CFileDescriptor::CFileDescriptor(int const fd) + : m_fd(fd) {} -CFileDescriptor::CFileDescriptor(CFileDescriptor&& other) : m_fd(std::exchange(other.m_fd, -1)) {} +CFileDescriptor::CFileDescriptor(CFileDescriptor &&other) noexcept + : m_fd(std::exchange(other.m_fd, -1)) {} -CFileDescriptor& CFileDescriptor::operator=(CFileDescriptor&& other) { - if (this == &other) // Shit will go haywire if there is duplicate ownership - abort(); +CFileDescriptor &CFileDescriptor::operator=(CFileDescriptor &&other) noexcept { + if (this == &other) // Shit will go haywire if there is duplicate ownership + abort(); - reset(); - m_fd = std::exchange(other.m_fd, -1); - return *this; + reset(); + m_fd = std::exchange(other.m_fd, -1); + return *this; } CFileDescriptor::~CFileDescriptor() { - reset(); + reset(); } bool CFileDescriptor::isValid() const { - return m_fd != -1; + return m_fd != -1; } int CFileDescriptor::get() const { - return m_fd; + return m_fd; } int CFileDescriptor::getFlags() const { - return fcntl(m_fd, F_GETFD); + return fcntl(m_fd, F_GETFD); } bool CFileDescriptor::setFlags(int flags) { - if (fcntl(m_fd, F_SETFD, flags) == -1) - return false; - - return true; + return fcntl(m_fd, F_SETFD, flags) != -1; } int CFileDescriptor::take() { - return std::exchange(m_fd, -1); + return std::exchange(m_fd, -1); } void CFileDescriptor::reset() { - if (m_fd != -1) { - close(m_fd); - m_fd = -1; - } + if (m_fd != -1) { + close(m_fd); + m_fd = -1; + } } CFileDescriptor CFileDescriptor::duplicate(int flags) const { - if (m_fd == -1) - return {}; + if (m_fd == -1) + return {}; - return CFileDescriptor{fcntl(m_fd, flags, 0)}; + return CFileDescriptor{fcntl(m_fd, flags, 0)}; } bool CFileDescriptor::isClosed() const { - return isClosed(m_fd); + return isClosed(m_fd); } bool CFileDescriptor::isReadable() const { - return isReadable(m_fd); + return isReadable(m_fd); } bool CFileDescriptor::isClosed(int fd) { - pollfd pfd = { - .fd = fd, - .events = POLLIN, - .revents = 0, - }; + pollfd pfd = { + .fd = fd, + .events = POLLIN, + .revents = 0, + }; - if (poll(&pfd, 1, 0) < 0) - return true; + if (poll(&pfd, 1, 0) < 0) + return true; - return pfd.revents & (POLLHUP | POLLERR); + return pfd.revents & (POLLHUP | POLLERR); } bool CFileDescriptor::isReadable(int fd) { - pollfd pfd = {.fd = fd, .events = POLLIN, .revents = 0}; + pollfd pfd = {.fd = fd, .events = POLLIN, .revents = 0}; - return poll(&pfd, 1, 0) > 0 && (pfd.revents & POLLIN); + return poll(&pfd, 1, 0) > 0 && (pfd.revents & POLLIN); } diff --git a/src/FileDescriptor.hpp b/src/FileDescriptor.hpp index 59fb7e9..3ba31c2 100644 --- a/src/FileDescriptor.hpp +++ b/src/FileDescriptor.hpp @@ -7,8 +7,8 @@ class CFileDescriptor { public: CFileDescriptor() = default; explicit CFileDescriptor(int const fd); - CFileDescriptor(CFileDescriptor &&); - CFileDescriptor &operator=(CFileDescriptor &&); + CFileDescriptor(CFileDescriptor &&) noexcept; + CFileDescriptor &operator=(CFileDescriptor &&) noexcept; ~CFileDescriptor(); CFileDescriptor(const CFileDescriptor &) = delete; diff --git a/src/log.hpp b/src/log.hpp index 5285dc8..1ee401e 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -3,11 +3,10 @@ #include <map> #include <string> typedef enum { - LOG_LEVEL_DEBUG = 0, - LOG_LEVEL_INFO, - LOG_LEVEL_WARN, - LOG_LEVEL_ERROR, - LOG_LEVEL_FATAL, - LOG_LEVEL_MAX + LOG_LEVEL_DEBUG = 0, + LOG_LEVEL_INFO, + LOG_LEVEL_WARN, + LOG_LEVEL_ERROR, + LOG_LEVEL_FATAL, + LOG_LEVEL_MAX } eLogLevel; - diff --git a/src/main.cpp b/src/main.cpp index 1cf50b5..e7d9780 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,9 @@ -#include "sessionManager.hpp" #include "server.hpp" +#include "sessionManager.hpp" int main() { - g_pServer = std::make_unique<CServer>(8080); - g_pSessionManager = std::make_unique<CSessionManager>(); - g_pSessionManager->enterLoop(); + g_pServer = std::make_unique<CServer>(8080); + g_pSessionManager = std::make_unique<CSessionManager>(); + g_pSessionManager->enterLoop(); return 0; } diff --git a/src/server.cpp b/src/server.cpp index f85d1fc..f840fbe 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -13,9 +13,9 @@ CServer::CServer(uint16_t port) m_addr.sin_family = AF_INET; m_addr.sin_port = htons(port); m_addr.sin_addr.s_addr = INADDR_ANY; - m_dBind = bind(m_sockfd.get(), reinterpret_cast<sockaddr *>(&m_addr), sizeof(m_addr)); - m_dListen = listen(m_sockfd.get(), 5); - m_dBind < 0 || m_dListen < 0 ? throw std::runtime_error("Failed to bind or listen") : std::println("Server started on port: {}", port); + m_dBind = bind(m_sockfd.get(), reinterpret_cast<sockaddr *>(&m_addr), sizeof(m_addr)); + m_dListen = listen(m_sockfd.get(), 5); + m_dBind < 0 || m_dListen < 0 ? throw std::runtime_error("Failed to bind or listen") : std::println("Server started on port: {}", port); // char buffer[1024] = {0}; // int clientSocket = accept(m_sockfd.get(), nullptr, nullptr); @@ -36,15 +36,14 @@ CServer::~CServer() { m_sockfd.reset(); } - void CServer::log(const eLogLevel loglevel, const std::string &message) const { - std::println("[{}]: {}", "a", message); + std::println("[{}]: {}", "a", message); } bool CServer::isValid() { - return m_sockfd.isValid() && m_dBind >= 0 && m_dListen >= 0; + return m_sockfd.isValid() && m_dBind >= 0 && m_dListen >= 0; } Hyprutils::OS::CFileDescriptor CServer::getSocket() const { - return m_sockfd.duplicate(); + return m_sockfd.duplicate(); } diff --git a/src/server.hpp b/src/server.hpp index e100266..5d6bdcf 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -2,22 +2,22 @@ #include "FileDescriptor.hpp" #include "log.hpp" +#include <memory> #include <netinet/in.h> #include <string> -#include <memory> class CServer { public: CServer(uint16_t port); ~CServer(); - void log(const eLogLevel loglevel, const std::string &message) const; - bool isValid(); - Hyprutils::OS::CFileDescriptor getSocket() const; + void log(const eLogLevel loglevel, const std::string &message) const; + bool isValid(); + Hyprutils::OS::CFileDescriptor getSocket() const; private: Hyprutils::OS::CFileDescriptor m_sockfd; sockaddr_in m_addr; - int m_dBind = -1; + int m_dBind = -1; int m_dListen = -1; }; inline std::unique_ptr<CServer> g_pServer; diff --git a/src/session.cpp b/src/session.cpp index da7ebf6..9f2b53e 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -1,112 +1,111 @@ #include "session.hpp" +#include <cerrno> #include <cmath> +#include <cstring> #include <ostream> -#include <sys/socket.h> #include <print> -#include <cerrno> -#include <cstring> +#include <sys/socket.h> -CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd) : m_sockfd(std::move(sockfd)) { - if (!m_sockfd.isValid()) - throw std::runtime_error("Failed to create socket"); +CSession::CSession(Hyprutils::OS::CFileDescriptor sockfd) + : m_sockfd(std::move(sockfd)) { + if (!m_sockfd.isValid()) + throw std::runtime_error("Failed to create socket"); } CSession::~CSession() { - m_sockfd.reset(); + m_sockfd.reset(); } void CSession::onConnect() const { - std::println("Client connected"); + std::println("Client connected"); } void CSession::onDisconnect() const { - std::println("Client disconnected"); + std::println("Client disconnected"); } void CSession::onErrno() const { - std::println("Error: {}", strerror(errno)); + std::println("Error: {}", strerror(errno)); } void CSession::onRecv(SRecvData &data) const { - std::println("Received: {}", data.data, data.dataSize); + std::println("Received: {}", data.data, data.dataSize); } void CSession::recvManager() { - while (true) { - CSession::SRecvData recvData = read(); - if (!recvData.good) { - break; - } - } + while (true) { + CSession::SRecvData recvData = read(); + if (!recvData.good) + break; + } } CSession::SRecvData CSession::read() { - SRecvData recvData; - ssize_t dataSize = recv(m_sockfd.get(), recvData.data, recvData.dataSize, 0); - if (dataSize < 0) { - recvData.good = false; - onErrno(); - } - if (dataSize == 0) { - recvData.good = false; - onDisconnect(); - } - onRecv(recvData); - return recvData; + SRecvData recvData; + ssize_t dataSize = recv(m_sockfd.get(), recvData.data, recvData.dataSize, 0); + if (dataSize < 0) { + recvData.good = false; + onErrno(); + } + if (dataSize == 0) { + recvData.good = false; + onDisconnect(); + } + onRecv(recvData); + return recvData; } CSession::SRecvData CSession::read(std::string &msg) { - write(msg); - SRecvData recvData; - ssize_t dataSize = recv(m_sockfd.get(), recvData.data, recvData.dataSize, 0); + write(msg); + SRecvData recvData; + ssize_t dataSize = recv(m_sockfd.get(), recvData.data, recvData.dataSize, 0); - if (dataSize > 0) { - recvData.good = false; + if (dataSize > 0) { + recvData.good = false; - if (dataSize < 0) - onErrno(); + if (dataSize < 0) + onErrno(); - if (dataSize == 0) - onDisconnect(); - } - onRecv(recvData); + if (dataSize == 0) + onDisconnect(); + } + onRecv(recvData); - return recvData; + return recvData; } void CSession::run() { - onConnect(); - // std::jthread recvThread(&CSession::recvManager, this); - registerSession(); - recvManager(); + onConnect(); + // std::jthread recvThread(&CSession::recvManager, this); + registerSession(); + recvManager(); } bool CSession::registerSession() { - SRecvData rervData = read((std::string&)"Name: "); - if (!rervData.good) { - return false; - } - m_name = rervData.data; + SRecvData recvData = read((std::string &)"Name: "); + if (!recvData.good) + return false; + m_name = recvData.data; - std::println("Client registered as: {}", m_name); + std::println("Client registered as: {}", m_name); - return true; + return true; } bool CSession::isValid() const { - return m_sockfd.isValid() && !m_name.empty(); + return m_sockfd.isValid() && !m_name.empty(); } bool CSession::write(const std::string &msg) const { - bool bad = false; - ssize_t bytes_sent = send(m_sockfd.get(), msg.c_str(), msg.size(), 0); - if (bytes_sent < 0) { - bad = true; - onErrno(); - } - return bad; + bool bad = false; + ssize_t bytes_sent = send(m_sockfd.get(), msg.c_str(), msg.size(), 0); + if (bytes_sent < 0) { + bad = true; + onErrno(); + } + return bad; } std::string CSession::getName() const { - return m_name; + return m_name; } diff --git a/src/session.hpp b/src/session.hpp index 8e0859c..2ecd74f 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -5,28 +5,28 @@ class CSession { public: - struct SRecvData { - char data[1024] = {0}; - ssize_t dataSize = sizeof(data); - bool good = true; - }; + struct SRecvData { + char data[1024] = {0}; + ssize_t dataSize = sizeof(data); + bool good = true; + }; CSession(Hyprutils::OS::CFileDescriptor m_sockfd); ~CSession(); - std::string getName() const; - bool isValid() const; - SRecvData read(); - SRecvData read(std::string &msg); - bool write(const std::string &msg) const; - void run(); + std::string getName() const; + bool isValid() const; + SRecvData read(); + SRecvData read(std::string &msg); + bool write(const std::string &msg) const; + void run(); private: Hyprutils::OS::CFileDescriptor m_sockfd; - std::string m_name; - void recvManager(); - bool registerSession(); -void onConnect() const; -void onDisconnect() const; -void onErrno() const; -void onRecv(SRecvData &data) const; - friend class CSessionManager; + std::string m_name; + void recvManager(); + bool registerSession(); + void onConnect() const; + void onDisconnect() const; + void onErrno() const; + void onRecv(SRecvData &data) const; + friend class CSessionManager; }; diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index a249f61..d960aeb 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -1,29 +1,26 @@ #include "sessionManager.hpp" -#include "session.hpp" #include "server.hpp" +#include "session.hpp" #include <print> CSessionManager::CSessionManager() = default; CSessionManager::~CSessionManager() { - for (auto &[thread, session] : m_vSessions) { - thread.join(); - } + for (auto &[thread, session] : m_vSessions) + thread.join(); } void CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { - std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd)); - m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session)); + std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd)); + m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session)); } void CSessionManager::enterLoop() { - while (true) { - newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket().take(), nullptr, nullptr) /* awaits new client/session */}); - } + while (true) + newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket().take(), nullptr, nullptr) /* awaits new client/session */}); } void CSessionManager::broadcast(const std::string &msg) const { - for (const auto &[thread, session] : m_vSessions) { - session->write(msg); - } + for (const auto &[thread, session] : m_vSessions) + session->write(msg); } diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp index 0e6eef3..2dd825a 100644 --- a/src/sessionManager.hpp +++ b/src/sessionManager.hpp @@ -6,15 +6,16 @@ #include <vector> class CSessionManager { -public: - CSessionManager(); - ~CSessionManager(); - void enterLoop(); - void newSession(Hyprutils::OS::CFileDescriptor sockfd); + public: + CSessionManager(); + ~CSessionManager(); + void enterLoop(); + void newSession(Hyprutils::OS::CFileDescriptor sockfd); void broadcast(const std::string &message) const; void kick(const CSession *session) const; void addSession(const CSession &session); -private: - std::vector<std::pair<std::jthread, std::shared_ptr<CSession>>> m_vSessions; + + private: + std::vector<std::pair<std::jthread, std::shared_ptr<CSession>>> m_vSessions; }; inline std::unique_ptr<CSessionManager> g_pSessionManager; |
