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

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

#include <framed_socket_transport.hpp>

Inheritance diagram for jsonrpc::transport::FramedSocketTransport:
Collaboration diagram for jsonrpc::transport::FramedSocketTransport:

Public Member Functions

 FramedSocketTransport (const std::string &host, uint16_t port, 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::SocketTransport
 SocketTransport (const std::string &host, uint16_t port, bool is_server)
 Constructs a SocketTransport.
 
 ~SocketTransport () override
 
 SocketTransport (const SocketTransport &)=delete
 
auto operator= (const SocketTransport &) -> SocketTransport &=delete
 
 SocketTransport (SocketTransport &&)=delete
 
auto operator= (SocketTransport &&) -> SocketTransport &=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::SocketTransport
auto GetSocket () -> asio::ip::tcp::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 sockets for JSON-RPC communication with framing.

Definition at line 17 of file framed_socket_transport.hpp.

Constructor & Destructor Documentation

◆ FramedSocketTransport()

jsonrpc::transport::FramedSocketTransport::FramedSocketTransport ( const std::string & host,
uint16_t port,
bool is_server )

Definition at line 11 of file framed_socket_transport.cpp.

13 : SocketTransport(host, port, is_server), FramedTransport() {
14 spdlog::info(
15 "FramedSocketTransport initialized with host: {} and port: {}", host,
16 port);
17}
SocketTransport(const std::string &host, uint16_t port, bool is_server)
Constructs a SocketTransport.

Member Function Documentation

◆ ReceiveMessage()

auto jsonrpc::transport::FramedSocketTransport::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 41 of file framed_socket_transport.cpp.

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

◆ SendMessage()

void jsonrpc::transport::FramedSocketTransport::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 19 of file framed_socket_transport.cpp.

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

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

Here is the call graph for this function:

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