ZIO
Python and C++ interface to ZeroMQ and Zyre
test_cppzmq.cpp
Go to the documentation of this file.
1 #include "zio/cppzmq.hpp"
2 #include "zio/main.hpp"
3 #include "zio/logging.hpp"
4 
5 #ifndef ZMQ_CPP11
6 #error c++11 should be defined
7 #endif
8 
9 #ifndef ZMQ_CPP14
10 #error c++14 should be defined
11 #endif
12 
13 #ifndef ZMQ_CPP17
14 #error c++17 should be defined
15 #endif
16 
17 #include <unistd.h>
18 using namespace std;
19 
20 int main()
21 {
22  zio::init_all();
23 
25  zio::socket_t s(ctx, ZMQ_SERVER);
26  s.bind("inproc://testcppzmq");
27  zio::socket_t c(ctx, ZMQ_CLIENT);
28  c.connect("inproc://testcppzmq");
29 
30  usleep(1000);
31 
32  const std::string hw = "Hello World!";
33 
34  zio::debug("sending {}", hw);
35  zio::message_t smsg(hw.data(), hw.size());
36  auto ses = c.send(smsg, zio::send_flags::none);
37  assert (ses);
38  assert (*ses == hw.size());
39  assert (ses.value() == hw.size());
40 
41 
42  zio::debug("receiving");
43  zio::message_t rmsg;
44  auto res = s.recv(rmsg);
45  assert (res);
46  assert (*res == hw.size());
47  assert (res.value() == hw.size());
48  assert (rmsg.size() == hw.size());
49  std::string hw2(static_cast<char*>(rmsg.data()), rmsg.size());
50  assert (hw2.size() == hw.size());
51  assert (hw2 == hw);
52 
53  assert (rmsg.routing_id());
54 
55  ses = s.send(rmsg, zio::send_flags::none);
56  assert(ses);
57 
58  res = c.recv(rmsg);
59  assert (res);
60  assert (*res == hw.size());
61  assert (res.value() == hw.size());
62  assert (rmsg.size() == hw.size());
63  std::string hw3(static_cast<char*>(rmsg.data()), rmsg.size());
64  assert (hw3.size() == hw.size());
65  assert (hw3 == hw);
66 
67 
68  return 0;
69 }
int main()
Definition: test_cppzmq.cpp:20
size_t send(const void *buf_, size_t len_, int flags_=0)
Definition: zmq.hpp:1345
void * data() ZMQ_NOTHROW
Definition: zmq.hpp:481
void init_all()
Definition: main.cpp:22
void connect(std::string const &addr)
Definition: zmq.hpp:1324
size_t size() const ZMQ_NOTHROW
Definition: zmq.hpp:488
void bind(std::string const &addr)
Definition: zmq.hpp:1306