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

Transport layer using Asio Unix domain sockets for JSON-RPC communication with framing. More...

#include <framed_pipe_transport.hpp>

Inheritance diagram for jsonrpc::transport::FramedPipeTransport:
Collaboration diagram for jsonrpc::transport::FramedPipeTransport:

Public Member Functions

 FramedPipeTransport (const std::string &socket_path, bool is_server)
 
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::PipeTransport
 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
 

Additional Inherited Members

- Protected Member Functions inherited from jsonrpc::transport::PipeTransport
auto GetSocket () -> asio::local::stream_protocol::socket &
 
- Static Protected Member Functions inherited from jsonrpc::transport::FramedTransport
static void FrameMessage (std::ostream &output, const std::string &message)
 Constructs a framed message.
 
static auto ReadHeadersFromStream (std::istream &input) -> HeaderMap
 
static auto ReadContentLengthFromStream (std::istream &input) -> int
 
static auto ReadContent (std::istream &input, std::size_t content_length) -> std::string
 Reads content from the input stream based on the content length.
 
static auto ReceiveFramedMessage (std::istream &input) -> std::string
 Receives a framed message.
 
- Static Protected Attributes inherited from jsonrpc::transport::FramedTransport
static constexpr const char * kHeaderDelimiter = "\r\n\r\n"
 The delimiter used to separate headers from the message content.
 

Detailed Description

Transport layer using Asio Unix domain sockets for JSON-RPC communication with framing.

Definition at line 16 of file framed_pipe_transport.hpp.

Constructor & Destructor Documentation

◆ FramedPipeTransport()

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

Definition at line 10 of file framed_pipe_transport.cpp.

12 : PipeTransport(socket_path, is_server) {
13 spdlog::info(
14 "FramedPipeTransport initialized with socket path: {}", socket_path);
15}
PipeTransport(const std::string &socket_path, bool is_server)
Constructs a PipeTransport.

Member Function Documentation

◆ ReceiveMessage()

auto jsonrpc::transport::FramedPipeTransport::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 39 of file framed_pipe_transport.cpp.

39 {
40 asio::streambuf buffer;
41 asio::error_code ec;
42
43 // Read headers until \r\n\r\n delimiter
44 asio::read_until(GetSocket(), buffer, kHeaderDelimiter, ec);
45 if (ec) {
46 throw std::runtime_error("Failed to read message headers: " + ec.message());
47 }
48
49 std::istream header_stream(&buffer);
50
51 // Extract content length from the headers
52 int content_length = ReadContentLengthFromStream(header_stream);
53
54 // Calculate how much more content we need to read
55 std::size_t remaining_content_length = content_length - buffer.size();
56
57 // Read any remaining content directly into the buffer
58 if (remaining_content_length > 0) {
59 asio::read(GetSocket(), buffer.prepare(remaining_content_length), ec);
60 if (ec && ec != asio::error::eof) {
61 throw std::runtime_error(
62 "Failed to read message content: " + ec.message());
63 }
64 buffer.commit(remaining_content_length);
65 }
66
67 // Convert the entire buffer to a string
68 std::string content(
69 asio::buffers_begin(buffer.data()), asio::buffers_end(buffer.data()));
70 return content;
71}
static constexpr const char * kHeaderDelimiter
The delimiter used to separate headers from the message content.
static auto ReadContentLengthFromStream(std::istream &input) -> int
auto GetSocket() -> asio::local::stream_protocol::socket &

◆ SendMessage()

void jsonrpc::transport::FramedPipeTransport::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 17 of file framed_pipe_transport.cpp.

17 {
18 try {
19 asio::streambuf message_buf;
20 std::ostream message_stream(&message_buf);
21 FrameMessage(message_stream, message);
22
23 asio::error_code ec;
24 std::size_t bytes_written =
25 asio::write(GetSocket(), message_buf.data(), ec);
26
27 if (ec) {
28 throw std::runtime_error("Error sending message: " + ec.message());
29 }
30
31 spdlog::info(
32 "FramedPipeTransport sent message with {} bytes", bytes_written);
33 } catch (const std::exception &e) {
34 spdlog::error("FramedPipeTransport failed to send message: {}", e.what());
35 throw;
36 }
37}
static void FrameMessage(std::ostream &output, const std::string &message)
Constructs a framed message.

References jsonrpc::transport::FramedTransport::FrameMessage(), and jsonrpc::transport::PipeTransport::GetSocket().

Here is the call graph for this function:

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