Skip to content

Commit

Permalink
fix clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 20, 2024
1 parent db9e001 commit 99188fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
15 changes: 10 additions & 5 deletions src/common/ipc/unix_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

#include "unix_socket_client.hpp"

#include <array>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <utility>
Expand Down Expand Up @@ -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<sockaddr*>(&addr), sizeof(addr)) == -1)
{
std::cerr << "Failed to connect to server: " << ::strerror(errno) << "\n";
Expand All @@ -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<char, buf_size> 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";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/ipc/unix_socket_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
32 changes: 22 additions & 10 deletions src/common/ipc/unix_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include <cetl/rtti.hpp>
#include <libcyphal/executor.hpp>

#include <array>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <utility>
Expand All @@ -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)}
Expand Down Expand Up @@ -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<sockaddr*>(&addr), sizeof(addr)) == -1)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
if (::bind(server_fd_, reinterpret_cast<const sockaddr*>(&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;
Expand All @@ -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";
Expand All @@ -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<char, buf_size> 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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/ipc/unix_socket_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down

0 comments on commit 99188fe

Please sign in to comment.