From 4ffd828655f90258ee911a74da8e1efdb778fa46 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Sat, 19 Apr 2025 14:17:22 +0200 Subject: [PATCH] Reorder function definitions; Rename function --- src/tcp.cpp | 112 ++++++++++++++++++++++++---------------------------- src/tcp.hpp | 8 ++-- 2 files changed, 55 insertions(+), 65 deletions(-) diff --git a/src/tcp.cpp b/src/tcp.cpp index 24bce00..6e96c4f 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -30,7 +30,7 @@ std::expected resolve_remote_address(const char* host, return address; } -inline auto get_local_address(uint16_t port) -> sockaddr_in { +inline sockaddr_in get_local_address(uint16_t port) { sockaddr_in address{}; address.sin_addr.s_addr = htonl(INADDR_ANY); address.sin_family = AF_INET; @@ -62,7 +62,7 @@ std::expected NonBlockingServer::start_listening(uint16_t port) { /// Create socket close_server_socket(); - if (auto res = create_socket(); !res) { + if (auto res = create_server_socket(); !res) { return std::unexpected{res.error()}; } @@ -113,14 +113,11 @@ bool NonBlockingServer::next_client_available() { return {}; } + void NonBlockingServer::disconnect() { close_client_socket(); } -// TODO: Do we need this? -// bool NonBlockingServer::socket_closed() const { -// } - [[nodiscard]] std::expected, int> NonBlockingServer::get_client_ip_as_string() { std::array addrStr; @@ -140,6 +137,17 @@ NonBlockingServer::get_client_ip_as_string() { return addrStr; } +bool NonBlockingServer::data_available() { + int ret = poll(&m_clientPfdIn, 1, 0); + if (ret < 0) { + spdlog::error("tcp::NonBlockingServer: poll() failed with errno={}", + errno); + return false; + } + + return (m_clientPfdIn.revents & POLLIN); +} + [[nodiscard]] std::expected NonBlockingServer::send(std::span data) { const int bytesWritten = @@ -167,18 +175,8 @@ NonBlockingServer::recv(std::span buffer) { return {bytesReceived}; } -bool NonBlockingServer::data_available() { - int ret = poll(&m_clientPfdIn, 1, 0); - if (ret < 0) { - spdlog::error("tcp::NonBlockingServer: poll() failed with errno={}", - errno); - return false; - } - - return (m_clientPfdIn.revents & POLLIN); -} - -[[nodiscard]] std::expected NonBlockingServer::create_socket() { +[[nodiscard]] std::expected +NonBlockingServer::create_server_socket() { m_serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (m_serverSocket < 1) { spdlog::error("tcp::NonBlockingServer: socket() failed with errno={}", @@ -288,6 +286,11 @@ NonBlockingClient::connect(const HostString& host, uint16_t port) { return {}; } +void NonBlockingClient::disconnect() { + m_lastKnownConStatus = ENOTCONN; + close_socket(); +} + [[nodiscard]] std::expected NonBlockingClient::get_last_connection_status() { if (!m_startedNewConAttempt) return m_lastKnownConStatus; @@ -319,33 +322,6 @@ NonBlockingClient::get_last_connection_status() { return m_lastKnownConStatus; } -void NonBlockingClient::disconnect() { - m_lastKnownConStatus = ENOTCONN; - close_socket(); -} - -[[nodiscard]] std::expected -NonBlockingClient::send(std::span data) { - // TODO: Do we need this? - auto conRes = get_last_connection_status(); - if (!conRes) return std::unexpected{conRes.error()}; - if (conRes.value() != EISCONN) { - spdlog::error("tcp::NonBlockingClient: Socket not connected"); - return std::unexpected{ENOTCONN}; - } - - const int bytesWritten = - ::send(m_socket, (const char*)data.data(), data.size(), 0); - - if (bytesWritten <= 0) { - spdlog::error("tcp::NonBlockingClient: send() failed with errno={}", - errno); - return std::unexpected{errno}; - } - - return bytesWritten; -} - bool NonBlockingClient::data_available() { auto conRes = get_last_connection_status(); if (!conRes) return false; @@ -361,6 +337,20 @@ bool NonBlockingClient::data_available() { return (m_pfdIn.revents & POLLIN); } +[[nodiscard]] std::expected +NonBlockingClient::send(std::span data) { + const int bytesWritten = + ::send(m_socket, (const char*)data.data(), data.size(), 0); + + if (bytesWritten <= 0) { + spdlog::error("tcp::NonBlockingClient: send() failed with errno={}", + errno); + return std::unexpected{errno}; + } + + return bytesWritten; +} + [[nodiscard]] std::expected NonBlockingClient::recv(std::span buffer) { const int bytesReceived = @@ -374,22 +364,6 @@ NonBlockingClient::recv(std::span buffer) { return {bytesReceived}; } -void NonBlockingClient::close_socket() { - if (shutdown(m_socket, 0) < 0) { - spdlog::debug("tcp::NonBlockingClient: shutdown() failed with errno={}", - errno); - } - - if (close(m_socket) < 0) { - spdlog::debug("tcp::NonBlockingClient: close() failed with errno={}", - errno); - } - - m_socket = -1; - m_pfdOut.fd = -1; - m_pfdIn.fd = -1; -} - [[nodiscard]] std::expected NonBlockingClient::create_socket() { m_socket = socket(AF_INET, SOCK_STREAM, 0); if (m_socket < 1) { @@ -419,6 +393,22 @@ void NonBlockingClient::close_socket() { return {}; } +void NonBlockingClient::close_socket() { + if (shutdown(m_socket, 0) < 0) { + spdlog::debug("tcp::NonBlockingClient: shutdown() failed with errno={}", + errno); + } + + if (close(m_socket) < 0) { + spdlog::debug("tcp::NonBlockingClient: close() failed with errno={}", + errno); + } + + m_socket = -1; + m_pfdOut.fd = -1; + m_pfdIn.fd = -1; +} + [[nodiscard]] std::expected NonBlockingClient::socket_has_new_output_event() { int ret = poll(&m_pfdOut, 1, 0); diff --git a/src/tcp.hpp b/src/tcp.hpp index b2dd51e..4b8811e 100644 --- a/src/tcp.hpp +++ b/src/tcp.hpp @@ -58,7 +58,7 @@ private: int m_clientSocket = -1; struct sockaddr m_clientAddress = {0}; - [[nodiscard]] std::expected create_socket(); + [[nodiscard]] std::expected create_server_socket(); void close_server_socket(); void close_client_socket(); }; @@ -89,8 +89,8 @@ public: bool data_available(); - [[nodiscard]] std::expected recv(std::span buffer); [[nodiscard]] std::expected send(std::span data); + [[nodiscard]] std::expected recv(std::span buffer); private: int m_socket = -1; @@ -100,9 +100,9 @@ private: bool m_startedNewConAttempt = false; int m_lastKnownConStatus = ENOTCONN; - void close_socket(); - [[nodiscard]] std::expected create_socket(); + void close_socket(); + [[nodiscard]] std::expected socket_has_new_output_event(); };