Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] continuous reads into streambuf
From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2018-05-24 15:22:36


On Thu, May 24, 2018 at 7:59 AM, Stephan Menzel via Boost-users
<boost-users_at_[hidden]> wrote:
> I am quite lost here. If anybody has some suggestion it is much appreciated.

I have no idea why your code is malfunctioning because I am not
familiar with using std streams with asio (although I know asio pretty
well, asio::basic_streambuf in particular).

However, let me propose a wild idea. Set your current code aside for
the moment and reimplement it a different way. Instead of doing three
asynchronous reads, call async_read_some once into your
basic_streambuf. Use a reasonably large size in the call to prepare
(say, 2048 or higher). When you have data in your buffer, inspect the
contents and determine if you have a complete message. If not, then
keep looping and calling async_read_some until your buffer has an
entire message. Then extract the message, consume the bytes used, and
repeat. When extracting the bytes do not use asynchronous I/O.

What, you ask, is the purpose of all this? Calls to asynchronous
initiating functions are VERY expensive. Calling async_read_until to
get some data, then async_read to get the header, then async_read to
get the body, is going to kill performance. Instead, you should be
using async_read_some to transfer into your streambuf, all the
existing data that has already been received into the socket's kernel
buffer. It is possible that you will get more than one message into
your buffer when you do this (if your protocol is full-duplex). This
is great when it happens because you are getting the most work out of
each call to async_read_some (which as I stated, is quite expensive).

In the absence of any other information about why your current code is
malfunctioning, it is possible that this reimplementation will not
only make your code perform better and become easier to reason about,
but might also solve your unknown bug.

Regards


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net