Problem

一旦数据序列化后, 送给zeromq的就是编码后的数据, 这时就无法使用envelop来过滤数据。

flowchart LR
envelope --> mix["envelop + data"]
mix --> fb["flatbuffer"]
fb --> zmq_send["zeromq send"]
zmq_send --> zmq_recv["zeromq receive"]

Solution

//  Prepare publisher
zmq::socket_t publisher(*ctx, zmq::socket_type::pub);
publisher.bind("inproc://#1");

// Give the subscribers a chance to connect, so they don't lose any messages
std::this_thread::sleep_for(std::chrono::milliseconds(20));

//  Write three messages, each with an envelope and content
publisher.send(zmq::str_buffer("A"), zmq::send_flags::sndmore);
publisher.send(zmq::str_buffer("Message in A envelope"));
std::this_thread::sleep_for(std::chrono::milliseconds(100));

先使用zmq::send_flags::sndmore发送envelop, 再发送序列化后的数据

flowchart LR
data --> fb["flatbuffer"]
fb --> ser_data["serialized data"]
ser_data --> zmq_send_more["zeromq send envelope with more"]
zmq_send_more --> zmq_send["zeromq send data"]
zmq_send --> zmq_recv["zeromq receive"]

subscriber

使用zmq::send_flags::sndmore后, 接受侧会有一些变化, 每次会收到两个zmq::message_t, 如下所示要使用一个vector来接受消息, 第一个消息是envelope, 第二个消息是content。

//  Prepare subscriber
zmq::socket_t subscriber(*ctx, zmq::socket_type::sub);
subscriber.connect("inproc://#1");

//  Thread2 opens "A" and "B" envelopes
subscriber.set(zmq::sockopt::subscribe, "A");

// Receive all parts of the message
std::vector<zmq::message_t> recv_msgs;
zmq::recv_result_t result =
  zmq::recv_multipart(subscriber, std::back_inserter(recv_msgs));
assert(result && "recv failed");
assert(*result == 2);

std::cout << "Thread2: [" << recv_msgs[0].to_string() << "] "
          << recv_msgs[1].to_string() << std::endl;

Reference

https://github.com/zeromq/cppzmq/blob/master/examples/pubsub_multithread_inproc.cpp


harriszh
338 声望131 粉丝

做些有趣的事,留些有用的存在


引用和评论

0 条评论