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

Transport implementation using TCP/IP sockets. More...

#include <socket_transport.hpp>

Inheritance diagram for jsonrpc::transport::SocketTransport:
Collaboration diagram for jsonrpc::transport::SocketTransport:

Public Member Functions

 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
 

Protected Member Functions

auto GetSocket () -> asio::ip::tcp::socket &
 

Detailed Description

Transport implementation using TCP/IP sockets.

This class provides transport functionality over TCP/IP sockets, supporting both client and server modes for communication over a network.

Definition at line 16 of file socket_transport.hpp.

Constructor & Destructor Documentation

◆ SocketTransport() [1/3]

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

Constructs a SocketTransport.

Parameters
hostThe host address (IP or domain name).
portThe port number.
isServerTrue if the transport acts as a server; false if it acts as a client.

Definition at line 9 of file socket_transport.cpp.

11 : socket_(io_context_), host_(host), port_(port), is_server_(is_server) {
12 spdlog::info(
13 "Initializing SocketTransport with host: {} and port: {}", host, port);
14
15 if (is_server_) {
16 BindAndListen();
17 } else {
18 Connect();
19 }
20}

◆ ~SocketTransport()

jsonrpc::transport::SocketTransport::~SocketTransport ( )
override

Definition at line 26 of file socket_transport.cpp.

26 {
27 spdlog::info("Closing socket and shutting down SocketTransport.");
28 std::error_code ec;
29 socket_.close(ec);
30 if (ec) {
31 spdlog::warn("Socket close error: {}", ec.message());
32 }
33 io_context_.stop();
34}

◆ SocketTransport() [2/3]

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

◆ SocketTransport() [3/3]

jsonrpc::transport::SocketTransport::SocketTransport ( SocketTransport && )
delete

Member Function Documentation

◆ GetSocket()

auto jsonrpc::transport::SocketTransport::GetSocket ( ) -> asio::ip::tcp::socket &
protected

Definition at line 22 of file socket_transport.cpp.

22 {
23 return socket_;
24}

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

Here is the caller graph for this function:

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ ReceiveMessage()

auto jsonrpc::transport::SocketTransport::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 96 of file socket_transport.cpp.

96 {
97 try {
98 asio::streambuf buffer;
99 asio::read_until(socket_, buffer, '\n');
100 std::istream is(&buffer);
101 std::string message;
102 std::getline(is, message);
103 spdlog::debug("Received message: {}", message);
104 return message;
105 } catch (const std::exception &e) {
106 spdlog::error("Error receiving message: {}", e.what());
107 socket_.close();
108 return "";
109 }
110}

◆ SendMessage()

void jsonrpc::transport::SocketTransport::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 86 of file socket_transport.cpp.

86 {
87 try {
88 asio::write(socket_, asio::buffer(message + "\n"));
89 spdlog::debug("Sent message: {}", message);
90 } catch (const std::exception &e) {
91 spdlog::error("Error sending message: {}", e.what());
92 throw std::runtime_error("Error sending message");
93 }
94}

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