coral
A C++ library for distributed co-simulation
zmqx.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_ZMQX_HPP
11 #define CORAL_NET_ZMQX_HPP
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <cstring>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 #include "zmq.hpp"
21 
22 #include "coral/config.h"
23 #include "coral/net.hpp"
24 
25 
26 namespace coral
27 {
28 namespace net
29 {
30 
38 namespace zmqx
39 {
40 
41 
48 zmq::context_t& GlobalContext();
49 
50 
56  zmq::socket_t& socket,
57  const std::string& networkInterface = "*");
58 
59 
64 std::string LastEndpoint(zmq::socket_t& socket);
65 
66 
74 std::uint16_t EndpointPort(const std::string& endpoint);
75 
76 
85 bool WaitForOutgoing(zmq::socket_t& socket, std::chrono::milliseconds timeout);
86 
87 
95 bool WaitForIncoming(zmq::socket_t& socket, std::chrono::milliseconds timeout);
96 
97 
99 enum class SendFlag : int
100 {
102  none = 0,
103 
108  more = 1
109 };
110 CORAL_DEFINE_BITWISE_ENUM_OPERATORS(SendFlag)
111 
112 
113 
121 void Send(
122  zmq::socket_t& socket,
124  SendFlag flags = SendFlag::none);
125 
126 
134 void Receive(
135  zmq::socket_t& socket,
136  std::vector<zmq::message_t>& message);
137 
138 
140 std::string ToString(const zmq::message_t& frame);
141 
142 
144 zmq::message_t ToFrame(const std::string& s);
145 
146 
154 {
155 public:
157  ReqSocket();
158 
159  CORAL_DEFINE_DEFAULT_MOVE(ReqSocket, m_socket)
160 
161  ~ReqSocket() CORAL_NOEXCEPT;
162 
169  void Connect(const coral::net::Endpoint& server);
170 
178  void Bind(const coral::net::Endpoint& localEndpoint);
179 
185  void Close();
186 
193 
200 
207  zmq::socket_t& Socket();
208  const zmq::socket_t& Socket() const;
209 
210 private:
212 };
213 
214 
225 {
226 public:
228  RepSocket();
229 
230  CORAL_DEFINE_DEFAULT_MOVE(RepSocket,
231  m_socket, m_boundEndpoint, m_clientEnvelope)
232 
233  ~RepSocket() CORAL_NOEXCEPT;
234 
242  void Bind(const coral::net::Endpoint& localEndpoint);
243 
250  void Connect(const coral::net::Endpoint& clientEndpoint);
251 
257  void Close();
258 
263  const coral::net::Endpoint& BoundEndpoint() const;
264 
276 
285 
296  void Ignore();
297 
304  zmq::socket_t& Socket();
305  const zmq::socket_t& Socket() const;
306 
307 private:
309  coral::net::Endpoint m_boundEndpoint;
310  std::vector<zmq::message_t> m_clientEnvelope;
311 };
312 
313 
322 bool Receive(
323  RepSocket& socket,
325  std::chrono::milliseconds timeout);
326 
327 
328 }}} // namespace
329 #endif // header guard
std::string LastEndpoint(zmq::socket_t &socket)
Returns the value of the ZMQ_LAST_ENDPOINT socket property.
void Send(zmq::socket_t &socket, std::vector< zmq::message_t > &message, SendFlag flags=SendFlag::none)
Sends a message.
The frames being sent are part of a multiframe message, and more frames are coming.
void Receive(zmq::socket_t &socket, std::vector< zmq::message_t > &message)
Receives a message.
bool WaitForIncoming(zmq::socket_t &socket, std::chrono::milliseconds timeout)
Waits up to timeout milliseconds for incoming messages on socket.
Main module header for coral::net.
std::string ToString(const zmq::message_t &frame)
Returns the content of a message frame as a std::string.
std::uint16_t EndpointPort(const std::string &endpoint)
Given a string on the form "tcp://addr:port", returns the port number.
zmq::message_t ToFrame(const std::string &s)
Returns a message frame whose contents are equal to s.
SendFlag
Flags for the Send() function.
Definition: zmqx.hpp:99
STL class.
A client socket for communication with a single server node.
Definition: zmqx.hpp:153
A protocol/transport independent endpoint address specification.
Definition: net.hpp:34
Definition: variable_io.hpp:28
zmq::context_t & GlobalContext()
Returns a reference to a global ZMQ context.
std::uint16_t BindToEphemeralPort(zmq::socket_t &socket, const std::string &networkInterface="*")
Binds socket to an ephemeral TCP port on the given network interface and returns the port number...
bool WaitForOutgoing(zmq::socket_t &socket, std::chrono::milliseconds timeout)
Waits up to timeout milliseconds to see if a message may be enqueued on socket.
A server socket for communication with one or more client nodes in a request-reply pattern...
Definition: zmqx.hpp:224