6#include <spdlog/spdlog.h>
11 : socket_(io_context_), socket_path_(socket_path), is_server_(is_server) {
13 "Initializing PipeTransport with socket path: {}. IsServer: {}",
14 socket_path, is_server);
17 RemoveExistingSocketFile();
29 spdlog::info(
"Closing socket and shutting down PipeTransport.");
34void PipeTransport::RemoveExistingSocketFile() {
35 if (unlink(socket_path_.c_str()) == 0) {
36 spdlog::info(
"Removed existing socket file: {}", socket_path_);
37 }
else if (errno != ENOENT) {
39 "Failed to remove existing socket file: {}. Error: {}", socket_path_,
41 throw std::runtime_error(
"Failed to remove existing socket file.");
45void PipeTransport::Connect() {
47 asio::local::stream_protocol::endpoint endpoint(socket_path_);
48 socket_.connect(endpoint);
49 spdlog::info(
"Connected to socket at path: {}", socket_path_);
50 }
catch (
const std::exception &e) {
51 spdlog::error(
"Error connecting to socket: {}", e.what());
52 throw std::runtime_error(
"Error connecting to socket");
56void PipeTransport::BindAndListen() {
58 asio::local::stream_protocol::acceptor acceptor(
59 io_context_, asio::local::stream_protocol::endpoint(socket_path_));
61 spdlog::info(
"Listening on socket path: {}", socket_path_);
62 acceptor.accept(socket_);
63 spdlog::info(
"Accepted connection on socket path: {}", socket_path_);
64 }
catch (
const std::exception &e) {
65 spdlog::error(
"Error binding/listening on socket: {}", e.what());
66 throw std::runtime_error(
"Error binding/listening on socket");
72 std::string full_message = message +
"\n";
73 asio::write(socket_, asio::buffer(full_message));
74 spdlog::debug(
"Sent message: {}", message);
75 }
catch (
const std::exception &e) {
76 spdlog::error(
"Error sending message: {}", e.what());
77 throw std::runtime_error(
"Error sending message");
83 asio::streambuf buffer;
84 asio::read_until(socket_, buffer,
'\n');
85 std::istream is(&buffer);
87 std::getline(is, message);
88 spdlog::debug(
"Received message: {}", message);
90 }
catch (
const std::exception &e) {
91 spdlog::error(
"Error receiving message: {}", e.what());
93 throw std::runtime_error(
"Error receiving message");
void SendMessage(const std::string &message) override
Sends a message in string to the transport layer.
auto GetSocket() -> asio::local::stream_protocol::socket &
PipeTransport(const std::string &socket_path, bool is_server)
Constructs a PipeTransport.
auto ReceiveMessage() -> std::string override
Receives a message from the transport layer.
~PipeTransport() override