|
Boost Users : |
Subject: [Boost-users] serialization using archive
From: Long P (longcae_at_[hidden])
Date: 2010-07-15 21:33:55
Hi
I have come across the example for serialization in http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/example/serialization/connection.hpp
In the following method async_write, the data typename T is put into an archive before being sent out. I would need to do the same thing here but the archive should be binary stream because I need to send this data to a third party application which expects a binary stream. Does anyone know how to do it?
Thanks & Regards
Long
/// Asynchronously write a data structure to the socket.
template <typename T, typename Handler>
void async_write(const T& t, Handler handler)
{
// Serialize the data first so we know how large it is.
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << t;
outbound_data_ = archive_stream.str();
// Format the header.
std::ostringstream header_stream;
header_stream << std::setw(header_length)
<< std::hex << outbound_data_.size();
if (!header_stream || header_stream.str().size() != header_length)
{
// Something went wrong, inform the caller.
boost::system::error_code error(boost::asio::error::invalid_argument);
socket_.io_service().post(boost::bind(handler, error));
return;
}
outbound_header_ = header_stream.str();
// Write the serialized data to the socket. We use "gather-write" to send
// both the header and the data in a single write operation.
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(outbound_header_));
buffers.push_back(boost::asio::buffer(outbound_data_));
boost::asio::async_write(socket_, buffers, handler);
}
_________________________________________________________________
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net