Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::asio::streambuf::consume - Injects garbage character

29 views
Skip to first unread message

Christopher Pisz

unread,
Oct 27, 2017, 3:42:28 PM10/27/17
to
When I lose connection I try to reconnect in a loop forever and once I reconnect I send a log in message to the component I am connection to. That component then sends back a log in response that looks like "MyResponse"

The initial connection works fine. After I reconnect, however, I get garbage before the expected message, that looks like: "ýMyMessage"

After googling this. I see many questions on Stack Overflow about the boost::asio::streambuf that is used for async sockets in boost::asio. I have followed the advice there and called consume.

I have also used wireshark to make sure that the garbage character is not being sent and it is not.

After much debugging, it appears as though the calls to consume are injecting a character rather than clearing out all characters.

Here is a minimal example:

#include <boost/asio.hpp>

#include <iostream>
#include <sstream>
#include <string>

int main()
{
boost::asio::streambuf buffer;

std::cout << "buffer size " << buffer.size() << std::endl;

buffer.consume(std::numeric_limits<size_t>::max());

std::cout << "buffer size " << buffer.size() << std::endl;

std::istream is(&buffer);
std::string contents;
is >> contents;

std::cout << "Contents: " << contents << std::endl;

std::cout << "buffer size " << buffer.size() << std::endl;

return 0;
}
Output:

buffer size 0
buffer size 1
Contents: ²
buffer size 0
If I do not use consume, I get some of the message, previous to disconnect, before the first message after reconnection, in my server code.

If I do use consume, I get a garbage character.

Öö Tiib

unread,
Oct 27, 2017, 4:30:29 PM10/27/17
to
It smells (no time to try) that the issue in posted code is because you
attempt consuming std::numeric_limits<size_t>::max() bytes. It is
something like 18446744073709551615 bytes on most platforms and that is
way more data than we have in internet. Perhaps try consuming somewhat
less and see what happens? If it is defect of boost ... then debug it
out and send them a patch. ;)

Christopher Pisz

unread,
Oct 27, 2017, 4:37:11 PM10/27/17
to
I've tried other values as well, with the same results.

I could not debug boost code if my life depended on it. I require comments, well named variables that are self describing, and the like...

I do like using it though :) Until something like this happens.

Öö Tiib

unread,
Oct 27, 2017, 4:42:55 PM10/27/17
to
OK, took time, tried. I can't reproduce with certain other values.
Demo here:
http://coliru.stacked-crooked.com/a/82265415d2ada82b

Christopher Pisz

unread,
Oct 27, 2017, 4:48:21 PM10/27/17
to
buffer.max_size() results are the same.
I did get a good result with 777, however. Very wierd.

Öö Tiib

unread,
Oct 27, 2017, 4:53:21 PM10/27/17
to
I suspect that std::numeric_limits<size_t>::max() - buffer.max_size() == 0
So it is not less like I suggested. ;)

Christopher Pisz

unread,
Oct 27, 2017, 4:58:18 PM10/27/17
to
Fair enough. Though it should work. Looks like a boost bug to me. thanks!

Jorgen Grahn

unread,
Oct 28, 2017, 3:58:07 AM10/28/17
to
On Fri, 2017-10-27, Christopher Pisz wrote:
...
>> > #include <boost/asio.hpp>
...
> I could not debug boost code if my life depended on it. I require
> comments, well named variables that are self describing, and the
> like...

IIRC, Boost.Asio's API documentation is rather sketchy, too ...
I remember having to guess how things worked by extrapolating from BSD
sockets.

> I do like using it though :) Until something like this happens.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages