增强Serialiaze档案转换为char

boost serialiaze archive convert to char

本文关键字:char 转换 档案 Serialiaze 增强      更新时间:2023-10-16

我已经成功地序列化了一个用以下代码的对象:

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

这是protocolbaseser dowrite:

void protocolBaseServer::DoWrite(std::string inMessage){
   _totalMessage=inMessage;
   std::cout << "totalMessage: " << _totalMessage << std::endl;
   int32_t dataLength=_totalMessage.length();
   int32_t orgdataLength=_totalMessage.length();
   std::cout << "data length: " << orgdataLength << std::endl;
   //read in todo
   dataLength = htonl(dataLength ); // Ensure host system byte order on the int;from the network will be network byte order
   std::cout << "writing length now" << std::endl;
   protoService<int32_t>::DoWrite(_sock,&dataLength,sizeof(int32_t));
   std::cout << "written length" << std::endl;
   std::cout << "writing :" << _totalMessage.data() << std::endl;
   protoService<int32_t>::DoWrite(_sock,(void*)_totalMessage.data(),orgdataLength); // send the string dataEE
   std::cout << "wrote :" << _totalMessage << std::endl;
}

为什么当我将_totalMessage转换为C字符串以通过插座发送时,它没有发送。

当我尝试将_totalMessage.c_str()打印到屏幕上时,它什么也没打印?这是上面的输出。如您所见,"写作:"行没有显示一些内容。我觉得我缺少将此存档转换为角色数组以发送我的插座的东西?

writing :
Size to write: 38
wrote :22 serialization::archive 12 0 0 0 0

有趣的是,准备要发送的字符串的函数实际上会输出" Outbound.c:"下面:

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   {  
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   }  
   archive_stream.flush();
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   std::cout << "outbound: " << outbound_data << std::endl;
   std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

编辑。我似乎找到了罪魁祸首。我想对出站字符串进行炭化,所以我这样做了:_intype是const char = 0x01。

似乎您无法使用 运算符将const char串联到字符串中。它会弄乱.c_str()返回。

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   {  
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   }  
   archive_stream.flush();
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   std::cout << "outbound: " << outbound_data << std::endl;
   std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

我发现了。我想的那样,我的_intype变量没有设置为0x01。它设置为0x00。

也许您需要在使用结果之前关闭存档:

{
    boost::archive::text_oarchive archive(archive_stream);
    archive << *this;
}
archive_stream.flush();
std::string outbound_data=archive_stream.str();