0

I am trying to make a simple broker (i.e. star topology) that allows any DEALER node to talk to any other DEALER node that has also connected to the broker. Here is the current setup:

// Simple broker
#include <string>
#include <zmq.hpp>
#include <zhelpers.hpp>

using namespace std;

int main()
{
    zmq::context_t context(1);
    zmq::socket_t router(context, ZMQ_ROUTER);
    router.bind("ipc://router.ipc");

    while (true)
    {
        string from_address = s_recv(router);
        string to_address   = s_recv(router);
        string message      = s_recv(router);

        s_sendmore(router, to_address);
        s_sendmore(router, from_address);
        s_send(router, message);
    }

    return 0;
}

This works, however, I feel like I just made this up and that it is inefficient to copy the incoming frames (esp. the data) into strings when I'm just swapping the positions of the first two frames and sending it right back out.

My question is - is there a standard way of doing what I am trying to do here?

It seems like every single google query sends me back to the monolithic Guide/Bible instead of implementation examples...

Gillespie
  • 5,780
  • 3
  • 32
  • 54

1 Answers1

2

AFAIK there's no other way.

Fundamentally you are reading data from a socket, changing it (swapping to_address & from_address around), and sending it back out.

The change (the swap) is important. ZMQ is a transport, it simply shifts your program's data from A to B. That's all it does. It doesn't have an ability to change the data in any way.

So it's inevitable that the messages have to come out of the transport, and be put back into it again.

You could try the zerocopy alternatives. That avoids some of the copying that's going on.

bazza
  • 7,580
  • 15
  • 22