|
| 1 | +[](https://github.com/zeromq/cppzmq/actions) |
| 2 | +[](https://coveralls.io/github/zeromq/cppzmq?branch=master) |
| 3 | +[](https://github.com/zeromq/cppzmq/blob/master/LICENSE) |
| 4 | + |
| 5 | +Introduction & Design Goals |
| 6 | +=========================== |
| 7 | + |
| 8 | +cppzmq is a C++ binding for libzmq. It has the following design goals: |
| 9 | + - cppzmq maps the libzmq C API to C++ concepts. In particular: |
| 10 | + - it is type-safe (the libzmq C API exposes various class-like concepts as void*) |
| 11 | + - it provides exception-based error handling (the libzmq C API provides errno-based error handling) |
| 12 | + - it provides RAII-style classes that automate resource management (the libzmq C API requires the user to take care to free resources explicitly) |
| 13 | + - 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. |
| 14 | + - 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. |
| 15 | + |
| 16 | +There are other C++ bindings for ZeroMQ with different design goals. In particular, none of the following bindings are header-only: |
| 17 | + - [zmqpp](https://github.com/zeromq/zmqpp) is a high-level binding to libzmq. |
| 18 | + - [czmqpp](https://github.com/zeromq/czmqpp) is a binding based on the high-level czmq API. |
| 19 | + - [fbzmq](https://github.com/facebook/fbzmq) is a binding that integrates with Apache Thrift and provides higher-level abstractions in addition. It requires C++14. |
| 20 | + |
| 21 | +Supported platforms |
| 22 | +=================== |
| 23 | + |
| 24 | + - Only a subset of the platforms that are supported by libzmq itself are supported. Some features already require a compiler supporting C++11. In the future, probably all features will require C++11. To build and run the tests, CMake and Catch are required. |
| 25 | + - Any libzmq 4.x version is expected to work. DRAFT features may only work for the most recent tested version. Currently explicitly tested libzmq versions are |
| 26 | + - 4.2.0 (without DRAFT API) |
| 27 | + - 4.3.4 (with and without DRAFT API) |
| 28 | + - Platforms with full support (i.e. CI executing build and tests) |
| 29 | + - Ubuntu 18.04 x64 (with gcc 4.8.5, 5.5.0, 7.5.0) |
| 30 | + - Ubuntu 20.04 x64 (with gcc 9.3.0, 10.3.0 and clang 12) |
| 31 | + - Visual Studio 2017 x64 |
| 32 | + - Visual Studio 2019 x64 |
| 33 | + - macOS 10.15 (with clang 12, without DRAFT API) |
| 34 | + - Additional platforms that are known to work: |
| 35 | + - We have no current reports on additional platforms that are known to work yet. Please add your platform here. If CI can be provided for them with a cloud-based CI service working with GitHub, you are invited to add CI, and make it possible to be included in the list above. |
| 36 | + - Additional platforms that probably work: |
| 37 | + - Any platform supported by libzmq that provides a sufficiently recent gcc (4.8.1 or newer) or clang (3.4.1 or newer) |
| 38 | + - Visual Studio 2012+ x86/x64 |
| 39 | + |
| 40 | +Examples |
| 41 | +======== |
| 42 | +These examples require at least C++11. |
| 43 | +```c++ |
| 44 | +#include <zmq.hpp> |
| 45 | + |
| 46 | +int main() |
| 47 | +{ |
| 48 | + zmq::context_t ctx; |
| 49 | + zmq::socket_t sock(ctx, zmq::socket_type::push); |
| 50 | + sock.bind("inproc://test"); |
| 51 | + sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait); |
| 52 | +} |
| 53 | +``` |
| 54 | +This a more complex example where we send and receive multi-part messages over TCP with a wildcard port. |
| 55 | +```c++ |
| 56 | +#include <iostream> |
| 57 | +#include <zmq_addon.hpp> |
| 58 | + |
| 59 | +int main() |
| 60 | +{ |
| 61 | + zmq::context_t ctx; |
| 62 | + zmq::socket_t sock1(ctx, zmq::socket_type::push); |
| 63 | + zmq::socket_t sock2(ctx, zmq::socket_type::pull); |
| 64 | + sock1.bind("tcp://127.0.0.1:*"); |
| 65 | + const std::string last_endpoint = |
| 66 | + sock1.get(zmq::sockopt::last_endpoint); |
| 67 | + std::cout << "Connecting to " |
| 68 | + << last_endpoint << std::endl; |
| 69 | + sock2.connect(last_endpoint); |
| 70 | + |
| 71 | + std::array<zmq::const_buffer, 2> send_msgs = { |
| 72 | + zmq::str_buffer("foo"), |
| 73 | + zmq::str_buffer("bar!") |
| 74 | + }; |
| 75 | + if (!zmq::send_multipart(sock1, send_msgs)) |
| 76 | + return 1; |
| 77 | + |
| 78 | + std::vector<zmq::message_t> recv_msgs; |
| 79 | + const auto ret = zmq::recv_multipart( |
| 80 | + sock2, std::back_inserter(recv_msgs)); |
| 81 | + if (!ret) |
| 82 | + return 1; |
| 83 | + std::cout << "Got " << *ret |
| 84 | + << " messages" << std::endl; |
| 85 | + return 0; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +See the `examples` directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable. |
| 90 | + |
| 91 | + |
| 92 | +API Overview |
| 93 | +============ |
| 94 | + |
| 95 | +For an extensive overview of the `zmq.hpp` API in use, see this [Tour of CPPZMQ by @brettviren](https://brettviren.github.io/cppzmq-tour/index.html). |
| 96 | + |
| 97 | +Bindings for libzmq in `zmq.hpp`: |
| 98 | + |
| 99 | +Types: |
| 100 | +* class `zmq::context_t` |
| 101 | +* enum `zmq::ctxopt` |
| 102 | +* class `zmq::socket_t` |
| 103 | +* class `zmq::socket_ref` |
| 104 | +* enum `zmq::socket_type` |
| 105 | +* enum `zmq::sockopt` |
| 106 | +* enum `zmq::send_flags` |
| 107 | +* enum `zmq::recv_flags` |
| 108 | +* class `zmq::message_t` |
| 109 | +* class `zmq::const_buffer` |
| 110 | +* class `zmq::mutable_buffer` |
| 111 | +* struct `zmq::recv_buffer_size` |
| 112 | +* alias `zmq::send_result_t` |
| 113 | +* alias `zmq::recv_result_t` |
| 114 | +* alias `zmq::recv_buffer_result_t` |
| 115 | +* class `zmq::error_t` |
| 116 | +* class `zmq::monitor_t` |
| 117 | +* struct `zmq_event_t`, |
| 118 | +* alias `zmq::free_fn`, |
| 119 | +* alias `zmq::pollitem_t`, |
| 120 | +* alias `zmq::fd_t` |
| 121 | +* class `zmq::poller_t` DRAFT |
| 122 | +* enum `zmq::event_flags` DRAFT |
| 123 | +* enum `zmq::poller_event` DRAFT |
| 124 | + |
| 125 | +Functions: |
| 126 | +* `zmq::version` |
| 127 | +* `zmq::poll` |
| 128 | +* `zmq::proxy` |
| 129 | +* `zmq::proxy_steerable` |
| 130 | +* `zmq::buffer` |
| 131 | +* `zmq::str_buffer` |
| 132 | + |
| 133 | +Extra high-level types and functions `zmq_addon.hpp`: |
| 134 | + |
| 135 | +Types: |
| 136 | +* class `zmq::multipart_t` |
| 137 | +* class `zmq::active_poller_t` DRAFT |
| 138 | + |
| 139 | +Functions: |
| 140 | +* `zmq::recv_multipart` |
| 141 | +* `zmq::send_multipart` |
| 142 | +* `zmq::send_multipart_n` |
| 143 | +* `zmq::encode` |
| 144 | +* `zmq::decode` |
| 145 | + |
| 146 | +Compatibility Guidelines |
| 147 | +======================== |
| 148 | + |
| 149 | +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): |
| 150 | + |
| 151 | +* Do not depend on any macros defined in cppzmq unless explicitly declared public here. |
| 152 | + |
| 153 | +The following macros may be used by consumers of cppzmq: `CPPZMQ_VERSION`, `CPPZMQ_VERSION_MAJOR`, `CPPZMQ_VERSION_MINOR`, `CPPZMQ_VERSION_PATCH`. |
| 154 | + |
| 155 | +Contribution policy |
| 156 | +=================== |
| 157 | + |
| 158 | +The contribution policy is at: http://rfc.zeromq.org/spec:22 |
| 159 | + |
| 160 | +Build instructions |
| 161 | +================== |
| 162 | + |
| 163 | +Build steps: |
| 164 | + |
| 165 | +1. Build [libzmq](https://github.com/zeromq/libzmq) via cmake. This does an out of source build and installs the build files |
| 166 | + - download and unzip the lib, cd to directory |
| 167 | + - mkdir build |
| 168 | + - cd build |
| 169 | + - cmake .. |
| 170 | + - sudo make -j4 install |
| 171 | + |
| 172 | +2. Build cppzmq via cmake. This does an out of source build and installs the build files |
| 173 | + - download and unzip the lib, cd to directory |
| 174 | + - mkdir build |
| 175 | + - cd build |
| 176 | + - cmake .. |
| 177 | + - sudo make -j4 install |
| 178 | + |
| 179 | +3. Build cppzmq via [vcpkg](https://github.com/Microsoft/vcpkg/). This does an out of source build and installs the build files |
| 180 | + - git clone https://github.com/Microsoft/vcpkg.git |
| 181 | + - cd vcpkg |
| 182 | + - ./bootstrap-vcpkg.sh # bootstrap-vcpkg.bat for Powershell |
| 183 | + - ./vcpkg integrate install |
| 184 | + - ./vcpkg install cppzmq |
| 185 | + |
| 186 | +Using this: |
| 187 | + |
| 188 | +A cmake find package scripts is provided for you to easily include this library. |
| 189 | +Add these lines in your CMakeLists.txt to include the headers and library files of |
| 190 | +cpp zmq (which will also include libzmq for you). |
| 191 | + |
| 192 | +``` |
| 193 | +#find cppzmq wrapper, installed by make of cppzmq |
| 194 | +find_package(cppzmq) |
| 195 | +target_link_libraries(*Your Project Name* cppzmq) |
| 196 | +``` |
0 commit comments