我试图通过ZMQ在网络上发送一个相当大的Flatbuffers对象,然后使用C++读取它。访问该对象时,会出现 Unresolved 异常,这些异常不知道如何解决。即使这个最小的示例也失败了:

Flatbuffers模式:

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

main.cpp使用REP套接字:
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:
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(),则会收到以下错误消息:
Unhandled exception at 0x000000013F373C53 in KUKAREQ.exe: 0xC0000005: Access violation reading location 0x00000000004B35D8.

我之前已经通过ZMQ成功发送了Flatbuffers对象,但是我没有在C++中阅读它们。我敢肯定,我会密切关注Flatbuffers tutorial,但是显然出了点问题。指针?缓冲区大小?无论哪种方式,我将不胜感激。

编辑:为了澄清我对已接受答案的评论,违规行为:
auto inmsg = flatbuffer::GetTestBuf(&reply);

它必须更改为:
auto inmsg = flatbuffer::GetTestBuf(reply.data());

谁读了这个问题,也许也有兴趣知道我以后遇到了一个错误,该错误是在FlatBufferBuilder函数的调用顺序不正确时发生的。显然,Flatbuffers对象的构建顺序很重要。发现一个人花了我一段时间-新手要当心。

最佳答案

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

通常,如果它在FlatBuffers中崩溃,则应使用验证程序来验证要传递给FlatBuffers的缓冲区。如果失败,则意味着您没有将合法数据传递给FlatBuffers,在这里就是这种情况。

另外,您可能想检查ZeroMQ是否可以发送缓冲区而不进行复制,这样会更快。

10-08 07:09