I need to use the data read from an asio::socket (with ASIO_STANDALONE) as an istream
Up to now I have this code
asio::error_code ec;
auto bytes_transferred = asio::read_until(socket, buffer, '\n', ec);
if(!ec)
{
buffer.commit(bytes_transferred);
std::istream line(&buffer);
// use line
buffer.consume(bytes_transferred);
}
the problem I'm facing is that the '\n'
appears at the end of the istream, since it is not discarded by the read_until
.
The declarations are
tcp::socket socket;
asio::streambuf buffer;
So I need to ignore the '\n' char when constructing the stream. I tried (as I saw in this question efficient copy of data from boost::asio::streambuf to std::string ) with
if(!ec)
{
// Ignore the last char ('\n')
std::string line = std::string(asio::buffers_begin(buffer),
asio::buffers_begin(buffer) + bytes_transferred - 1);
std::stringstream ss(line);
// use line
buffer.consume(bytes_transferred);
}
But this produces the following compile error
In instantiation of ‘struct asio::detail::buffers_iterator_types<asio::basic_streambuf<>, char>’:
lib/asio/asio/buffers_iterator.hpp:77:46: required from ‘class asio::buffers_iterator<asio::basic_streambuf<>, char>’
file.h:58:78: required from here
lib/asio/asio/buffers_iterator.hpp:60:5: error: no type named ‘value_type’ in ‘class asio::basic_streambuf<>’
{
^
lib/asio/asio/buffers_iterator.hpp: In instantiation of ‘class asio::buffers_iterator<asio::basic_streambuf<>, char>’:
file.h:58:78: required from here
lib/asio/asio/buffers_iterator.hpp:455:43: error: no type named ‘const_iterator’ in ‘class asio::basic_streambuf<>’
typename BufferSequence::const_iterator begin_;
and a few others always complaining about missing types. I'm fairly new to C++ and this message is kind of cryptic to me, moreover I'm not sure this is the best approach, as I will have 3 copy of the same data (in the buffer, in the string and in the stream)
What could be a possible solution to my problem?