Electroneum
cryptonote::rpc::ZmqServer Class Reference

#include <zmq_server.h>

Public Member Functions

 ZmqServer (RpcHandler &h)
 
 ~ZmqServer ()
 
void serve ()
 
bool addIPCSocket (boost::string_ref address, boost::string_ref port)
 
bool addTCPSocket (boost::string_ref address, boost::string_ref port)
 
void run ()
 
void stop ()
 

Static Public Member Functions

static void init_options (boost::program_options::options_description &desc)
 

Detailed Description

Definition at line 45 of file zmq_server.h.

Constructor & Destructor Documentation

◆ ZmqServer()

cryptonote::rpc::ZmqServer::ZmqServer ( RpcHandler h)

Definition at line 48 of file zmq_server.cpp.

48  :
49  handler(h),
50  context(zmq_init(num_zmq_threads))
51 {
52  if (!context)
53  ELECTRONEUM_ZMQ_THROW("Unable to create ZMQ context");
54 }
#define ELECTRONEUM_ZMQ_THROW(msg)
Throw an exception with a custom msg, current ZMQ error code, filename, and line number.
Definition: zmq.h:53

◆ ~ZmqServer()

cryptonote::rpc::ZmqServer::~ZmqServer ( )

Definition at line 56 of file zmq_server.cpp.

57 {
58 }

Member Function Documentation

◆ addIPCSocket()

bool cryptonote::rpc::ZmqServer::addIPCSocket ( boost::string_ref  address,
boost::string_ref  port 
)

Definition at line 97 of file zmq_server.cpp.

98 {
99  MERROR("ZmqServer::addIPCSocket not yet implemented!");
100  return false;
101 }
#define MERROR(x)
Definition: misc_log_ex.h:73

◆ addTCPSocket()

bool cryptonote::rpc::ZmqServer::addTCPSocket ( boost::string_ref  address,
boost::string_ref  port 
)

Definition at line 103 of file zmq_server.cpp.

104 {
105  if (!context)
106  {
107  MERROR("ZMQ RPC Server already shutdown");
108  return false;
109  }
110 
111  rep_socket.reset(zmq_socket(context.get(), ZMQ_REP));
112  if (!rep_socket)
113  {
114  ELECTRONEUM_LOG_ZMQ_ERROR("ZMQ RPC Server socket create failed");
115  return false;
116  }
117 
118  if (zmq_setsockopt(rep_socket.get(), ZMQ_MAXMSGSIZE, std::addressof(max_message_size), sizeof(max_message_size)) != 0)
119  {
120  ELECTRONEUM_LOG_ZMQ_ERROR("Failed to set maximum incoming message size");
121  return false;
122  }
123 
124  static constexpr const int linger_value = std::chrono::milliseconds{linger_timeout}.count();
125  if (zmq_setsockopt(rep_socket.get(), ZMQ_LINGER, std::addressof(linger_value), sizeof(linger_value)) != 0)
126  {
127  ELECTRONEUM_LOG_ZMQ_ERROR("Failed to set linger timeout");
128  return false;
129  }
130 
131  if (address.empty())
132  address = "*";
133  if (port.empty())
134  port = "*";
135 
136  std::string bind_address = "tcp://";
137  bind_address.append(address.data(), address.size());
138  bind_address += ":";
139  bind_address.append(port.data(), port.size());
140 
141  if (zmq_bind(rep_socket.get(), bind_address.c_str()) < 0)
142  {
143  ELECTRONEUM_LOG_ZMQ_ERROR("ZMQ RPC Server bind failed");
144  return false;
145  }
146  return true;
147 }
#define MERROR(x)
Definition: misc_log_ex.h:73
::std::string string
Definition: gtest-port.h:1097
#define ELECTRONEUM_LOG_ZMQ_ERROR(...)
Print a message followed by the current ZMQ error message.
Definition: zmq.h:46
boost::endian::big_uint16_t port
Definition: socks.cpp:60
const char * address
Definition: multisig.cpp:37
Here is the caller graph for this function:

◆ init_options()

static void cryptonote::rpc::ZmqServer::init_options ( boost::program_options::options_description &  desc)
static

◆ run()

void cryptonote::rpc::ZmqServer::run ( )

Definition at line 149 of file zmq_server.cpp.

150 {
151  run_thread = boost::thread(boost::bind(&ZmqServer::serve, this));
152 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serve()

void cryptonote::rpc::ZmqServer::serve ( )

Definition at line 60 of file zmq_server.cpp.

61 {
62  try
63  {
64  // socket must close before `zmq_term` will exit.
65  const net::zmq::socket socket = std::move(rep_socket);
66  if (!socket)
67  {
68  MERROR("ZMQ RPC server reply socket is null");
69  return;
70  }
71 
72  while (1)
73  {
75  MDEBUG("Received RPC request: \"" << message << "\"");
76  const std::string& response = handler.handle(message);
77 
78  ELECTRONEUM_UNWRAP(net::zmq::send(epee::strspan<std::uint8_t>(response), socket.get()));
79  MDEBUG("Sent RPC reply: \"" << response << "\"");
80  }
81  }
82  catch (const std::system_error& e)
83  {
84  if (e.code() != net::zmq::make_error_code(ETERM))
85  MERROR("ZMQ RPC Server Error: " << e.what());
86  }
87  catch (const std::exception& e)
88  {
89  MERROR("ZMQ RPC Server Error: " << e.what());
90  }
91  catch (...)
92  {
93  MERROR("Unknown error in ZMQ RPC server");
94  }
95 }
#define MERROR(x)
Definition: misc_log_ex.h:73
std::error_code make_error_code(int code) noexcept
Definition: zmq.h:64
::std::string string
Definition: gtest-port.h:1097
virtual std::string handle(const std::string &request)=0
expect< std::string > receive(void *const socket, const int flags)
Definition: zmq.cpp:175
epee::misc_utils::struct_init< response_t > response
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
#define MDEBUG(x)
Definition: misc_log_ex.h:76
std::string message("Message requiring signing")
#define ELECTRONEUM_UNWRAP(...)
Definition: expect.h:60
const T & move(const T &t)
Definition: gtest-port.h:1317
expect< void > send(const epee::span< const std::uint8_t > payload, void *const socket, const int flags) noexcept
Definition: zmq.cpp:182
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void cryptonote::rpc::ZmqServer::stop ( )

Definition at line 154 of file zmq_server.cpp.

155 {
156  if (!run_thread.joinable())
157  return;
158 
159  context.reset(); // destroying context terminates all calls
160  run_thread.join();
161 }
Here is the caller graph for this function:

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