0

I have the following code and I am trying to send a capnp message over ZMQ:

::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);

I am using the publish-subscribe pattern & I am getting the following error:

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)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
nik
  • 1

2 Answers2

2

kj::ArrayPtr is from the KJ library, which is part of Cap'n Proto. ZeroMQ does not know anything about KJ, so won't recognize that type. It either wants a message_t (a ZeroMQ type) or a pointer/size combo. You can do:

auto bytes = words.asBytes();
_publisher.send(bytes.begin(), bytes.size());
Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
0

I haven't used zeromq, and I'm only just starting to use capnp, but I really enjoy the simplicity that capnp brings to sending data via socket.

I would use capnp functions over zeromq to send and receive data when that data is also serialized by capnp if you can:

capnp::writeMessageToFd(fd, builder)

where fd is the socket and builder is your builder: message

On the receiving end you can wait for it with something like capnp::StreamFdMessageReader message(socket)

If you really wanted to pack this thing into a format that zeromq could take, perhaps converting the array to bytes, as is detailed here, will be acceptable for the constructor of your zeromq message.

Good luck!

Ditofry
  • 76
  • 4
  • I added kj::ArrayPtr bytes = words.asBytes();_publisher.send(bytes); I am still getting a similar error: no known conversion from 'kj::ArrayPtr' to 'zmq::message_t &' @Ditofry – nik Jul 29 '16 at 08:40