Electroneum
net::zmq Namespace Reference

Classes

struct  close
 Calls zmq_close More...
 
class  terminate
 Calls zmq_term More...
 

Typedefs

using context = std::unique_ptr< void, terminate >
 Unique ZMQ context handle, calls zmq_term on destruction. More...
 
using socket = std::unique_ptr< void, close >
 Unique ZMQ socket handle, calls zmq_close on destruction. More...
 

Functions

const std::error_category & error_category () noexcept
 
expect< std::string > receive (void *const socket, const int flags)
 
expect< void > send (const epee::span< const std::uint8_t > payload, void *const socket, const int flags) noexcept
 
std::error_code make_error_code (int code) noexcept
 
std::error_code get_error_code () noexcept
 

Typedef Documentation

◆ context

using net::zmq::context = typedef std::unique_ptr<void, terminate>

Unique ZMQ context handle, calls zmq_term on destruction.

Definition at line 98 of file zmq.h.

◆ socket

using net::zmq::socket = typedef std::unique_ptr<void, close>

Unique ZMQ socket handle, calls zmq_close on destruction.

Definition at line 101 of file zmq.h.

Function Documentation

◆ error_category()

const std::error_category & net::zmq::error_category ( )
noexcept
Returns
Category for ZMQ errors.

Definition at line 40 of file zmq.cpp.

41  {
42  struct category final : std::error_category
43  {
44  virtual const char* name() const noexcept override final
45  {
46  return "error::error_category()";
47  }
48 
49  virtual std::string message(int value) const override final
50  {
51  char const* const msg = zmq_strerror(value);
52  if (msg)
53  return msg;
54  return "zmq_strerror failure";
55  }
56 
57  virtual std::error_condition default_error_condition(int value) const noexcept override final
58  {
59  // maps specific errors to generic `std::errc` cases.
60  switch (value)
61  {
62  case EFSM:
63  case ETERM:
64  break;
65  default:
66  /* zmq is using cerrno errors. C++ spec indicates that
67  `std::errc` values must be identical to the cerrno value.
68  So just map every zmq specific error to the generic errc
69  equivalent. zmq extensions must be in the switch or they
70  map to a non-existent errc enum value. */
71  return std::errc(value);
72  }
73  return std::error_condition{value, *this};
74  }
75 
76  };
77  static const category instance{};
78  return instance;
79  }
::std::string string
Definition: gtest-port.h:1097
const std::error_category & error_category() noexcept
Definition: zmq.cpp:40
const char * name
std::string message("Message requiring signing")
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_error_code()

std::error_code net::zmq::get_error_code ( )
inlinenoexcept
Returns
Error from zmq_errno() using net::zmq::error_category().

Definition at line 70 of file zmq.h.

71  {
72  return make_error_code(zmq_errno());
73  }
std::error_code make_error_code(::common_error value) noexcept
Definition: error.h:41
Here is the call graph for this function:

◆ make_error_code()

std::error_code net::zmq::make_error_code ( int  code)
inlinenoexcept
Returns
code (usally from zmq_errno()) usingnet::zmq::error_category()`.

Definition at line 64 of file zmq.h.

65  {
66  return std::error_code{code, error_category()};
67  }
std::error_category const & error_category() noexcept
Definition: error.cpp:92
Here is the call graph for this function:
Here is the caller graph for this function:

◆ receive()

expect< std::string > net::zmq::receive ( void *  socket,
int  flags = 0 
)

Read all parts of the next message on socket. Blocks until the entire next message (all parts) are read, or until zmq_term is called on the zmq_context associated with socket. If the context is terminated, make_error_code(ETERM) is returned.

Note
This will automatically retry on EINTR, so exiting on interrupts requires context termination.
If non-blocking behavior is requested on socket or by flags, then net::zmq::make_error_code(EAGAIN) will be returned if this would block.
Parameters
socketHandle created with zmq_socket.
flagsSee zmq_msg_read for possible flags.
Returns
Message payload read from socket or ZMQ error.

Definition at line 175 of file zmq.cpp.

176  {
177  std::string payload{};
178  ELECTRONEUM_CHECK(retry_op(do_receive{}, payload, socket, flags));
179  return {std::move(payload)};
180  }
::std::string string
Definition: gtest-port.h:1097
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
#define ELECTRONEUM_CHECK(...)
Check expect<void> and return errors in current scope.
Definition: expect.h:47
const T & move(const T &t)
Definition: gtest-port.h:1317
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send()

expect< void > net::zmq::send ( epee::span< const std::uint8_t payload,
void *  socket,
int  flags = 0 
)
noexcept

Sends payload on socket. Blocks until the entire message is queued for sending, or until zmq_term is called on the zmq_context associated with socket. If the context is terminated, make_error_code(ETERM) is returned.

Note
This will automatically retry on EINTR, so exiting on interrupts requires context termination.
If non-blocking behavior is requested on socket or by flags, then net::zmq::make_error_code(EAGAIN) will be returned if this would block.
Parameters
payloadsent as one message on socket.
socketHandle created with zmq_socket.
flagsSee zmq_send for possible flags.
Returns
success() if sent, otherwise ZMQ error.

Definition at line 182 of file zmq.cpp.

183  {
184  return retry_op(zmq_send, socket, payload.data(), payload.size(), flags);
185  }
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
constexpr std::size_t size() const noexcept
Definition: span.h:111
constexpr pointer data() const noexcept
Definition: span.h:110
Here is the caller graph for this function: