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.