中通过ZMQ发送的Flatbuffers对象抛出未处理的异常

中通过ZMQ发送的Flatbuffers对象抛出未处理的异常

本文介绍了在C ++中通过ZMQ发送的Flatbuffers对象抛出未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过ZMQ通过网络发送一个相当大的Flatbuffers对象,然后使用C + +读取它。当访问对象时,我得到未处理的异常,我不知道如何解决。即使此最小示例失败:

I am trying to send a reasonably big Flatbuffers object over the network via ZMQ and then read it using C++. When accessing the object, I get unhandled exceptions that I don't know how to solve. Even this minimal example fails:

flatbuffers模式:

The flatbuffers schema:

namespace flatbuffer;
table TestBuf {
  testStatus:bool;
  testNumber:double;
  testInt:int;
}
root_type TestBuf;

使用REP套接字的main.cpp:

The main.cpp using the REP socket:

int main() {
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REP);
    socket.bind("tcp://*:5555");

    std::cout << "Listening for requests." << std::endl;
    std::cout << "-----" << std::endl;

    double count = 0;
    while (1) {
        zmq::message_t request;
        socket.recv(&request);

        // Read incoming data
        auto reqmsg = flatbuffer::GetTestBuf(&request);
        std::cout << "Received: " << reqmsg << std::endl;

        flatbuffers::FlatBufferBuilder fbb;
        flatbuffer::TestBufBuilder builder(fbb);

        count++;
        builder.add_testNumber(count);
        std::cout << "Sending " << count << std::endl;

        auto response = builder.Finish();
        fbb.Finish(response);

        // Send the flatbuffer
        int buffersize = fbb.GetSize();
        zmq::message_t message(buffersize);
        memcpy((void *)message.data(), fbb.GetBufferPointer(), buffersize);
        socket.send(message);
    }
    return 0;
}

使用REQ套接字的main.cpp:

The main.cpp using the REQ socket:

int main() {
    // Prepare ZMQ context and socket
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REQ);
    std::cout << "Sending out data requests." << std::endl;
    socket.connect("tcp://localhost:5555");

    double count = 0;
    while (1) {
        // Formulate response
        flatbuffers::FlatBufferBuilder fbb;
        flatbuffer::TestBufBuilder builder(fbb);

        count++;
        builder.add_testNumber(count);
        auto response = builder.Finish();
        fbb.Finish(response);

        // Send the flatbuffer
        std::cout << "Sending. " << count << ". ";
        int buffersize = fbb.GetSize();
        zmq::message_t message(buffersize);
        memcpy((void *)message.data(), fbb.GetBufferPointer(), buffersize);
        socket.send(message);
        std::cout << "Sent. ";

        // Receive reply
        zmq::message_t reply;
        socket.recv(&reply);

        // Read the data
        auto inmsg = flatbuffer::GetTestBuf(&reply);
        std::cout << " Received reply: " << inmsg << std::endl;

        //auto num = inmsg->testNumber();
        //std::cout << num << " test number.";
    }
    return 0;
}

此代码运行正常,并显示接收。奇怪的是,它并没有改变,虽然消息的内容应该是。如果我取消注释最后两行并尝试访问inmsg-> testNumber(),我得到这个错误消息:

This code runs fine and displays (I think) the raw buffer each program is receiving. Strangely, it is not changing, although the content of the message should be. If I uncomment the last two lines and try to access inmsg->testNumber(), I get this error message:

Unhandled exception at 0x000000013F373C53 in KUKAREQ.exe: 0xC0000005: Access violation reading location 0x00000000004B35D8.

我已经通过ZMQ成功发送了Flatbuffers对象,但是我还没有在C ++中读取它们。我相当确定我遵循了,但是显然出现了问题。指针?缓冲区大小?

I have sent Flatbuffers objects through ZMQ successfully before, but I have not read them in C++. I am fairly sure I followed the Flatbuffers tutorial closely, but something is obviously going wrong. Pointers? Buffer sizes? Either way I would appreciate help.

推荐答案

不熟悉ZeroMQ,但 flatbuffer :: GetTestBuf ;请求)这看起来有问题..你需要传递缓冲区,而不是消息结构。可能 request.data()或类似的效果更好。

Not familiar with ZeroMQ, but flatbuffer::GetTestBuf(&request) this looks problematic.. you need to pass the buffer, not the message structure. Likely request.data() or similar works better.

一般来说,如果它在FlatBuffers崩溃,使用验证程序验证您传递给FlatBuffers的缓冲区。如果失败,这意味着你不传递法律数据到FlatBuffers,如这里的情况。

In general, if it crashes in FlatBuffers, you should use the verifier to verify the buffer you're passing to FlatBuffers. If that fails, it means you're not passing legal data to FlatBuffers, as is the case here.

此外,你可能想检查ZeroMQ是否可以发送缓冲区复制,会更快。

Also, you may want to check if ZeroMQ can send buffers without copying, will be faster.

这篇关于在C ++中通过ZMQ发送的Flatbuffers对象抛出未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:51