JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::transport::PipeTransport Class Reference

Transport implementation using Unix domain sockets. More...

#include <pipe_transport.hpp>

Inheritance diagram for jsonrpc::transport::PipeTransport:
Collaboration diagram for jsonrpc::transport::PipeTransport:

Public Member Functions

 PipeTransport (const std::string &socket_path, bool is_server)
 Constructs a PipeTransport.
 
 ~PipeTransport () override
 
 PipeTransport (const PipeTransport &)=delete
 
auto operator= (const PipeTransport &) -> PipeTransport &=delete
 
 PipeTransport (PipeTransport &&)=delete
 
auto operator= (PipeTransport &&) -> PipeTransport &=delete
 
void SendMessage (const std::string &message) override
 Sends a message in string to the transport layer.
 
auto ReceiveMessage () -> std::string override
 Receives a message from the transport layer.
 
- Public Member Functions inherited from jsonrpc::transport::Transport
 Transport ()=default
 
virtual ~Transport ()=default
 
 Transport (const Transport &)=default
 
auto operator= (const Transport &) -> Transport &=default
 
 Transport (Transport &&)=delete
 
auto operator= (Transport &&) -> Transport &=delete
 

Protected Member Functions

auto GetSocket () -> asio::local::stream_protocol::socket &
 

Detailed Description

Transport implementation using Unix domain sockets.

This class provides transport functionality over Unix domain sockets, supporting both client and server modes for inter-process communication on the same machine.

Definition at line 17 of file pipe_transport.hpp.

Constructor & Destructor Documentation

◆ PipeTransport() [1/3]

jsonrpc::transport::PipeTransport::PipeTransport ( const std::string & socket_path,
bool is_server )

Constructs a PipeTransport.

Parameters
socketPathPath to the Unix domain socket.
isServerTrue if the transport acts as a server; false if it acts as a client.

Definition at line 10 of file pipe_transport.cpp.

11 : socket_(io_context_), socket_path_(socket_path), is_server_(is_server) {
12 spdlog::info(
13 "Initializing PipeTransport with socket path: {}. IsServer: {}",
14 socket_path, is_server);
15
16 if (is_server_) {
17 RemoveExistingSocketFile();
18 BindAndListen();
19 } else {
20 Connect();
21 }
22}

◆ ~PipeTransport()

jsonrpc::transport::PipeTransport::~PipeTransport ( )
override

Definition at line 28 of file pipe_transport.cpp.

28 {
29 spdlog::info("Closing socket and shutting down PipeTransport.");
30 socket_.close();
31 io_context_.stop();
32}

◆ PipeTransport() [2/3]

jsonrpc::transport::PipeTransport::PipeTransport ( const PipeTransport & )
delete

◆ PipeTransport() [3/3]

jsonrpc::transport::PipeTransport::PipeTransport ( PipeTransport && )
delete

Member Function Documentation

◆ GetSocket()

auto jsonrpc::transport::PipeTransport::GetSocket ( ) -> asio::local::stream_protocol::socket &
protected

Definition at line 24 of file pipe_transport.cpp.

24 {
25 return socket_;
26}

Referenced by jsonrpc::transport::FramedPipeTransport::SendMessage().

Here is the caller graph for this function:

◆ operator=() [1/2]

auto jsonrpc::transport::PipeTransport::operator= ( const PipeTransport & ) -> PipeTransport &=delete
delete

◆ operator=() [2/2]

auto jsonrpc::transport::PipeTransport::operator= ( PipeTransport && ) -> PipeTransport &=delete
delete

◆ ReceiveMessage()

auto jsonrpc::transport::PipeTransport::ReceiveMessage ( ) -> std::string
overridevirtual

Receives a message from the transport layer.

Returns
The JSON-RPC response as a string.

Implements jsonrpc::transport::Transport.

Definition at line 81 of file pipe_transport.cpp.

81 {
82 try {
83 asio::streambuf buffer;
84 asio::read_until(socket_, buffer, '\n');
85 std::istream is(&buffer);
86 std::string message;
87 std::getline(is, message);
88 spdlog::debug("Received message: {}", message);
89 return message;
90 } catch (const std::exception &e) {
91 spdlog::error("Error receiving message: {}", e.what());
92 socket_.close();
93 throw std::runtime_error("Error receiving message");
94 }
95}

◆ SendMessage()

void jsonrpc::transport::PipeTransport::SendMessage ( const std::string & message)
overridevirtual

Sends a message in string to the transport layer.

Parameters
requestThe JSON-RPC request as a string.

Implements jsonrpc::transport::Transport.

Definition at line 70 of file pipe_transport.cpp.

70 {
71 try {
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");
78 }
79}

The documentation for this class was generated from the following files: