14#ifndef ZEROMQ_IZEROMQSVC_H
15#define ZEROMQ_IZEROMQSVC_H 1
45template <
int PERIOD = 0>
55 socket->set(zmq::sockopt::linger, PERIOD);
57 }
catch (zmq::error_t &
e) {
58 if (++tries == max_tries ||
e.num() == EINVAL ||
e.num() == ETERM ||
61 std::cerr <<
"ERROR in ZeroMQSvc::socket: " <<
e.what() <<
" (errno: " <<
e.num() <<
")\n";
64 std::cerr <<
"RETRY " << tries <<
"/" << (max_tries - 1)
65 <<
" in ZmqLingeringSocketPtrDeleter: call interrupted (errno: " <<
e.num() <<
")\n";
73template <
int PERIOD = 0>
78template <
typename... args_t>
85 return socket.send(args...);
86 }
catch (zmq::error_t &
e) {
87 if (++tries == max_tries ||
e.num() != EINTR
91 std::cerr <<
"RETRY " << tries <<
"/" << (max_tries - 1) <<
" in ZeroMQSvc::send (retry_send) on pid "
92 << getpid() <<
": " <<
e.what() <<
")\n";
97template <
typename... args_t>
104 return socket.recv(args...);
105 }
catch (zmq::error_t &
e) {
106 if (++tries == max_tries ||
e.num() != EINTR
110 std::cerr <<
"RETRY " << tries <<
"/" << (max_tries - 1) <<
" in ZeroMQSvc::recv (retry_recv) on pid "
111 << getpid() <<
": " <<
e.what() <<
")\n";
126 zmq::context_t &
context()
const;
127 zmq::socket_t
socket(zmq::socket_type
type)
const;
134 T
decode(
const zmq::message_t &msg)
const
137 memcpy(&
object, msg.data(), msg.size());
142 template <class T, typename std::enable_if<std::is_same<T, std::string>::value, T>
::type * =
nullptr>
143 std::string
decode(
const zmq::message_t &msg)
const
145 std::string
r(msg.size() + 1,
char{});
146 r.assign(
static_cast<const char *
>(msg.data()), msg.size());
152 template <
class T,
typename std::enable_if<!(std::is_same<zmq::message_t, T>::value), T>::type * =
nullptr>
153 T
receive(zmq::socket_t &
socket, zmq::recv_flags flags = zmq::recv_flags::none,
bool *more =
nullptr)
const
165 return decode<T>(msg);
169 template <class T, typename std::enable_if<std::is_same<zmq::message_t, T>::value, T>
::type * =
nullptr>
170 T
receive(zmq::socket_t &
socket, zmq::recv_flags flags = zmq::recv_flags::none,
bool *more =
nullptr)
const
186 zmq::message_t
encode(
const T &item, std::function<
size_t(
const T &t)> sizeFun = ZMQ::defaultSizeOf<T>)
const
188 size_t s = sizeFun(item);
189 zmq::message_t msg{s};
190 memcpy((
void *)msg.data(), &item, s);
194 zmq::message_t
encode(
const char *item)
const;
195 zmq::message_t
encode(
const std::string &item)
const;
198 template <class T, typename std::enable_if<!std::is_same<T, zmq::message_t>::value, T>
::type * =
nullptr>
199 zmq::send_result_t
send(zmq::socket_t &
socket,
const T &item, zmq::send_flags flags = zmq::send_flags::none)
const
205 send(zmq::socket_t &
socket,
const char *item, zmq::send_flags flags = zmq::send_flags::none)
const;
207 send(zmq::socket_t &
socket, zmq::message_t &msg, zmq::send_flags flags = zmq::send_flags::none)
const;
209 send(zmq::socket_t &
socket, zmq::message_t &&msg, zmq::send_flags flags = zmq::send_flags::none)
const;
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
std::unique_ptr< zmq::socket_t, ZmqLingeringSocketPtrDeleter< PERIOD > > ZmqLingeringSocketPtr
auto retry_send(zmq::socket_t &socket, int max_tries, args_t... args) -> decltype(socket.send(args...))
auto retry_recv(zmq::socket_t &socket, int max_tries, args_t... args) -> decltype(socket.recv(args...))
ZeroMQSvc & zmqSvc()
Get singleton object of this class.
Wrapper class for basic ZeroMQ context and socket management.
zmq::context_t * m_context
zmq::message_t encode(const T &item, std::function< size_t(const T &t)> sizeFun=ZMQ::defaultSizeOf< T >) const
encode message to ZMQ
zmq::send_result_t send(zmq::socket_t &socket, const T &item, zmq::send_flags flags=zmq::send_flags::none) const
Send message with ZMQ.
zmq::socket_t * socket_ptr(zmq::socket_type type) const
Create and return a new socket by pointer.
Encoding encoding() const
T decode(const zmq::message_t &msg) const
decode message with ZMQ, POD version
zmq::context_t & context() const
Get context.
T receive(zmq::socket_t &socket, zmq::recv_flags flags=zmq::recv_flags::none, bool *more=nullptr) const
receive message with ZMQ, general version
void close_context() const
void setEncoding(const Encoding &e)
Set encoding mode.
std::string decode(const zmq::message_t &msg) const
decode ZMQ message, string version
TimeOutException()=default
void operator()(zmq::socket_t *socket)