Reputation: 2144
I am trying to use a std::istream as data source. I want to place custom binary data in the istream's stream buffer so it can later be retrieved from the istream.
I have read about boost::asio::streambuf and how it is used to do exactly what I want but using a socket as data source instead of an in-memory buffer, which is what I would like to use.
From what I understand from the documentation, the steps should be:
I don't know how to address step 4, so I don't know even if I'm going in the right direction.
Are the depicted steps correct? If so, how to address step 4?
Upvotes: 4
Views: 7815
Reputation: 393769
You can easily send any std stream, so you can also you a stringstream
. You can write binary data to your stringstream (it is just a byte array, effectively).
A few samples:
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream.write(&binarydata_buf, sizeof(binarydata_buf));
// or use stream operators: request_stream << "xxx" << yyy;
// Send the request.
boost::asio::write(socket, request);
If you already have a fully populated istream (using std::cin as dummy in this example):
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << std::cin.rdbuf() << std::flush;
// Send the request.
boost::asio::write(socket, request);
Ways to populate an istream are e.g. ostream::write
or Boost Serialization binary_archive
There are many ways to skin a cat of course, so be sure to think over the other options before blindly copying this.
See How to send ostream via boost sockets in C++?
Upvotes: 6
Reputation: 2147
Why not just transmit the data across a socket into your streambuf? If you can link the std::istream to the asio::streambuf which is listening on a particular socket, just use the same socket with boost::asio::write to send it data.
There isn't much penalty to using actual sockets intra-process, rather than trying to simulated it by accessing underlying data structures.
Upvotes: 0