gpt4 book ai didi

c++ - Boost:反序列化通过 ZeroMQ pull socket 传递的自定义 C++ 对象

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:41 34 4
gpt4 key购买 nike

我在这里写的文字是我之前在另一个线程中打开的后续问题 Boost: Serializing/De-serializing a custom C++ object passed over ZeroMQ pull socket .早期线程中的编译问题已通过使用 textarchive 类型而不是 binaryarchive 类型得到解决,但现在我在反序列化时遇到了运行时问题。为了您的方便,我在这里用一个新的问题陈述重复前面的文字。我是 C++ 领域的新手,感谢任何进一步的帮助。

描述:

我有一个名为 GenericMessage 的 C++ 类,它只包含一个 id 和数据作为其成员(参见下面的代码片段 2 - GenericMessage.hxx)。我的目的是序列化此类的实例并通过实现推送模式的 ZeroMQ 套接字发送它。

序列化和发送任务已在 类 ZMQHandler 中实现(参见 sendToBE 函数),它位于 代码段 3 中所示的头文件名称 ZMQHandler.hxx 中以下。此类由 TestFE.cxx 实例化,显示在下面的第 4 个代码片段中。

GenericMessage 实例的接收和反序列化在TestBE.cxx 中实现,可在下面的第 5 段代码 中找到。我的意图是通过 ZMQ 套接字(即拉式套接字)接收 GenericMessage 实例,对其进行反序列化,然后将其成员打印到标准输出。

问题陈述:

当我运行接收器(即 TestBE.cxx)时,我在这里验证了我可以通过 ZMQ 套接字将数据从 TestFE.cxx 传输到 TEstBE.cxx。但是,当我尝试反序列化 TestBE.cxx 中第 28 行的输入存档时,我在下面的第一个代码片段中得到了一个异常(请参阅下面的第 5 个代码片段,第 28 行已被标记)。

在代码片段 5 的 TestBE.cxx 中实现的反序列化过程中,我是否遗漏了什么?为什么你认为我会得到这个异常(exception)?可能是我在 ZMQHandler.cxx 中实现的序列化过程中缺少 st(代码片段 3 - 函数 sendToBE)。提前致谢。

代码片段 1 GDB 输出和回溯

$ gdb TestBE 
GNU gdb (GDB) 7.5-ubuntu
.....
(gdb) r
Starting program: /TestBE
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
[New Thread 0xb7c12b40 (LWP 16644)]
[New Thread 0xb7411b40 (LWP 16645)]
Connecting to FE...

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!

 **CHAR [22 serialization::archive 9 0 1 0
0 1 12 Hello there!]
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid**

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!

 Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fdd424 in __kernel_vsyscall ()
#1 0xb7c7a1df in raise () from /lib/i386-linux-gnu/libc.so.6
#2 0xb7c7d825 in abort () from /lib/i386-linux-gnu/libc.so.6
#3 0xb7e608ad in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/i386- linux-gnu/libstdc++.so.6
#4 0xb7e5e4f3 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#5 0xb7e5e52f in std::terminate() () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#6 0xb7e5e825 in __cxa_rethrow () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#7 0x0804c1d4 in boost::archive::detail::pointer_iserializer<boost::archive::text_iarchive, GenericMessage<std::string> >::load_object_ptr (this=0x8054444, ar=...,
x=@0x805cb40: 0x805cb50, file_version=0) at /usr/include/boost/archive/detail/iserializer.hpp:327
#8 0xb7f3839d in boost::archive::detail::basic_iarchive::load_pointer(void*&, boost::archive::detail::basic_pointer_iserializer const*, boost::archive::detail::basic_pointer_iserializer const* (*) (boost::serialization::extended_type_info const&)) () from /usr/lib/libboost_serialization.so.1.49.0
#9 0x0804bea9 in boost::archive::detail::load_pointer_type<boost::archive::text_iarchive>::invoke<GenericMes sage<std::string>*> (ar=..., t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/iserializer.hpp:524
#10 0x0804be55 in boost::archive::load<boost::archive::text_iarchive, GenericMessage<std::string>*> (ar=..., t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/iserializer.hpp:592
#11 0x0804be36 in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<Gener icMessage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/common_iarchive.hpp:66
#12 0x0804be14 in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<GenericMe ssage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/basic_text_iarchive.hpp:65
#13 0x0804bdf2 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<GenericMes sage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/text_iarchive.hpp:82
#14 0x0804bcec in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>> <GenericMessage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/interface_iarchive.hpp:60
#15 0x0804b2a1 in main () at TestBE.cxx:28

代码片段 2 (GenericMessage.hxx)

#include <iostream>
#include <string>
#include <sstream>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

template <class T>
class GenericMessage {
public:
GenericMessage():
beId(-1),
data(NULL)
{}

GenericMessage(int id, T msg):
beId(id),
data(msg)
{}

~GenericMessage(){}

T getData()
{
return data;
}


std::string toString()
{
std::ostringstream ss;
ss << getBeId();
std::string ret = ss.str();

return ret;
}

void setBeId(int id)
{
beId = id;
}

int getBeId()
{
return beId;
}


private:
friend class boost::serialization::access;

int beId;
T data;


template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & beId;
ar & data;
}

};

代码片段 3 (ZmqHandler.hxx)

#include "zmq.hpp"
#include "GenericMessage.hxx"
#include <unistd.h>
#include <cassert>

template <class A>
class ZmqHandler {
public:

ZmqHandler():
mContext(1),
mOutbHandlerSocket(mContext, ZMQ_PUSH)
{
mOutbHandlerSocket.bind ("tcp://*:5555");
}


~ZmqHandler() {}

void sendToBE(GenericMessage<A> *theMsg)
{
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);

try
{
archive << theMsg;
} catch (boost::archive::archive_exception& ex) {
std::cout << "Archive Exception during deserializing:" << std::endl;
std::cout << ex.what() << std::endl;
} catch (int e) {
std::cout << "EXCEPTION " << e << std::endl;
}

std::string outbound_data_ = archive_stream.str();
const char * buf = outbound_data_.c_str();
int len = strlen((const char*)buf);
std::cout << "LENGTH [" << len << "]" << std::endl;

zmq::message_t msgToSend(len);

memcpy ((char *) msgToSend.data(), buf, len);

if(memcmp((char *) msgToSend.data(), buf, len) != 0)
{
std::cout << "memcpy error!" << std::endl;
}

mOutbHandlerSocket.send(msgToSend);
std::cout << "SENT request: [" << theMsg->toString() << "]" << std::endl;
}

private:
zmq::context_t mContext;
zmq::socket_t mOutbHandlerSocket;
};

代码片段 4 (TestFE.cxx)

  #include "ZmqHandler.hxx"

int main ()
{
ZmqHandler<std::string> zmqHandler;
int counter = 1;

while(1)
{
std::string data = "Hello there!\0";
GenericMessage<std::string> msg(counter, data);
zmqHandler.sendToBE(&msg);
counter++;
sleep(1);
}

return 0;
}

代码片段 5 (TestBE.cxx)

 #include "zmq.hpp"
#include "GenericMessage.hxx"
#include <fstream>

int main ()
{
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_PULL);

std::cout << "Connecting to FE..." << std::endl;
socket.connect ("tcp://localhost:5555");

while(1){
zmq::message_t reply;
socket.recv (&reply);

const char *buf = static_cast<const char*>(reply.data());
std::cout << "CHAR [" << buf << "]" << std::endl;

std::string input_data_(buf);
std::istringstream archive_stream(input_data_);
boost::archive::text_iarchive archive(archive_stream);
GenericMessage<std::string> *theMsg;

try
{
/* !!!!!!!!!! LINE 28 is the following !!!!!!!!!!*/
archive >> theMsg;
} catch (boost::archive::archive_exception& ex) {
std::cout << "Archive Exception during deserializing:" << std::endl;
std::cout << ex.what() << std::endl;
} catch (int e) {
std::cout << "EXCEPTION " << e << std::endl;
}

std::cout << "ID" << theMsg->getBeId() << std::endl;
std::cout << "Data" << theMsg->getData() << std::endl;

}

return 0;
}

最佳答案

您声明了 theMsg作为指针 ( GenericMessage<std::string> *theMsg; )。

尝试将该行更改为 GenericMessage<std::string> theMsg; .`

异常的真正来源

GenericMessage默认构造函数,你初始化dataNULL .但是,您不允许初始化 std::stringNULL指针。不要初始化你的 data默认构造函数中的成员。

GenericMessage()
: beId(-1)
{}

只要你输入T有一个默认构造函数,编译器将在生成模板时处理它的初始化。

(希望)有用的提示 #1

zmq::message_t 中的数据缓冲区是(通常)不是 NULL 终止的。收到消息后,请注意如何将缓冲区转换为字符串。

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

const char *buf = static_cast<const char*>(reply.data());
std::cout << "CHAR [" << buf << "]" << std::endl;

//std::string input_data_(buf); // assumes a null-term string
std::string input_data_( buf, reply.size() );
// snip

(希望)有用的提示 #2

另外,我注意到 ZmqHandler.hxx 中有一些东西。

// snip
std::string outbound_data_ = archive_stream.str();
const char * buf = outbound_data_.c_str();
int len = strlen((const char*)buf);
std::cout << "LENGTH [" << len << "]" << std::endl;

zmq::message_t msgToSend(len);

memcpy ((char *) msgToSend.data(), buf, len);

if(memcmp((char *) msgToSend.data(), buf, len) != 0)
{
std::cout << "memcpy error!" << std::endl;
}
// snip

你不需要检查 memcpy 的结果(除非你想检查它的返回值)。整个 block 可以更改为如下所示:

std::string outbound_data_ = archive_stream.str();
// no need to use the c-style string function 'strlen'
int len = outbound_data_.length();
std::cout << "LENGTH [" << len << "]" << std::endl;

zmq::message_t msgToSend(len);
memcpy( msgToSend.data(), outbound_data_.data(), len );

关于c++ - Boost:反序列化通过 ZeroMQ pull socket 传递的自定义 C++ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565538/

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