无法通过ZMQ发送capnp消息

时间:2016-07-28 22:20:51

标签: c++ arrays serialization zeromq capnproto

我有以下代码,我正在尝试通过ZMQ发送capnp消息:

::capnp::MallocMessageBuilder message;    
Guitar::Builder guitar = message.initRoot<Guitar>();  
guitar.setModel(1); 
guitar.setPrice(1200);

kj::Array<capnp::word> words = messageToFlatArray(message);

_publisher.send(words);

我正在使用发布 - 订阅模式&amp;我收到以下错误:

error: no matching member function for call to  
      'send'  
        _publisher.send(words);  
        ~~~~~~~~~~~^~~~ /usr/local/include/zmq.hpp:610:21: note: candidate function not viable: no known conversion from  
'kj::Array<capnp::word>' to  
      'zmq::message_t &' for 1st argument  
        inline bool send (message_t &msg_, int flags_ = 0)  
                    ^ /usr/local/include/zmq.hpp:627:21: note: candidate   function not viable: no known conversion from
'kj::Array<capnp::word>' to
      'zmq::message_t' for 1st argument
        inline bool send (message_t &&msg_, int flags_ = 0)
                    ^ /usr/local/include/zmq.hpp:620:35: note: candidate function template not viable: requires at least 2 arguments,
but 1 was
      provided
        template<typename I    bool send(I first, I last, int flags_=0)
                                  ^ /usr/local/include/zmq.hpp:600:23: note: candidate function not viable: requires at least 2 arguments,
but 1 was provided
        inline size_t send (const void *buf_, size_t len_, int flags_ = 0)

2 个答案:

答案 0 :(得分:1)

kj::ArrayPtr来自KJ图书馆,它是Cap&n; Proto的一部分。 ZeroMQ对KJ一无所知,因此不会认识到这种类型。它要么需要message_t(ZeroMQ类型),要么需要指针/大小组合。你可以这样做:

auto bytes = words.asBytes();
_publisher.send(bytes.begin(), bytes.size());

答案 1 :(得分:0)

我没有使用过zeromq,我只是刚刚开始使用capnp,但我真的很喜欢capnp通过socket发送数据所带来的简单性。

如果可以的话,我会使用capnp functions而不是zeromq来发送和接收数据,当数据也被capnp序列化时:

capnp::writeMessageToFd(fd, builder)

其中fd是套接字而builder是您的构建者:message

在接收端,您可以使用capnp::StreamFdMessageReader message(socket)

等等

如果你真的想把这个东西打包成zeromq可以采用的格式,也许将数组转换为字节,详见here,你的zeromq消息的构造函数是可以接受的。

祝你好运!