src/cppzmq/README.md

CI Coverage Status License

Introduction & Design Goals

cppzmq is a C++ binding for libzmq. It has the following design goals: - cppzmq maps the libzmq C API to C++ concepts. In particular: - it is type-safe (the libzmq C API exposes various class-like concepts as void*) - it provides exception-based error handling (the libzmq C API provides errno-based error handling) - it provides RAII-style classes that automate resource management (the libzmq C API requires the user to take care to free resources explicitly) - cppzmq is a light-weight, header-only binding. You only need to include the header file zmq.hpp (and maybe zmq_addon.hpp) to use it. - zmq.hpp is meant to contain direct mappings of the abstractions provided by the libzmq C API, while zmq_addon.hpp provides additional higher-level abstractions.

There are other C++ bindings for ZeroMQ with different design goals. In particular, none of the following bindings are header-only: - zmqpp is a high-level binding to libzmq. - czmqpp is a binding based on the high-level czmq API. - fbzmq is a binding that integrates with Apache Thrift and provides higher-level abstractions in addition. It requires C++14.

Supported platforms

Examples

These examples require at least C++11.

#include <zmq.hpp>

int main()
{
    zmq::context_t ctx;
    zmq::socket_t sock(ctx, zmq::socket_type::push);
    sock.bind("inproc://test");
    sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
}

This a more complex example where we send and receive multi-part messages over TCP with a wildcard port.

#include <iostream>
#include <zmq_addon.hpp>

int main()
{
    zmq::context_t ctx;
    zmq::socket_t sock1(ctx, zmq::socket_type::push);
    zmq::socket_t sock2(ctx, zmq::socket_type::pull);
    sock1.bind("tcp://127.0.0.1:*");
    const std::string last_endpoint =
        sock1.get(zmq::sockopt::last_endpoint);
    std::cout << "Connecting to "
              << last_endpoint << std::endl;
    sock2.connect(last_endpoint);

    std::array<zmq::const_buffer, 2> send_msgs = {
        zmq::str_buffer("foo"),
        zmq::str_buffer("bar!")
    };
    if (!zmq::send_multipart(sock1, send_msgs))
        return 1;

    std::vector<zmq::message_t> recv_msgs;
    const auto ret = zmq::recv_multipart(
        sock2, std::back_inserter(recv_msgs));
    if (!ret)
        return 1;
    std::cout << "Got " << *ret
              << " messages" << std::endl;
    return 0;
}

See the examples directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable.

API Overview

For an extensive overview of the zmq.hpp API in use, see this Tour of CPPZMQ by @brettviren.

Bindings for libzmq in zmq.hpp:

Types: class zmq::context_t enum zmq::ctxopt class zmq::socket_t class zmq::socket_ref enum zmq::socket_type enum zmq::sockopt enum zmq::send_flags enum zmq::recv_flags class zmq::message_t class zmq::const_buffer class zmq::mutable_buffer struct zmq::recv_buffer_size alias zmq::send_result_t alias zmq::recv_result_t alias zmq::recv_buffer_result_t class zmq::error_t class zmq::monitor_t struct zmq_event_t, alias zmq::free_fn, alias zmq::pollitem_t, alias zmq::fd_t class zmq::poller_t DRAFT enum zmq::event_flags DRAFT enum zmq::poller_event DRAFT

Functions: zmq::version zmq::poll zmq::proxy zmq::proxy_steerable zmq::buffer zmq::str_buffer

Extra high-level types and functions zmq_addon.hpp:

Types: class zmq::multipart_t class zmq::active_poller_t DRAFT

Functions: zmq::recv_multipart zmq::send_multipart zmq::send_multipart_n zmq::encode * zmq::decode

Compatibility Guidelines

The users of cppzmq are expected to follow the guidelines below to ensure not to break when upgrading cppzmq to newer versions (non-exhaustive list):

The following macros may be used by consumers of cppzmq: CPPZMQ_VERSION, CPPZMQ_VERSION_MAJOR, CPPZMQ_VERSION_MINOR, CPPZMQ_VERSION_PATCH.

Contribution policy

The contribution policy is at: http://rfc.zeromq.org/spec:22

Build instructions

Build steps:

  1. Build libzmq via cmake. This does an out of source build and installs the build files
  2. download and unzip the lib, cd to directory
  3. mkdir build
  4. cd build
  5. cmake ..
  6. sudo make -j4 install

  7. Build cppzmq via cmake. This does an out of source build and installs the build files

  8. download and unzip the lib, cd to directory
  9. mkdir build
  10. cd build
  11. cmake ..
  12. sudo make -j4 install

  13. Build cppzmq via vcpkg. This does an out of source build and installs the build files

  14. git clone https://github.com/Microsoft/vcpkg.git
  15. cd vcpkg
  16. ./bootstrap-vcpkg.sh # bootstrap-vcpkg.bat for Powershell
  17. ./vcpkg integrate install
  18. ./vcpkg install cppzmq

Using this:

A cmake find package scripts is provided for you to easily include this library. Add these lines in your CMakeLists.txt to include the headers and library files of cpp zmq (which will also include libzmq for you).

#find cppzmq wrapper, installed by make of cppzmq
find_package(cppzmq)
target_link_libraries(*Your Project Name* cppzmq)


Try the clustermq package in your browser

Any scripts or data that you put into this service are public.

clustermq documentation built on Nov. 21, 2023, 5:06 p.m.