I would like to use the very convenient Boost async_read_until to read a message until I get the
delimiter.
I like using this delimiter because it's easy to debug with telnet and make multiline commands. I just signal end of command by two new lines.
I call async_read_until
like this:
void do_read()
{
boost::asio::async_read_until(m_socket,
m_input_buffer,
"
",
std::bind(&player::handle_read, this, std::placeholders::_1, std::placeholders::_2));
}
And my handler looks like this at the moment:
void handle_read(boost::system::error_code ec, std::size_t nr)
{
std::cout << "handle_read: ec=" << ec << ", nr=" << nr << std::endl;
if (ec) {
std::cout << " -> emit on_disconnect
";
} else {
std::istream iss(&m_input_buffer);
std::string msg;
std::getline(iss, msg);
std::cout << "dump:
";
std::copy(msg.begin(), msg.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
do_read();
}
}
I wanted to use std::getline
just like the example, but on my system this keeps the
character. As you can see, if I connect to the server and write hello
plus two CRLF, I get this dump server side:
handle_read: ec=system:0, nr=9
dump:
104, 101, 108, 108, 111, 13,
^^^
here
By the way, this will also keep the next new line in the buffer. So I think that std::getline
will not do the job for me.
I search a convenient and efficient way to read from the boost::asio::streambuf
until I get this
delimiter. Since I use async_read_until
once at a time, when the handler is called, the buffer is supposed to have the exact and full data isn't it? What do you recommend to read until I get
?
See Question&Answers more detail:
os