Transport implementation using TCP/IP sockets.
More...
#include <socket_transport.hpp>
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.
◆ SocketTransport() [1/3]
jsonrpc::transport::SocketTransport::SocketTransport |
( |
const std::string & | host, |
|
|
uint16_t | port, |
|
|
bool | is_server ) |
Constructs a SocketTransport.
- Parameters
-
host | The host address (IP or domain name). |
port | The port number. |
isServer | True 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 |
◆ GetSocket()
auto jsonrpc::transport::SocketTransport::GetSocket |
( |
| ) |
-> asio::ip::tcp::socket & |
|
protected |
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ 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
-
request | The 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: