gpt4 book ai didi

c++ - 如何准备 msgpack 以通过 ZeroMQ 基础结构发送结构?

转载 作者:行者123 更新时间:2023-11-30 05:00:24 26 4
gpt4 key购买 nike

我正在尝试通过 ZeroMQ 在节点之间进行通信。我需要发送一个 struct:

struct Content{
std::vector<cv::Mat> image;
std::string msg;
};

我尝试利用 msgpack :

Content content;
content.image = msg2;
content.mesaj = "naber kardes";

msgpack::type::tuple<Content> src(content);
// serialize the object into the buffer.
// any classes that implements
// write(const char*,size_t) can be a buffer.
std::stringstream buffer;
msgpack::pack(buffer, src);
cout << sizeof(buffer) << endl;

但它给出:

/usr/local/include/msgpack/v1/object.hpp:631:11: error: no member named 'msgpack_pack' in 'Content'

对 c++ 还是新手。

如何在 msgpack 的帮助下通过 ZeroMQ 发送我的内容结构?

最佳答案

您需要为您的Content 结构提供一个pack() 函数:

namespace msgpack
{
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
{
namespace adaptor
{

template <>
struct pack<Content> {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& out, Content const& obj) const
{
out.pack(obj.image);
out.pack(obj.msg);
return out;
}
};

}
}
}

如果您的软件不评估 MessagePack 数据,您需要根据您的 zmq/MessagePack 接收者的格式/布局期望创建 pack() 函数。

convert() 函数示例(与上面的 pack() 函数相同的命名空间):

template <>
struct convert<Content> {
msgpack::object const& operator()(
msgpack::object const& o, Content& v) const
{
// unpack data in the same format as it was packed. see above!
return o;
}
};

关于c++ - 如何准备 msgpack 以通过 ZeroMQ 基础结构发送结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com