Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] continuous reads into streambuf
From: Stephan Menzel (stephan.menzel_at_[hidden])
Date: 2018-05-25 15:12:56


On Fri, May 25, 2018 at 2:49 PM, Raymond Burkholder via Boost-users <
boost-users_at_[hidden]> wrote:

>
> Would it be possible for you to post a modified snippet of your code
> showing your new way of doing things?
>

I have changed many things and that wasn't real code. But the easiest way
to transform my example would be to change the header size. I went from
having a CRLF limited line to a 64 bit integer, which I serialize right
into the buffer. This way I always know how large it is and don't need
read_until anymore. Like this:

void read_header_size() {

  asio::async_read(m_socket, m_streambuf, asio::transfer_at_least(8),
    [self](const boost::system::error_code, const std::size_t) {
        // < handle error skipped >

        boost::uint64_t header_size = 0;

        std::istream is(&self->m_streambuf, std::ios::binary);
        is.read(reinterpret_cast<char *>(&header_size),
sizeof(header_size));

        self->read_header(header_size);
     });
}

The rest could stay as it is. We're not mixing the functions anymore.
I changed more though. Gave my packet a method to de-serialize itself from
a streambuf and return the number of missing bytes or 0 if ready. Then I
call this until it returns a number of bytes and only then go for more
async read, with the handler calling the de-serialize again.
Advantages are less code and I can use the same method on client and server
alike.

Cheers,
Stephan



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