Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ Multipart message second part 0 size

Tags:

c++

zeromq

I'm trying to receive a multi part message using a sub/pub pattern in ZMQ 4.2.3 and cppzmq I'm able to successfully send and receive single part messages but when I try to read in the second frame it's size is always 0. What has me completely baffled is a C# version using NetMQ reads the second frame without issue, causing me to believe it is being sent properly. I know I'm missing something but today is my second day of trying to figure out what with no success.

Here is my pub code

#include <iostream>
#include "zmq_addon.hpp"
void main()
{
    zmq::context_t ctx = zmq::context_t();
    zmq::socket_t pub = zmq::socket_t(ctx, zmq::socket_type::pub);
    try
    {
        //binding using localhost gives an error about invalid device?
        pub.bind("tcp://*:8845");
    }
    catch (...)
    {
        std::cout << zmq_strerror(zmq_errno());
        std::cin.get();
        return;
    }
    byte topic = 8;
    std::string id = "ragefire.bob";
    while (true)
    {
        std::cout << "Spam\n";
        pub.send(id.c_str(), id.length(), ZMQ_SNDMORE);
        pub.send("foo", 3);
    }
}

And my C++ sub code

#include <iostream>
#include "zmq_addon.hpp"
int main()
{
    zmq::context_t ctx = zmq::context_t();
    zmq::socket_t sub = zmq::socket_t(ctx, zmq::socket_type::sub);
    sub.connect("tcp://localhost:8845");
    std::string id = "ragefire.bob";
    sub.setsockopt(ZMQ_SUBSCRIBE, id.c_str(), id.length());
    while (true)
    {
        zmq::message_t msg;
        if(sub.recv(&msg,ZMQ_NOBLOCK))
        {
            auto rpl = std::string(static_cast<char*>(msg.data()), msg.size());
            std::cout << "Recv returned true! " << rpl << "\n";
            int more;
            auto more_size = sizeof(more);
            sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
            while (more)
            {
                zmq::message_t moreMsg;
                sub.recv(&msg);
                std::string moreRpl = std::string(static_cast<char*>(moreMsg.data()), moreMsg.size());
                std::cout << "There's more! " << moreRpl << "Size is " << moreMsg.size() << "\n";
                sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
            }
        }
        else
            std::cout << "Recv returned false! " << zmq_strerror(zmq_errno()) << "\n";
    }
}

With an output of

Recv returned true! ragefire.bob
There's more! Size is 0

For completeness, my NetMQ sub that can read both of the frames

static void Main()
{
    SubscriberSocket sub = new SubscriberSocket("tcp://localhost:8845");
    sub.SubscribeToAnyTopic();
    while (true)
    {
        NetMQMessage msg = sub.ReceiveMultipartMessage();
        Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg.First.Buffer)}");
        Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg[1].Buffer)}");
    }
}
like image 998
Mark Underwood Avatar asked Oct 18 '22 07:10

Mark Underwood


1 Answers

Your initial call to sub.recv() is receiving the message in &msg, which is fine.

Your subsequent call to sub.recv() is also receiving into &msg, but you are then reporting about moreMsg. If you really want to keep a second message_t around, do

sub.recv(&moreMsg);
like image 82
Conrad Parker Avatar answered Oct 20 '22 23:10

Conrad Parker