Skip to content

Commit

Permalink
Refactor crete/asio/common.h and add comments about usage (SVL-PSU#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
likebreath committed Feb 22, 2017
1 parent 31dfbf9 commit 02c5b47
Showing 1 changed file with 27 additions and 114 deletions.
141 changes: 27 additions & 114 deletions lib/include/crete/asio/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,12 @@ struct PacketInfo
uint64_t type; // Type of the associated packet.
};

// binary-archive based: for general communication from the same platform
// NOTE: xxx non-portable for cross-platform, but most efficient
template <typename Connection, typename T>
void write_serialized_text(Connection& connection, PacketInfo& pktinfo, T& t)
{
boost::asio::streambuf sbuf;
std::ostream os(&sbuf);

boost::archive::text_oarchive oa(os);
oa << t;

pktinfo.size = sbuf.size();

connection.write(sbuf, pktinfo);
}

template <typename Connection, typename T>
void write_serialized_binary(Connection& connection, PacketInfo& pktinfo, T& t)
void write_serialized_binary(Connection& connection,
PacketInfo& pktinfo,
T& t)
{
boost::asio::streambuf sbuf;
std::ostream os(&sbuf);
Expand All @@ -111,50 +101,40 @@ void write_serialized_binary(Connection& connection, PacketInfo& pktinfo, T& t)
connection.write(sbuf, pktinfo);
}

template <typename Connection>
void write_serialized_text(
Connection& connection,
PacketInfo& pktinfo,
const boost::property_tree::ptree& t,
size_t version)
template <typename Connection, typename T>
void read_serialized_binary(Connection& connection,
T& t,
uint32_t pk_type)
{
boost::asio::streambuf sbuf;
std::ostream os(&sbuf);
PacketInfo pkinfo = connection.read(sbuf);

boost::archive::text_oarchive oa(os);
boost::property_tree::save(oa, t, version);
CRETE_EXCEPTION_ASSERT(pkinfo.type == pk_type,
err::network_type_mismatch(pkinfo.type));

pktinfo.size = sbuf.size();
std::istream is(&sbuf);
boost::archive::binary_iarchive ia(is);

connection.write(sbuf, pktinfo);
ia >> t;
}

// text-archive based: for general communication from different platforms
template <typename Connection, typename T>
void write_serialized_text_xml(Connection& connection, PacketInfo& pktinfo, T& t)
void write_serialized_text(Connection& connection,
PacketInfo& pktinfo,
T& t)
{
boost::asio::streambuf sbuf;
std::ostream os(&sbuf);

boost::archive::xml_oarchive oa(os);
oa << BOOST_SERIALIZATION_NVP(t);
boost::archive::text_oarchive oa(os);
oa << t;

pktinfo.size = sbuf.size();

connection.write(sbuf, pktinfo);
}

template <typename Connection, typename T>
void read_serialized_text(Connection& connection, T& t)
{
boost::asio::streambuf sbuf;
connection.read(sbuf);

std::istream is(&sbuf);
boost::archive::text_iarchive ia(is);

ia >> t;
}

template <typename Connection, typename T>
void read_serialized_text(Connection& connection,
T& t,
Expand All @@ -172,10 +152,13 @@ void read_serialized_text(Connection& connection,
ia >> t;
}

template <typename T>
void read_serialized_text(boost::asio::streambuf& sbuf,
T& t)
// Misc
template <typename Connection, typename T>
void read_serialized_text(Connection& connection, T& t)
{
boost::asio::streambuf sbuf;
connection.read(sbuf);

std::istream is(&sbuf);
boost::archive::text_iarchive ia(is);

Expand All @@ -192,76 +175,6 @@ void read_serialized_binary(boost::asio::streambuf& sbuf,
ia >> t;
}

template <typename Connection, typename T>
void read_serialized_binary(Connection& connection,
T& t,
uint32_t pk_type)
{
boost::asio::streambuf sbuf;
PacketInfo pkinfo = connection.read(sbuf);

CRETE_EXCEPTION_ASSERT(pkinfo.type == pk_type,
err::network_type_mismatch(pkinfo.type));

std::istream is(&sbuf);
boost::archive::binary_iarchive ia(is);

ia >> t;
}


template <typename Connection, typename T>
void read_serialized_text_xml(Connection& connection, T& t)
{
boost::asio::streambuf sbuf;
connection.read(sbuf);

#if 0 // Debugging: Write contents to file instead.
std::istream ist(&sbuf);
std::ofstream ofs("config.ser");
std::copy(std::istreambuf_iterator<char>(ist),
std::istreambuf_iterator<char>(),
std::ostream_iterator<char>(ofs));
#endif

std::istream is(&sbuf);
boost::archive::xml_iarchive ia(is);

ia >> BOOST_SERIALIZATION_NVP(t);
}


template <typename Connection, typename T>
void read_serialized_text_xml(Connection& connection,
T& t,
uint32_t pk_type)
{
boost::asio::streambuf sbuf;
PacketInfo pkinfo = connection.read(sbuf);

CRETE_EXCEPTION_ASSERT(pkinfo.type == pk_type,
err::network_type_mismatch(pkinfo.type));

std::istream is(&sbuf);
boost::archive::xml_iarchive ia(is);

ia >> BOOST_SERIALIZATION_NVP(t);
}

template <typename Connection>
void read_serialized_text(
Connection& connection,
boost::property_tree::ptree& t)
{
boost::asio::streambuf sbuf;
connection.read(sbuf);

std::istream is(&sbuf);
boost::archive::text_iarchive ia(is);

boost::property_tree::load(ia, t, 0);
}

/**
* @brief Writes entire contents of stream, is, to destination represented by 'connection'
* incrementally based on 'chunk_size.' Enables "streaming" of files across connections.
Expand Down

0 comments on commit 02c5b47

Please sign in to comment.