Skip to content

Commit

Permalink
introduced posix utils
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 20, 2024
1 parent 99188fe commit 240f7f4
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 32 deletions.
47 changes: 36 additions & 11 deletions src/common/ipc/unix_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

#include "unix_socket_client.hpp"

#include "platform/posix_utils.hpp"

#include <cetl/cetl.hpp>

#include <array>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <string>
Expand All @@ -33,16 +36,23 @@ UnixSocketClient::~UnixSocketClient()
{
if (client_fd_ != -1)
{
::close(client_fd_);
platform::posixSyscallError([this] {
//
return ::close(client_fd_);
});
}
}

bool UnixSocketClient::connect_to_server()
{
client_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (client_fd_ == -1)
CETL_DEBUG_ASSERT(client_fd_ == -1, "");

if (const auto err = platform::posixSyscallError([this] {
//
return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0);
}))
{
std::cerr << "Failed to create socket: " << ::strerror(errno) << "\n";
std::cerr << "Failed to create socket: " << ::strerror(err) << "\n";
return false;
}

Expand All @@ -51,10 +61,13 @@ bool UnixSocketClient::connect_to_server()
// 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)
if (const auto err = platform::posixSyscallError([this, &addr] {
//
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return ::connect(client_fd_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr));
}))
{
std::cerr << "Failed to connect to server: " << ::strerror(errno) << "\n";
std::cerr << "Failed to connect to server: " << ::strerror(err) << "\n";
return false;
}

Expand All @@ -63,14 +76,26 @@ bool UnixSocketClient::connect_to_server()

void UnixSocketClient::send_message(const std::string& message) const
{
if (::write(client_fd_, message.c_str(), message.size()) == -1)
if (const auto err = platform::posixSyscallError([this, &message] {
//
return ::write(client_fd_, message.c_str(), message.size());
}))
{
std::cerr << "Failed to send message: " << ::strerror(errno) << "\n";
std::cerr << "Failed to write: " << ::strerror(err) << "\n";
return;
}

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);
ssize_t bytes_read = 0;
if (const auto err = platform::posixSyscallError([this, &bytes_read, &buffer] {
//
return bytes_read = ::read(client_fd_, buffer.data(), buffer.size() - 1);
}))
{
std::cerr << "Failed to read: " << ::strerror(err) << "\n";
return;
}
if (bytes_read > 0)
{
buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
Expand Down
91 changes: 70 additions & 21 deletions src/common/ipc/unix_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#include "unix_socket_server.hpp"

#include "platform/posix_executor_extension.hpp"
#include "platform/posix_utils.hpp"

#include <cetl/cetl.hpp>
#include <cetl/rtti.hpp>
#include <libcyphal/executor.hpp>

#include <array>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -46,36 +46,56 @@ UnixSocketServer::~UnixSocketServer()
{
if (server_fd_ != -1)
{
::close(server_fd_);
::unlink(socket_path_.c_str());
platform::posixSyscallError([this] {
//
return ::close(server_fd_);
});
platform::posixSyscallError([this] {
//
return ::unlink(socket_path_.c_str());
});
}
}

bool UnixSocketServer::start()
{
server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (server_fd_ == -1)
CETL_DEBUG_ASSERT(server_fd_ == -1, "");

if (const auto err = platform::posixSyscallError([this] {
//
return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0);
}))
{
std::cerr << "Failed to create socket: " << ::strerror(errno) << "\n";
std::cerr << "Failed to create socket: " << ::strerror(err) << "\n";
return false;
}

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());

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
if (::bind(server_fd_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) == -1)
platform::posixSyscallError([this] {
//
return ::unlink(socket_path_.c_str());
});

if (const auto err = platform::posixSyscallError([this, &addr] {
//
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return ::bind(server_fd_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr));
}))
{
std::cerr << "Failed to bind socket: " << ::strerror(errno) << "\n";
std::cerr << "Failed to bind socket: " << ::strerror(err) << "\n";
return false;
}

if (::listen(server_fd_, MaxConnections) == -1)
if (const auto err = platform::posixSyscallError([this] {
//
return ::listen(server_fd_, MaxConnections);
}))
{
std::cerr << "Failed to listen on socket: " << ::strerror(errno) << "\n";
std::cerr << "Failed to listen on socket: " << ::strerror(err) << "\n";
return false;
}

Expand All @@ -84,27 +104,56 @@ bool UnixSocketServer::start()

void UnixSocketServer::accept() const
{
const int client_fd = ::accept(server_fd_, nullptr, nullptr);
if (client_fd == -1)
CETL_DEBUG_ASSERT(server_fd_ != -1, "");

int client_fd = -1;
if (const auto err = platform::posixSyscallError([this, &client_fd] {
//
return client_fd = ::accept(server_fd_, nullptr, nullptr);
}))
{
std::cerr << "Failed to accept connection: " << ::strerror(errno) << "\n";
std::cerr << "Failed to accept connection: " << ::strerror(err) << "\n";
return;
}

handle_client(client_fd);
::close(client_fd);

platform::posixSyscallError([this, client_fd] {
//
return ::close(client_fd);
});
}

void UnixSocketServer::handle_client(const int client_fd)
{
CETL_DEBUG_ASSERT(client_fd != -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)
ssize_t bytes_read = 0;
if (const auto err = platform::posixSyscallError([client_fd, &bytes_read, &buffer] {
//
return bytes_read = ::read(client_fd, buffer.data(), buffer.size() - 1);
}))
{
std::cerr << "Failed to read: " << ::strerror(err) << "\n";
return;
}
if (bytes_read == 0)
{
return;
}
buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
std::cout << "Received: " << buffer.data() << "\n";

// Echo back
//
if (const auto err = platform::posixSyscallError([client_fd, bytes_read, &buffer] {
//
return ::write(client_fd, buffer.data(), bytes_read);
}))
{
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
std::cerr << "Failed to write: " << ::strerror(err) << "\n";
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/common/platform/posix_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//

#ifndef OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED
#define OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED

#include <cerrno>

namespace ocvsmd
{
namespace common
{
namespace platform
{

template <typename Call>
int posixSyscallError(const Call& call)
{
while (call() < 0)
{
const int error_num = errno;
if (error_num != EINTR)
{
return error_num;
}
}
return 0;
}

} // namespace platform
} // namespace common
} // namespace ocvsmd

#endif // OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED

0 comments on commit 240f7f4

Please sign in to comment.