本文介绍了ZeroMQ 简单代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的代理(即星型拓扑),它允许任何 DEALER 节点与也连接到该代理的任何其他 DEALER 节点通信.这是当前设置:

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...

推荐答案

AFAIK 没有其他办法.

AFAIK there's no other way.

从根本上说,您正在从套接字读取数据,更改它(交换 to_address & from_address ),然后将其发送回.

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

变化(交换)很重要.ZMQ 是一种传输,它只是将程序的数据从 A 转移到 B.这就是它所做的全部.它没有以任何方式更改数据的能力.

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.

您可以尝试使用 zerocopy 替代方案.这避免了一些正在发生的复制.

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

这篇关于ZeroMQ 简单代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:54