From bbf039a596a4717d76706ba6550977fc56d2958c Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Fri, 18 Apr 2025 22:00:35 +0200 Subject: [PATCH] Replace custom error type with int --- src/tcp.cpp | 64 ++++++++++++++++++----------------------------------- src/tcp.hpp | 26 +++++----------------- 2 files changed, 27 insertions(+), 63 deletions(-) diff --git a/src/tcp.cpp b/src/tcp.cpp index dc76768..b846dff 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -1,7 +1,6 @@ #include "tcp.hpp" #include -#include #include #include #include @@ -19,13 +18,13 @@ namespace { -std::expected get_server_address(const char* host, - uint16_t port) { +std::expected get_server_address(const char* host, + uint16_t port) { hostent* server = gethostbyname(host); if (server == nullptr) { spdlog::error("tcp: get_host_by_name() failed with h_errno={}", h_errno); - return std::unexpected{tcp::Error::FailedToResolveHost}; + return std::unexpected{h_errno}; } sockaddr_in address{}; @@ -44,7 +43,7 @@ std::expected get_server_address(const char* host, namespace tcp { -[[nodiscard]] std::expected +[[nodiscard]] std::expected NonBlockingClient::connect(HostString host, uint16_t port) { /// Resolve host @@ -65,17 +64,9 @@ NonBlockingClient::connect(HostString host, uint16_t port) { sizeof(serverAddress)); if (connectRes < 0) { - if (errno == EALREADY) { - spdlog::error( - "tcp::NonBlockingClient: connect() failed with errno={}", - errno); - return std::unexpected{Error::OtherOperationInProgress}; - } else if (errno != EINPROGRESS) { - spdlog::error( - "tcp::NonBlockingClient: connect() failed with errno={}", - errno); - return std::unexpected{Error::FailedToStartConnecting}; - } + spdlog::error("tcp::NonBlockingClient: connect() failed with errno={}", + errno); + return std::unexpected{errno}; } m_startedNewConAttemt = true; @@ -83,7 +74,7 @@ NonBlockingClient::connect(HostString host, uint16_t port) { return {}; } -[[nodiscard]] std::expected +[[nodiscard]] std::expected NonBlockingClient::get_last_connection_status() { if (!m_startedNewConAttemt) return m_lastKnownConStatus; @@ -102,7 +93,7 @@ NonBlockingClient::get_last_connection_status() { if (getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &err, &len) < 0) { spdlog::error( "tcp::NonBlockingClient: getsockopt() failed with errno={}", errno); - return std::unexpected{Error::FailedToReadSocketError}; + return std::unexpected{errno}; } if (err == 0) { @@ -134,13 +125,14 @@ void NonBlockingClient::disconnect() { close_socket(); } -[[nodiscard]] std::expected +[[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 != ConnectionStatus::Successful) { spdlog::error("tcp::NonBlockingClient: Socket not connected"); - return std::unexpected{Error::SocketNotConnected}; + return std::unexpected{ENOTCONN}; } const int bytesWritten = @@ -149,15 +141,7 @@ NonBlockingClient::send(std::span data) { if (bytesWritten <= 0) { spdlog::error("tcp::NonBlockingClient: send() failed with errno={}", errno); - - switch (errno) { - case EWOULDBLOCK: - return std::unexpected{Error::BufferFull}; - case EALREADY: - return std::unexpected{Error::OtherOperationInProgress}; - default: - return std::unexpected{Error::SendFailed}; - } + return std::unexpected{errno}; } return bytesWritten; @@ -179,7 +163,7 @@ bool NonBlockingClient::data_available() { return (m_pfdIn.revents & POLLIN); } -[[nodiscard]] std::expected +[[nodiscard]] std::expected NonBlockingClient::recv(std::span buffer) { const int bytesReceived = ::recv(m_socket, (char*)buffer.data(), buffer.size(), 0); @@ -187,13 +171,7 @@ NonBlockingClient::recv(std::span buffer) { if (bytesReceived < 0) { spdlog::error("tcp::NonBlockingClient: recv() failed with errno={}", errno); - - switch (errno) { - case EWOULDBLOCK: - return std::unexpected{Error::NoDataAvailable}; - default: - return std::unexpected{Error::RecvFailed}; - } + return std::unexpected{errno}; } return {bytesReceived}; } @@ -214,12 +192,12 @@ void NonBlockingClient::close_socket() { m_pfdIn.fd = -1; } -[[nodiscard]] std::expected NonBlockingClient::create_socket() { +[[nodiscard]] std::expected NonBlockingClient::create_socket() { m_socket = socket(AF_INET, SOCK_STREAM, 0); if (m_socket < 1) { spdlog::error("tcp::NonBlockingClient: socket() failed with errno={}", errno); - return std::unexpected{Error::FailedToCreateSocket}; + return std::unexpected{errno}; } m_pfdOut.fd = m_socket; @@ -230,26 +208,26 @@ void NonBlockingClient::close_socket() { spdlog::error("tcp::NonBlockingClient: fcntl() failed with errno={}", errno); close_socket(); - return std::unexpected{Error::FailedToCreateSocket}; + return std::unexpected{errno}; } if (fcntl(m_socket, F_SETFL, (currentFlags) | O_NONBLOCK) == -1) { spdlog::error("tcp::NonBlockingClient: fcntl() failed with errno={}", errno); close_socket(); - return std::unexpected{Error::FailedToCreateSocket}; + return std::unexpected{errno}; } return {}; } -[[nodiscard]] std::expected +[[nodiscard]] std::expected NonBlockingClient::new_error_codes_available() { int ret = poll(&m_pfdOut, 1, 0); if (ret < 0) { spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}", errno); - return std::unexpected{Error::FailedToReadSocketError}; + return std::unexpected{errno}; } return (ret == 0); diff --git a/src/tcp.hpp b/src/tcp.hpp index 4d22e5b..ec31000 100644 --- a/src/tcp.hpp +++ b/src/tcp.hpp @@ -24,20 +24,6 @@ namespace tcp { -enum class Error { - FailedToCreateSocket, - FailedToStartConnecting, - FailedToReadSocketError, - FailedToResolveHost, - OtherOperationInProgress, - SocketNotConnected, - BufferFull, - NoDataAvailable, - SendFailed, - RecvFailed, -}; - - enum class ConnectionStatus { AttemptingConnection, Successful, @@ -63,19 +49,19 @@ using HostString = std::array; class NonBlockingClient { public: // clang-format off - [[nodiscard]] std::expected + [[nodiscard]] std::expected connect(HostString host, uint16_t port); // clang-format off void disconnect(); - [[nodiscard]] std::expected + [[nodiscard]] std::expected get_last_connection_status(); bool data_available(); - [[nodiscard]] std::expected recv(std::span buffer); - [[nodiscard]] std::expected send(std::span data); + [[nodiscard]] std::expected recv(std::span buffer); + [[nodiscard]] std::expected send(std::span data); private: int m_socket = -1; @@ -87,8 +73,8 @@ private: void close_socket(); - [[nodiscard]] std::expected create_socket(); - [[nodiscard]] std::expected new_error_codes_available(); + [[nodiscard]] std::expected create_socket(); + [[nodiscard]] std::expected new_error_codes_available(); }; } // namespace tcp