Code style

This commit is contained in:
Andreas Tsouchlos 2025-04-19 14:12:16 +02:00
parent 9582dbdc88
commit 1c91b7a1d7
2 changed files with 17 additions and 24 deletions

View File

@ -102,8 +102,10 @@ bool NonBlockingServer::next_client_available() {
[[nodiscard]] std::expected<void, int> NonBlockingServer::accept_next_client() { [[nodiscard]] std::expected<void, int> NonBlockingServer::accept_next_client() {
disconnect(); disconnect();
socklen_t clientAddrLen = sizeof(m_clientAddress);
int result = ::accept(m_serverSocket, (struct sockaddr*)&m_clientAddress, int result = ::accept(m_serverSocket, (struct sockaddr*)&m_clientAddress,
&m_clientAddrLen); &clientAddrLen);
if (result < 0) return std::unexpected{errno}; if (result < 0) return std::unexpected{errno};
m_clientSocket = result; m_clientSocket = result;
@ -256,7 +258,7 @@ NonBlockingClient::~NonBlockingClient() {
} }
[[nodiscard]] std::expected<void, int> [[nodiscard]] std::expected<void, int>
NonBlockingClient::connect(HostString host, uint16_t port) { NonBlockingClient::connect(const HostString& host, uint16_t port) {
/// Resolve host /// Resolve host
auto addrRes = resolve_remote_address(host.data(), port); auto addrRes = resolve_remote_address(host.data(), port);
@ -292,7 +294,7 @@ NonBlockingClient::get_last_connection_status() {
/// Check if connect operation has been completed /// Check if connect operation has been completed
auto pendingOpRes = new_error_codes_available(); auto pendingOpRes = socket_has_new_output_event();
if (!pendingOpRes) return std::unexpected{pendingOpRes.error()}; if (!pendingOpRes) return std::unexpected{pendingOpRes.error()};
if (!pendingOpRes.value()) return EINPROGRESS; if (!pendingOpRes.value()) return EINPROGRESS;
@ -418,7 +420,7 @@ void NonBlockingClient::close_socket() {
} }
[[nodiscard]] std::expected<bool, int> [[nodiscard]] std::expected<bool, int>
NonBlockingClient::new_error_codes_available() { NonBlockingClient::socket_has_new_output_event() {
int ret = poll(&m_pfdOut, 1, 0); int ret = poll(&m_pfdOut, 1, 0);
if (ret < 0) { if (ret < 0) {
spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}", spdlog::error("tcp::NonBlockingClient: poll() failed with errno={}",

View File

@ -41,29 +41,22 @@ public:
[[nodiscard]] std::expected<void, int> accept_next_client(); [[nodiscard]] std::expected<void, int> accept_next_client();
void disconnect(); void disconnect();
[[nodiscard]] std::expected<int, int>
send(std::span<const std::byte> data);
[[nodiscard]] std::expected<int, int>
recv(std::span<std::byte> buffer);
bool data_available();
[[nodiscard]] std::expected<std::array<char, 16>, int> [[nodiscard]] std::expected<std::array<char, 16>, int>
get_client_ip_as_string(); get_client_ip_as_string();
bool data_available();
[[nodiscard]] std::expected<int, int> send(std::span<const std::byte> data);
[[nodiscard]] std::expected<int, int> recv(std::span<std::byte> buffer);
private: private:
ServerSettings m_settings;
const struct sockaddr_in m_serverAddress;
int m_serverSocket = -1; int m_serverSocket = -1;
pollfd m_serverPfdIn = {.fd = -1, .events = POLLIN}; pollfd m_serverPfdIn = {.fd = -1, .events = POLLIN};
pollfd m_clientPfdIn = {.fd = -1, .events = POLLIN}; pollfd m_clientPfdIn = {.fd = -1, .events = POLLIN};
int m_clientSocket = -1; int m_clientSocket = -1;
struct sockaddr m_clientAddress; struct sockaddr m_clientAddress = {0};
socklen_t m_clientAddrLen = sizeof(m_clientAddress);
[[nodiscard]] std::expected<void, int> create_socket(); [[nodiscard]] std::expected<void, int> create_socket();
void close_server_socket(); void close_server_socket();
@ -87,10 +80,8 @@ public:
NonBlockingClient& operator=(NonBlockingClient&&) = delete; NonBlockingClient& operator=(NonBlockingClient&&) = delete;
~NonBlockingClient(); ~NonBlockingClient();
// clang-format off [[nodiscard]] std::expected<void, int> connect(const HostString& host,
[[nodiscard]] std::expected<void, int> uint16_t port);
connect(HostString host, uint16_t port);
// clang-format on
void disconnect(); void disconnect();
@ -112,7 +103,7 @@ private:
void close_socket(); void close_socket();
[[nodiscard]] std::expected<void, int> create_socket(); [[nodiscard]] std::expected<void, int> create_socket();
[[nodiscard]] std::expected<bool, int> new_error_codes_available(); [[nodiscard]] std::expected<bool, int> socket_has_new_output_event();
}; };