cppzmq is a C++ binding for libzmq. It has the following design goals:
There are other C++ bindings for ZeroMQ with different design goals. In particular, none of the following bindings are header-only:
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.
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:
zmq::context_t
zmq::ctxopt
zmq::socket_t
zmq::socket_ref
zmq::socket_type
zmq::sockopt
zmq::send_flags
zmq::recv_flags
zmq::message_t
zmq::const_buffer
zmq::mutable_buffer
zmq::recv_buffer_size
zmq::send_result_t
zmq::recv_result_t
zmq::recv_buffer_result_t
zmq::error_t
zmq::monitor_t
zmq_event_t
,zmq::free_fn
,zmq::pollitem_t
,zmq::fd_t
zmq::poller_t
DRAFTzmq::event_flags
DRAFTzmq::poller_event
DRAFTFunctions:
zmq::version
zmq::poll
zmq::proxy
zmq::proxy_steerable
zmq::buffer
zmq::str_buffer
Extra high-level types and functions zmq_addon.hpp
:
Types:
zmq::multipart_t
zmq::active_poller_t
DRAFTFunctions:
zmq::recv_multipart
zmq::send_multipart
zmq::send_multipart_n
zmq::encode
zmq::decode
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
.
The contribution policy is at: http://rfc.zeromq.org/spec:22
Build steps:
Build libzmq via cmake. This does an out of source build and installs the build files
git clone https://github.com/zeromq/libzmq.git
cd libzmq
mkdir build
cd build
cmake ..
sudo make -j4 install
Build cppzmq via cmake. This does an out of source build and installs the build files
git clone https://github.com/zeromq/cppzmq.git
cd cppzmq
mkdir build
cd build
cmake ..
or cmake -DCPPZMQ_BUILD_TESTS=OFF ..
to skip building testssudo make -j4 install
Alternatively, build cppzmq via vcpkg. This does an out of source build and installs the build files
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
(bootstrap-vcpkg.bat for Powershell)./vcpkg integrate install
./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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。