coral
A C++ library for distributed co-simulation
Classes | Enumerations | Functions
coral::net::zmqx Namespace Reference

Functions and classes that extend or wrap the ZeroMQ API. More...

Classes

class  RepSocket
 A server socket for communication with one or more client nodes in a request-reply pattern. More...
 
class  ReqSocket
 A client socket for communication with a single server node. More...
 

Enumerations

Functions

zmq::context_t & GlobalContext ()
 Returns a reference to a global ZMQ context. More...
 
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.
 
std::string LastEndpoint (zmq::socket_t &socket)
 Returns the value of the ZMQ_LAST_ENDPOINT socket property. More...
 
std::uint16_t EndpointPort (const std::string &endpoint)
 Given a string on the form "tcp://addr:port", returns the port number. More...
 
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. More...
 
bool WaitForIncoming (zmq::socket_t &socket, std::chrono::milliseconds timeout)
 Waits up to timeout milliseconds for incoming messages on socket. More...
 
void Send (zmq::socket_t &socket, std::vector< zmq::message_t > &message, SendFlag flags=SendFlag::none)
 Sends a message. More...
 
void Receive (zmq::socket_t &socket, std::vector< zmq::message_t > &message)
 Receives a message. More...
 
std::string ToString (const zmq::message_t &frame)
 Returns the content of a message frame as a std::string.
 
zmq::message_t ToFrame (const std::string &s)
 Returns a message frame whose contents are equal to s.
 
bool Receive (RepSocket &socket, std::vector< zmq::message_t > &message, std::chrono::milliseconds timeout)
 Receives a message, given that one arrives before the timeout is reached. More...
 

Detailed Description

Functions and classes that extend or wrap the ZeroMQ API.

The namespace name zmqx stands for "ZeroMQ eXtensions", and was mainly chosen because zmq caused all kinds of annoying name conflicts with the standard ZeroMQ C++ API.

Enumeration Type Documentation

enum coral::net::zmqx::SendFlag : int
strong

Flags for the Send() function.

Enumerator
none 

No flags are set.

more 

The frames being sent are part of a multiframe message, and more frames are coming.

Function Documentation

std::uint16_t coral::net::zmqx::EndpointPort ( const std::string endpoint)

Given a string on the form "tcp://addr:port", returns the port number.

Exceptions
std::invalid_argumentif endpoint does not have the structure described above.
std::bad_castif the port number is not in a valid number format.
zmq::context_t& coral::net::zmqx::GlobalContext ( )

Returns a reference to a global ZMQ context.

This function is thread safe as long as it is not used in a static initialisation setting.

std::string coral::net::zmqx::LastEndpoint ( zmq::socket_t &  socket)

Returns the value of the ZMQ_LAST_ENDPOINT socket property.

Exceptions
zmq::error_tif ZMQ reports an error.
void coral::net::zmqx::Receive ( zmq::socket_t &  socket,
std::vector< zmq::message_t > &  message 
)

Receives a message.

Existing message content will be overwritten.

Exceptions
zmq::error_ton failure to receive a message frame.
bool coral::net::zmqx::Receive ( RepSocket socket,
std::vector< zmq::message_t > &  message,
std::chrono::milliseconds  timeout 
)

Receives a message, given that one arrives before the timeout is reached.

Existing message content will be overwritten.

Returns
true if a message was received, or false if the function timed out.
Exceptions
zmq::error_ton failure to receive a message frame.
void coral::net::zmqx::Send ( zmq::socket_t &  socket,
std::vector< zmq::message_t > &  message,
SendFlag  flags = SendFlag::none 
)

Sends a message.

The message content will be cleared on return.

Exceptions
std::invalid_argumentif message is empty.
zmq::error_ton failure to send a message frame.
bool coral::net::zmqx::WaitForIncoming ( zmq::socket_t &  socket,
std::chrono::milliseconds  timeout 
)

Waits up to timeout milliseconds for incoming messages on socket.

Returns
whether there are incoming messages on socket.
Exceptions
std::invalid_argumentif timeout is negative.
zmq::error_ton communications error.
bool coral::net::zmqx::WaitForOutgoing ( zmq::socket_t &  socket,
std::chrono::milliseconds  timeout 
)

Waits up to timeout milliseconds to see if a message may be enqueued on socket.

Returns
whether a message may be immediately enqueued on socket.
Exceptions
std::invalid_argumentif timeout is negative.
zmq::error_ton communications error.