I need to do async_read_until, after which I want to call async_read_some using the same boost::asio::streambuf that was used for async_read_until.
The reason why I want to use the same boost::asio::streambuf is that after async_read_until there might be some data left in the buffer. When reading via async_read_some I want to get not only the new data that is available but also the data that might have been left after async_read_until.
However, async_read_some does not have an overload for boost::asio::streambuf. How can I solve this problem?
Here is an example of the code I need to execute. Note that read_some can be called multiple times.
class io
{
//...
public:
void read_until()
{
boost::asio::async_read_until(m_socket, m_buffer, "\n",
[this](const boost::system::error_code& ec, std::size_t bytes_transferred)
{
read_some();
});
}
void read_some()
{
// I need use async_read_some here into m_buffer
// ...
// read_some() can be called again from completion handler
}
private:
boost::asio::streambuf m_buffer;
boost::asio::ip::tcp::socket m_socket;
};
I read that you can use prepare/commit/consume, but I don't fully understand how to use these methods correctly. Can someone give an example and explain what exactly these methods do and how to use them correctly?
UPDATE
Please answer the following questions:
- Is it true that boost::asio::streambuf consists of at least two buffers? A read buffer and a write buffer?
- If so, is it true that boost::asio::streambuf::prepare allocates space in the write buffer?
- If so, is it true that boost::asio::streambuf::commit copies data from the write buffer to the read buffer?
- Is it true that boost::asio::streambuf::consume removes data from the read buffer?
- Is it true that if the read buffer contained data [my_data], and then prepare(n), async_read_some and commit is called, then after commit the read buffer will contain [my_data]+[new_data_from_async_read_some]?
