From 99188fefdd3d950e04f6f2ddc109b5e7ff65266d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 20 Dec 2024 11:44:06 +0200 Subject: [PATCH] fix clang-tidy --- src/common/ipc/unix_socket_client.cpp | 15 ++++++++----- src/common/ipc/unix_socket_client.hpp | 2 +- src/common/ipc/unix_socket_server.cpp | 32 ++++++++++++++++++--------- src/common/ipc/unix_socket_server.hpp | 4 ++-- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index 9eec87d..5538d42 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -5,11 +5,13 @@ #include "unix_socket_client.hpp" +#include #include #include #include #include #include +#include #include #include #include @@ -46,8 +48,10 @@ bool UnixSocketClient::connect_to_server() sockaddr_un addr{}; addr.sun_family = AF_UNIX; + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) ::strncpy(addr.sun_path, socket_path_.c_str(), sizeof(addr.sun_path) - 1); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) if (::connect(client_fd_, reinterpret_cast(&addr), sizeof(addr)) == -1) { std::cerr << "Failed to connect to server: " << ::strerror(errno) << "\n"; @@ -57,19 +61,20 @@ bool UnixSocketClient::connect_to_server() return true; } -void UnixSocketClient::send_message(const std::string& message) +void UnixSocketClient::send_message(const std::string& message) const { if (::write(client_fd_, message.c_str(), message.size()) == -1) { std::cerr << "Failed to send message: " << ::strerror(errno) << "\n"; } - char buffer[256]; - ssize_t bytes_read = ::read(client_fd_, buffer, sizeof(buffer) - 1); + constexpr std::size_t buf_size = 256; + std::array buffer{}; + const ssize_t bytes_read = ::read(client_fd_, buffer.data(), buffer.size() - 1); if (bytes_read > 0) { - buffer[bytes_read] = '\0'; - std::cout << "Received: " << buffer << "\n"; + buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + std::cout << "Received: " << buffer.data() << "\n"; } } diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index 6316fcc..fdc1b8e 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -28,7 +28,7 @@ class UnixSocketClient final ~UnixSocketClient(); bool connect_to_server(); - void send_message(const std::string& message); + void send_message(const std::string& message) const; private: std::string socket_path_; diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index ef7c60f..dd88fe8 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -11,11 +11,13 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -26,6 +28,13 @@ namespace common { namespace ipc { +namespace +{ + +constexpr int MaxConnections = 5; + +} // namespace + UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path) : executor_{executor} , socket_path_{std::move(socket_path)} @@ -53,16 +62,18 @@ bool UnixSocketServer::start() sockaddr_un addr{}; addr.sun_family = AF_UNIX; + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) ::strncpy(addr.sun_path, socket_path_.c_str(), sizeof(addr.sun_path) - 1); ::unlink(socket_path_.c_str()); - if (::bind(server_fd_, reinterpret_cast(&addr), sizeof(addr)) == -1) + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + if (::bind(server_fd_, reinterpret_cast(&addr), sizeof(addr)) == -1) { std::cerr << "Failed to bind socket: " << ::strerror(errno) << "\n"; return false; } - if (::listen(server_fd_, 5) == -1) + if (::listen(server_fd_, MaxConnections) == -1) { std::cerr << "Failed to listen on socket: " << ::strerror(errno) << "\n"; return false; @@ -71,9 +82,9 @@ bool UnixSocketServer::start() return true; } -void UnixSocketServer::accept() +void UnixSocketServer::accept() const { - int client_fd = ::accept(server_fd_, nullptr, nullptr); + const int client_fd = ::accept(server_fd_, nullptr, nullptr); if (client_fd == -1) { std::cerr << "Failed to accept connection: " << ::strerror(errno) << "\n"; @@ -84,15 +95,16 @@ void UnixSocketServer::accept() ::close(client_fd); } -void UnixSocketServer::handle_client(int client_fd) +void UnixSocketServer::handle_client(const int client_fd) { - char buffer[256]; - ssize_t bytes_read = ::read(client_fd, buffer, sizeof(buffer) - 1); + constexpr std::size_t buf_size = 256; + std::array buffer{}; + const ssize_t bytes_read = ::read(client_fd, buffer.data(), buffer.size() - 1); if (bytes_read > 0) { - buffer[bytes_read] = '\0'; - std::cout << "Received: " << buffer << "\n"; - ::write(client_fd, buffer, bytes_read); // Echo back + buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + std::cout << "Received: " << buffer.data() << "\n"; + ::write(client_fd, buffer.data(), bytes_read); // Echo back } } diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/unix_socket_server.hpp index 9fb3edf..a53e787 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/unix_socket_server.hpp @@ -35,10 +35,10 @@ class UnixSocketServer final CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerListenCallback( libcyphal::IExecutor::Callback::Function&& function) const; - void accept(); + void accept() const; private: - void handle_client(int client_fd); + static void handle_client(const int client_fd); libcyphal::IExecutor& executor_; std::string socket_path_;