我正在尝试序列化一个数据结构,通过网络发送它并在另一端反序列化它。如果双方始终编译为 x64 或 x86,则工作得非常好,但即使我只序列化一个布尔值,它也不会在两者之间工作。
序列化代码:
Myclass myc;
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << myc;
// get stringstream's length
ss.seekg(0, ios::end);
int len = ss.tellg();
ss.seekg(0, ios::beg);
// allocate CORBA type and copy the stringstream buffer over
const std::string copy = ss.str(); // copy since str() only generates a temporary object
const char* buffer = copy.c_str();
// ByteSequence is a sequence of octets which again is defined as a raw, platform-independent byte type by the CORBA standard
IDL::ByteSequence_var byteSeq(new IDL::ByteSequence());
byteSeq->length(len);
memcpy(byteSeq->get_buffer(), buffer, len);
return byteSeq._retn();
反序列化代码:
IDL::ByteSequence_var byteSeq;
byteSeq = _remoteObject->getMyClass();
// copy CORBA input sequence to local char buffer
int seqLen = byteSeq->length();
std::unique_ptr<char[]> buffer(new char[seqLen]);
memcpy(buffer.get(), byteSeq->get_buffer(), seqLen);
// put buffer into a stringstream
std::stringstream ss;
std::stringbuf* ssbuf = ss.rdbuf();
ssbuf->sputn(buffer.get(), seqLen);
// deserialize from stringstream
// throws exception 'Unsupported version' between x86 and x64
boost::archive::text_iarchive ia(ss);
MyClass result;
ia >> result;